"When to while and when to for" // My for loop isn't printing out what I expect


#1

Hi guys, I have written a for loop that Codecademy accepts as correct, but it isn't printing out the answer I would expect. Any help much appreciated.

Thanks!
Stuart

--

The lesson is 'Introduction to 'While' Loops in JS, questions 7, "When to while and when to for".

https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/2/1?curriculum_id=506324b3a7dffd00020bf661#

My code is below. When I run this, it prints out:

10
3
4
5
6
7
8
9
10
11
12
13

However, I would expect it to print out:

10 (from the while loop)
10 (from the for loop: j = i, and i = 7. so console logging j+3 would log 7+3 = 10)
11 (from the for loop: j ++ now equals 8. so console logging j+3 would log 8+3 = 11)
12 (from the for loop: j ++ now equals 9. so console logging j+3 would log 9+3 = 12)
13 (from the for loop: j ++ now equals 10. so console logging j+3 would log 10+3 = 13)

My code is:

    var i = 7;

    while(i){
        console.log(i+3);
        i = false;
    };

    for (j = i; j<=10; j++) {
        console.log(j+3);
    };


#2

okay, lets go through it:
10 (from the while loop)
I agree here.

next one:
10 (from the for loop: j = i, and i = 7. so console logging j+3 would log 7+3 = 10)
I disagree here, let me explain why. i isn't 7. in the while loop, you changed i to false. False in a number would be 0. so then j = i would be j = 0, then console.log(j + 3) would print 3


#3

why did you expect like that?
printing is right.
it should be.

var i=7 ok?
while (i)
console.log(i+3) means 7+3 console.log=10
then i=false
loops stop
because while(i) means while(true)
but its false now
you can understand this just run this code

var i = 7;

while(i){
    console.log(i+3);
    i = false;
};

next for loop

for (j = i; j<=10; j++) {
console.log(j+3);
};

for(j=0(because i=false means i=0);j<=10;j++)
console.log(j+3); means (0+3)
};
you can understand this just run this code

for (j = 0; j<=10; j++) {
console.log(j+3);
};

Any question?
@terarockstar74318


#4

Thanks for the reply.

So my thinking was that as 'i' was set to false within the while loop, that this would not impact the global 'i' which would stay at 7.

I thought it was a global / local scope thing, whereby the variable 'i' is only changed within the while loop, unless I had put 'var i = false', in which case that would then change it.

Or have I got it the wrong way round?


#5

Thanks for the reply. I've posted the below reply, above, but it would also be good to get your answer to this.


So my thinking was that as 'i' was set to false within the while loop, that this would not impact the global 'i' which would stay at 7.

I thought it was a global / local scope thing, whereby the variable 'i' is only changed within the while loop, unless I had put 'var i = false', in which case that would then change it.

Or have I got it the wrong way round?


#6

When to while and when to for is a good question. On the surface we can come up with one criterion: Is there an object with a fixed size? Given an array or an object we would immediately reach for the range iteration tool, for. A great tool at that, iterating arrays and objects.

Given a state based scenario, while is rangeless. It has no limiting value since it is only a control, an on/off switch, as it were.

while is not an if statement but its conditional expression is evaluated exactly as an if statement, with boolean yield. do lets us hold off the evaluation until after at least one time through the body code, but beyond that, it is still a while loop and it still behaves similar to if.

States are held in variables and their related expressions. Functionally speaking we would hope that those states are local, but some may still be in the global scope. In the current operating environment we are using an ECMA 5.x script engine which leaks memory to scopes containing loop blocks. ECMA 6 does not do that anymore. We finally get to work with scope the way Ruby and Python do.

Variables declared outside of a loop block will be seen inside the loop. Those declared inside (though this is not a good practice) will not be seen outside of the loop (by rights). for loops do create variables, but in the scope of the block, not within the loop. This is another reason to use for in some instances. for is another scope. By rights, we should not be able to see any of the variables outside of the loop.

I know this is confusing. We really must get a grasp on scope and related concepts such as execution context and closure. These are fundamental concepts, but not elementary, per se. It's mental homework, for sure.


#7

Good question :bulb:

your code was:

var i = 7;

while(i){
    console.log(i+3);
    i = false;
};

for (j = i; j<=10; j++) {
    console.log(j+3);
};

here "i" is a global variable so when i=false it remains false.

if you run same code inside a function, it would be local variable and
local variables have local scope,they can only be accessed within the function.
run same code inside a function

var i = 7;

function name(){ 
    while(i){
    console.log(i+3);
  i = false;
}
};

for (j = i; j<=10; j++) {
    console.log(j+3);
};

hope this'll help :slight_smile:
any question?
@terarockstar74318


#8

but using the var keyword, is what is creating local variable. Either way, with ECMA5, it seems with and without var keyword, your global variable gets overwritten


#9

i want to say more

run this code

var i = 7;
function name(){
var i=5
console.log(i); //inside the function result 5
};
name();
console.log(i) //outside the function result 7

now tun this code too

var i = 7;
function name(){
i=5
console.log(i); //inside the function result 5
};
name();
console.log(i) //outside the function result 5

question is why?
i wrote i =5 not var i=5;
If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable

@terarockstar74318


#10

Aha, ok. Thanks!

So the local / global scope thing only comes into play when you have functions, and not when you just have loops?


#11

yes and no, it also comes into play with the latest JS standards. Look at this for loop:

for (j = 0; j<=10; j++)

with the latest standards, j is local as well (it doesn't exist outside the for loop), but codecademy doesn't use this latest version. Roy explains this in detail