0 added
0 removed
Original
2026-01-01
Modified
2026-03-09
1
<p>Something went wrong, and instead of printing pages one through seven, the driver prints a blank page and then pages one through six. Let’s look at the sequence in which the parts of the for structure are implemented using this example:</p>
1
<p>Something went wrong, and instead of printing pages one through seven, the driver prints a blank page and then pages one through six. Let’s look at the sequence in which the parts of the for structure are implemented using this example:</p>
2
for (var i = 0; i < 5; i = i + 1) { console.log(i); }<ol><li>First, var i = 0; works. i variable will be created, which is equal to 0.</li>
2
for (var i = 0; i < 5; i = i + 1) { console.log(i); }<ol><li>First, var i = 0; works. i variable will be created, which is equal to 0.</li>
3
<li>Then the i < 5; test will be performed. Since i is now equal to 0 and this value is less than 5, the condition returns true. This means that the loop can continue working and go to execution of the code from the body of the loop.</li>
3
<li>Then the i < 5; test will be performed. Since i is now equal to 0 and this value is less than 5, the condition returns true. This means that the loop can continue working and go to execution of the code from the body of the loop.</li>
4
<li>The code will be executed from the body of the loop. In our case, the console will log 0, the current value of the i variable.</li>
4
<li>The code will be executed from the body of the loop. In our case, the console will log 0, the current value of the i variable.</li>
5
<li>Then i = i + 1 will be executed. The variable i becomes equal to 1.</li>
5
<li>Then i = i + 1 will be executed. The variable i becomes equal to 1.</li>
6
<li>Then i < 5; will be tested. It will return true once again, since 1 is less than 5.</li>
6
<li>Then i < 5; will be tested. It will return true once again, since 1 is less than 5.</li>
7
<li>The body of the loop is executed again, then the value i will increase by one, and so on in a loop until the condition i < 5; doesn’t return false. Then the loop will finish its work.</li>
7
<li>The body of the loop is executed again, then the value i will increase by one, and so on in a loop until the condition i < 5; doesn’t return false. Then the loop will finish its work.</li>
8
</ol><p>Now let’s look at the example from the previous task. Why was the blank page printed first?</p>
8
</ol><p>Now let’s look at the example from the previous task. Why was the blank page printed first?</p>
9
<p>On the first go-round or, scientifically speaking, during the first<em>iteration</em>of the loop, variable page still equals zero. And it increases to one already after the first iteration. If you change the initial value of the variable page from 0 to 1, then the problem disappears.</p>
9
<p>On the first go-round or, scientifically speaking, during the first<em>iteration</em>of the loop, variable page still equals zero. And it increases to one already after the first iteration. If you change the initial value of the variable page from 0 to 1, then the problem disappears.</p>
10
<p>Why are there six pages, and not seven? Let’s take a look at the loop step-by-step:</p>
10
<p>Why are there six pages, and not seven? Let’s take a look at the loop step-by-step:</p>
11
Preparation: totalPages = 7; page = 0 1 iteration: page = 0; 0 < 7? yes! Printing page 0; page = 1 2 iteration: page = 1; 1 < 7? yes! Printing page 1; page = 2 3 iteration: page = 2; 2 < 7? yes! Printing page 2; page = 3 4 iteration: page = 3; 3 < 7? yes! Printing page 3; page = 4 5 iteration: page = 4; 4 < 7? yes! Printing page 4; page = 5 6 iteration: page = 5; 5 < 7? yes! Printing page 5; page = 6 7 iteration: page = 6; 6 < 7? yes! Printing page 6; page = 7 8 iteration: page = 7; 7 < 7? no! Finish the loop!<p>To get the seventh page, you need to change the comparison from “less than” to “less than or equals to”. In this case, the check on the eighth iteration of the loop will work and the seventh page will be printed.</p>
11
Preparation: totalPages = 7; page = 0 1 iteration: page = 0; 0 < 7? yes! Printing page 0; page = 1 2 iteration: page = 1; 1 < 7? yes! Printing page 1; page = 2 3 iteration: page = 2; 2 < 7? yes! Printing page 2; page = 3 4 iteration: page = 3; 3 < 7? yes! Printing page 3; page = 4 5 iteration: page = 4; 4 < 7? yes! Printing page 4; page = 5 6 iteration: page = 5; 5 < 7? yes! Printing page 5; page = 6 7 iteration: page = 6; 6 < 7? yes! Printing page 6; page = 7 8 iteration: page = 7; 7 < 7? no! Finish the loop!<p>To get the seventh page, you need to change the comparison from “less than” to “less than or equals to”. In this case, the check on the eighth iteration of the loop will work and the seventh page will be printed.</p>
12
8 iteration: page = 7; 7 <= 7? yes! Printing page 7; page = 8 9 iteration: page = 8; 8 <= 7? yes! Finish the loop!<p>To summarize: to make everything print as it should, we need to start counting from page number 1 and use the <= sign in condition to include the last page in the loop.</p>
12
8 iteration: page = 7; 7 <= 7? yes! Printing page 7; page = 8 9 iteration: page = 8; 8 <= 7? yes! Finish the loop!<p>To summarize: to make everything print as it should, we need to start counting from page number 1 and use the <= sign in condition to include the last page in the loop.</p>