Code is correct but output displays a little extra!


#1

In section 2. while syntax, I have the following code:

var understand = true;
var i = 0;
while( i < 5  ){
	console.log("I'm learning while loops!");
	understand = false;
	i++;
}

Output:

I'm learning while loops!
I'm learning while loops!
I'm learning while loops!
I'm learning while loops!
I'm learning while loops!
4

I am not sure why "4" is displayed as a part output. Can someone please explain the reason behind this output.


#2

@sureshdev,

Try it

var understand = true;
var i = 0;
while( i < 5  ){
	console.log("I'm learning while loops!");
	understand = false;
	i++;
}
console.log( "=End-of-my-code=" );

=============================

What they are trying to achieve,
that you are able to use a while-loop.

             point of entry
             in while-loop
                   |
                   V
 ->  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;

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

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.

Reference::

google search
== the Book ==
javascript while site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while

== discussions / opinions ==
javascript while vs do while site:stackoverflow.com
http://stackoverflow.com/questions/5599027/the-do-while-statement

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript


#3

That is a "feature" of the console, which echoes the last value in your code (unless it is undefined). So in your case you see the value of this statement: i++;. This happens all the time and you might go back to one of the first exercises where this was used before console.log was introduced. As said undefined is not echoed so if you wan to get rid of it just move your console.log after the i++ (as they do not depend on each other this doesn't change your outcome).


#4

console.log("Thanks for the code, buddy!");


#5

I really appreciate your help, thanks guys!