Difference between revisions of "Loops"

From CodeStuff
Jump to: navigation, search
(Created page with "This is a very simple loop. <edcode> for (var i=0; i<10; i+=1) { print("the value of i is currently "+i); } </edcode> This is called a for loop and is most often used...")
 
(for Loops)
 
Line 12: Line 12:
 
  for(where_to_start; how_long_to_run; what_to_change) {  }
 
  for(where_to_start; how_long_to_run; what_to_change) {  }
  
 +
so
 +
 +
for(var i=0;i<10;i+=1) { ... }
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
Line 31: Line 34:
 
|increase the value of i by 1
 
|increase the value of i by 1
 
|}
 
|}
 
  
 
====while Loops====
 
====while Loops====

Latest revision as of 06:14, 19 June 2014

This is a very simple loop.


This is called a for loop and is most often used for counting a number of times to run a piece of code.

for Loops[edit]

for loops take the form

for(where_to_start; how_long_to_run; what_to_change) {  }

so

for(var i=0;i<10;i+=1) { ... }
Where to start When to End What to Change
Code var i=0 i<10 i+=1
What it does Creates a variable called i

sets i to zero

keep running the loop until

i<10 is no longer true

increase the value of i by 1

while Loops[edit]

The for loop can be thought of as a simplified form of the while loop.

do while loops[edit]

do while loops enable you to move the check to after the loop contents have been run, instead of before.