0

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.

1
  • have fun learning :) that's why we all do it Commented Apr 13, 2012 at 15:24

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)

3 Comments

//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)// @krystan honour - Is it that the increment is done first and then the condition is checked for? and the loop statement it means here - console.log right i.e. inside the {} brackets. Thank you for your help. I hope you'll help me clear this up.
the order is do a check that i is not violating the 'while cause' so i is not 41 in this case then execute the code in braces, increment i after the code in braces then check. this is why i is equal to 41 but no code is executed in the braces for that because the gate condition is reached. Hope this helps don't forget to upvote and accept the answer if it helps :) and welcome to the site.
The condition really only means anything inside the loop and not outside. Even though the condition is i <= 40, a console.log done outside the loop will print 41 because that's when the incrementing stops, no?
1

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 */ ) 

Comments

0

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.

Comments

0

When i = 41 the conditional statement (i <= 40) is checked and false is returned the loop stops executing its contents.

2 Comments

Does't look like homework to me, OP is just slightly confused about the order of operations in loop conditions
Fair enough, removed that unnecessary statement.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.