If, While and Dowhile

What’s the difference between if, while and ***do…while ***besides the way they are presented?

 
//if
if (truth == true) {
	i++
}
//while
while (truth == true) {
	i++
}
//do...while
do (i++) {
} while (truth == true)
 

if is a one time true or false check ie.

if (truth == true) {
    i++
}

will return i as 1 IF i is the same as truth prior to the if statement

while is a loop which will loop all the time that your statement is true, ie.

i=0
while (i<3) {
    i++
}

will loop 3 times and i will end up as 3 (at which point the condition becomes false and it therefore stops the loop

the do while is incredibly similar to the while loop BUT the do while loop will always execute at least once. ie.

i=0
do{
	i++
}
while(i<5)

will run through once, and then if the while condition is true, it will repeat until the while condition is not true… in this example it will run a total of 5 times… also note, you got the format slightly wrong for this last one. there is no () after a do operation :wink:

hope this helps!

Prophet.

and lest we not forget about for and for…in loops :wink:

i seeeeeeeee:eye: Thanks :smiley:

no probs :wink: and @sen: isnt a for…in loop just a lazy alternative to tracing a particular property? :wink:

Prophet.

for in doesnt trace, but it does iterate - iterates through all the enumerable properties of an object… as a loop. (though normal for loops are just restructured while loops)

so while is pretty much the same as for…but in a diff format?

yes. There are two versions of for loops, for and for.in. For…in loops are quite a bit different, but normal for loops are an abbreviated version of a commonly used while loop.

While loops work on the following principle (and Ill cover the basics for anyone else who’s curious):

[FONT=Courier New]while (condition is true){
repeat actions within code block
}
otherwise, exit out of code block and continue script[/FONT]

Common use of loops are to perform like actions on many objects or variables which are conveniently ordered or named with a steadily increasing number value. The names value1, value2, value3, for example, all have incrementing values in their names meaning you can loop through at add 1 to a number to represent the next variable in each iteration of the loop. Arrays are perfect because they are simply structured numerically in that manner

[FONT=Courier New]myArray = new Array(“a”,“b”,“c”);[/FONT]

In myArray, myArray[0] is “a”, myArray[1] is “b”, and myArray[2] is “c”. Using loops you just start an index variable, usually set as the variable i, at 0 and increase it through each iteration making it so you can easily the next value in the array with myArray*;

i = 0;
while(i < myArray.length){
trace(myArray*);
i++;
}

Here, i starts at 0. The while condition is checked and makes sure that i is less than the array’s length (if its not, there are no more values in the array to check). If so, the while block is run and myArray* is traced. After that, i is increased by one making i 1 following the first loop. The while is checked again and if i is still low enough, goes through the code again. Each time the loop performs an iteration, i has changed so the element we’re referencing in the array has changed. These means each loop is different (which is usually the idea).

So with the above use of the while loop you have the format

[FONT=Courier New]set index variable
while (check index against condition){
(code)
increment (or otherwise alter) index
}[/FONT]

which can be loosely interpreted into 4 actions

[FONT=Courier New]A
while (B){
C
D
}[/FONT]

A for loop takes this and compacts it into 2 vs 4 lines. It does the same thing as a while loop, only its set up slightly different. A for loop is layed out as

[FONT=Courier New]for (A; B; D){
C
}[/FONT]

making the prior example:

while(i=0; i<myArray.length; i++){
trace(myArray*);
}

For loops make while loops smaller and puts all the components controlling the looping (initial variable setting, the condition check and the variable altering) all in one place to make it easier to understand. You see more for loops than while’s because of this. In fact I rarely use whiles. do…while loops are virtually out of my vocabulary. I almost forgot they exist but they have their uses too.

lol, nice sen! Thanks! I’m sure others would find this useful as well :smiley:

by the way, there’s a typo here:

myArray[1] is “b” isn’t it? :wink:

Vince :slight_smile:

yes, yes it is. fixed :wink:

I know a lot of people read your posts , so maybe fix this one too.
while (i=0; i<myArray.length; i++) {
trace(myArray*);
}