2.While Syntax



I am soo confused as to what the ■■■■ i have to do in this lesson. In the while loop condition i enter understand = 1 but when i run it it just screws up my browser

var understand = true;

while( understand = 1) {
    console.log("I'm learning while loops!");
    understand = false;


If you want to create a condition
you will have to use the comparison operator which in Javascript syntax is expressed by either == or ===


What they are trying to achieve,
that you are able to use 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.


google search
== the Book ==
javascript while site:developer.mozilla.org

== discussions / opinions ==
javascript while vs do while site:stackoverflow.com

== guidance ==
http://javascript.crockford.com/code.html <== code convention


Thanks- I was stuck on the same exercises. Thanks to your post I figured it out.
It works if you use understand===true in the parenthesis.


It took me awhile to realize that the variable 'understand' is initially set as 'true', so when entering 'understand ==== true' in the brackets, the while loop will run. During the running of the loop the variable 'understand' is then changed to false, so when the loop is run again, it now is false, and ends.

May be obvious to some, but helpful to others. Took me a second to figure out.


and as the Javascript interpreter expects a Boolean between =while='s parenthesis-( )
you could also have used

while( understand ) {


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.