Skip to content Skip to sidebar Skip to footer

Javascript - Variable Incremented In For Loop Issue

I was trying to make a for loop that increments through the numbers 1 - 4 and print them, but when I printed the value of i after the loop, my code outputs 5. How is this possible

Solution 1:

When you declare a variable in a loop statement, it stores the variable in the same scope as the loop. Loops increment (i++) at the end of the loop, then check the condition (i < 5) to see if they should repeat. After the loop, the variable i still exists. See the snippet below for a play-by play.

Also, you should use the var keyword when declaring i, otherwise, the variable is stored in the global scope (which is bad practice).

//variable i is captured here (with the var keyword)for(var i = 1; i < 5; i++) {
  //for the fourth iteration, i === 4//so it prints 4.document.write(i);
  
  
  //i++ happens right now, so i now is 5.//then the loop checks the condition to see if it//should continue. i isn't less than 5, so the loop breaks.
}

//and the value of i is still 5. so this prints 5.document.write('</br>' + i);

Solution 2:

You could move the final-expression part into the condition part of the for statement, where the increment takes place only if the value is smaller than 4.

var i;
for (i = 0; i < 4 && ++i;) {
    console.log(i);            // 1 ... 4
}

console.log('last value', i);  // 4

Solution 3:

The test clause of i < 5 is evaluated at the beginning of each loop; if it is false, the loop exits (leaving the value at 5). The i++ part always happens at the end of an iteration, meaning that after the number 4 has been processed in the loop, it increments it to 5, then it loops to the top of the loop where it checks the condition again (i<5) which it fails, but the value of i is not reversed back to 4.

Solution 4:

  • The condition of the loop block to be processed is i < 5, thus i must be less than 5 for the statements and expressions within it to be evaluated
  • You increment i by 1 with each pass
  • When i equals 4, you increment it by 1 again such that i will be equal to 5. i < 5 is no longer true, so within the loop block is not executed
  • Nothing turns i back one increment; so it retains it after the loop

You can apply unique ways, but generally the first and last example below is the common approach:

var log = console.log;

// Example 1: Decrease Loop Iterator Afterfor (var i = 1; i < 5; i++)
  log(i);
log('New i:', --i); // 4// Example 2:  Count in conditionalvar count = 0;
for (var i = 1; i < 5 && ++count; i++)
  log(i);
log('New i:', count); // 4// Example 3: Count in loop bodyvar count = 0;
for (var i = 1; i < 5 && ++count; i++)
  log(i);
log('New i:', count); // 4

Solution 5:

  1. you have declared i as global variable.
  2. That's how the for loop works.

for (initialization, condition, increment/decrement ) {
    
    }

You have initialized with 1 and it satisfies the condition i<5 so it enters the loop and prints i. after then it performs the increment/decrement operation and increases the value by 1 in your case. So while i=4 and i<5 it enters the loop and prints 4, but after then it performs increment operation and increase the value of i to 5.

In the next step, as the condition i<5 is not satisfied , so it breaks the loop and go to the last statement, where you are printing i, that is 5.

Hope it is clear now.

Post a Comment for "Javascript - Variable Incremented In For Loop Issue"