Loops

From CodeStuff
Revision as of 06:14, 19 June 2014 by Lerc (Talk | contribs) (for Loops)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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.