While Loops (I need teaching)


I need help on what While Loops are, what they are used for and How to create and use them
I had a hard time undestanding 'For' Loops and this isnt much better.
Please help, thank you


The simple way to differentiate between for and while loops for me is that a for loop has three requirements, and a while loop has only one.

A for loop is used when you know when it starts, when it will end, and how you want to sort through the data. For example: You know that there are 10 apples in your hand and you want to eat all of them one by one.
The code would be:

for (var apple = 1; apple < 11; apple++);

"var apple = 1" means that you start going through the apples with the first one.
"apple < 11" means that you keep going through the apples until what's left is less than 11 (meaning 10).
"apple++" means that you go through the data by increments of 1, or going through them one by one. You can decrement by 1 ("apple--") or any number you like. By 5 would be "apple += 5".

A while loop is used when you only know one thing. For example: While the number of apples in your hand is still more than 2, keep eating.
The code is more simple. It only requires you to write "while (apple > 2)". You stop going through the loop when there are only two apples left.

I hope I didn't make you more confused..



== the Book ==
the for-loop:

the while-loop:

the do-while-loop:

a while-loop.

             point of entry
             in while-loop
 ->  while ( condition===true )   -->EXIT--->if condition===false
|       {  // begin of while-code-block  
|          // your code that will be executed 
|          //       as long as while-condition === true
^<- - - } //end of while-code-block, 
                     while-condition is checked again !!

They give you the code:

var understand = true;

	console.log("I'm learning while loops!");
	//Change the value of 'understand' here!
	understand=false;   //<<== you make sure

1 You assign true to the variable understand

2 The while-statement -tests the condition-,
--which is the variable understand with its value being true
3 Access is granted to the while-code-block
4 console.log("I'm learning while loops!"); is executed.
5 assignment of the false value to the variable understand
6 END of while-code-block is reached,
goto 2 //The while-statement -tests the condition-, understand

As understand renders to false,
the while will NOT execute its body,
and will EXIT.


You didn't confuse me but how exactly would you go about making a successful while loop?


I've made a longer post over here covering all kinds of loops (that occurred in the exercises so far) with examples syntax and when to use, maybe have a look at it:

Questions, remarks and feedback appreciated.
@laushinka nice overview but better replace the semicolon after the for loop with a pair of {} otherwise this is a rather pointless loop just counting apples to 11 without doing anything else.


@haxor789 Oh right of course! How did I make that mistake. Thanks!