Reputation: 11
I am actually a newbie to javascript and just to learn the basics, I've started taking lessons on codecademy.
var i;
for (i = 1; i <= 40; i++) {
console.log("i is now equal to " + i);
}
Here's my question - In this code, when i <= 40
, it should print till 41, no? Because the for
loop continues to run until the condition i <= 40
is true
but why is that it only prints till 40 then?
i.e.
When it runs at 40, it would print till 41 by incrementing it once, no? Then why is it that it prints only till 40?
If someone also explained me what exactly the code does and what each line means and does, I'd be really very grateful. Thanks.
Upvotes: 1
Views: 196
Reputation: 20131
i=1
is the initialiser, so the first value of i
is 1.
After initialising, and on each iteration back to the top, i
is checked against the condition i<=40
. After each completed iteration, the increment is performed i++
. On the 40th iteration, i=40, which passes the condition and prints the log. Then (as in all previous iterations) i++
is called, which notches i
up to 41. This then fails the i<=40
test, so the loop does not execute the code block again.
The result is that the console prints 1 to 40, and then quits with i=41
.
Upvotes: 2
Reputation: 6783
it says the following
var i; // DECLARE A NEW VARIABLE NAMED i
// set i to 1 and do things while i is less than or equal to 40
// after this block of code has executed increase the counter by 1
for (i = 1; i <= 40; i++) {
console.log("i is now equal to " + i); // print "i is now equal to <the value of i>"
}
this means it will print out the number 1 the first time and each time increment by 1 until such time i is equal to 41 at which time the loop will finish (but will do no more operations between the {
's meaning 41 will never be printed)
if you had written the following
for (i = 0; i <= 40; i++) {
console.log("i is now equal to " + i); // print "i is now equal to <the value of i>"
}
it would make no different aside the count would start from 0, one more iteration but it would still exit when i became equal to 41 but it still wouldn't print 41
If you were to do another log statement outside the loop you'd find that i is equal to 41 but this value was never used in the body of the loop (the bit in curly braces)
Upvotes: 1
Reputation: 14219
When i = 41
the conditional statement (i <= 40
) is checked and false
is returned the loop stops executing its contents.
Upvotes: 0
Reputation: 2812
The condition, in your case i <= 40, is checked before executing the code inside the for loop. So when i gets to 41, i <= 40 will evaluate to false, so the block is not executed and 41 is not printed.
Also, the increment (i++) happens before the condition.
Upvotes: 0
Reputation: 92274
The last time the loop is run, i
is 40, once it hits 41, it drops out of the loop. The i++
statement gets executed after the loop statements.
var i;
for (i = 1; i <= 40; i++) {
console.log("i is now equal to " + i);
}
console.log("i is now equal to " + i); // Now it's 41
This is how the for loop works
for ( var i= 0; /* initializer */;
i < 40; /* check whether it should run, before running loop statements */
i++ /* happens after the loop statements */ )
Upvotes: 1