HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-03-09
1 <p>Excellent! The first part of the driver is executed. The second step: printing all pages of the document.</p>
1 <p>Excellent! The first part of the driver is executed. The second step: printing all pages of the document.</p>
2 <p>This task also has many repeated actions, and we therefore won’t be able to solve it without loops. Let’s look at for loop in more detail:</p>
2 <p>This task also has many repeated actions, and we therefore won’t be able to solve it without loops. Let’s look at for loop in more detail:</p>
3 for (var i = 0; i &lt; 5; i = i + 1) { // repeated commands or the “loop body” }<p>How does the for loop work? It executes the actions from the loop body again and again, as long as the condition returns true. We are about to look into what kind of condition it is and how we can write loops correctly.</p>
3 for (var i = 0; i &lt; 5; i = i + 1) { // repeated commands or the “loop body” }<p>How does the for loop work? It executes the actions from the loop body again and again, as long as the condition returns true. We are about to look into what kind of condition it is and how we can write loops correctly.</p>
4 <p>Loop control code must be in parentheses. It consists of three parts, separated by ;. The meaning of each part is this:</p>
4 <p>Loop control code must be in parentheses. It consists of three parts, separated by ;. The meaning of each part is this:</p>
5 <ol><li><p>The first part is the preparatory one. Commands are started here<em>once</em>before the start of the loop. Usually, the initial value for the counter variable is set here. We can say that the first part is the initial setting for the loop.</p>
5 <ol><li><p>The first part is the preparatory one. Commands are started here<em>once</em>before the start of the loop. Usually, the initial value for the counter variable is set here. We can say that the first part is the initial setting for the loop.</p>
6 <p>In the example below, we created the counter variable i and set to it the initial value 0. The variable will be equal to this value when the loop begins to work.</p>
6 <p>In the example below, we created the counter variable i and set to it the initial value 0. The variable will be equal to this value when the loop begins to work.</p>
7 <p>Note that in the loop, we create a counter variable with var, as in the case of any other variable. Traditionally, such a variable is called i (from the word index), but it can have any other name. For example, if a loop counts days, the counter variable may be called day, and if it counts pages of the document, then it can be called page.</p>
7 <p>Note that in the loop, we create a counter variable with var, as in the case of any other variable. Traditionally, such a variable is called i (from the word index), but it can have any other name. For example, if a loop counts days, the counter variable may be called day, and if it counts pages of the document, then it can be called page.</p>
8 for (<b>var i = 0</b>; i &lt; 5; i = i + 1) { }</li>
8 for (<b>var i = 0</b>; i &lt; 5; i = i + 1) { }</li>
9 <li><p>The second part is a test. It contains a condition and starts<em>before</em>each new iteration of the loop. The condition here works according to the algorithm you are familiar with. If the condition returns true, the loop completes one more iteration, otherwise the loop stops.</p>
9 <li><p>The second part is a test. It contains a condition and starts<em>before</em>each new iteration of the loop. The condition here works according to the algorithm you are familiar with. If the condition returns true, the loop completes one more iteration, otherwise the loop stops.</p>
10 <p>In the example, we indicated that the loop should work until the variable ibecomes less than 5.</p>
10 <p>In the example, we indicated that the loop should work until the variable ibecomes less than 5.</p>
11 for (var i = 0;<b>i &lt; 5</b>; i = i + 1) { }</li>
11 for (var i = 0;<b>i &lt; 5</b>; i = i + 1) { }</li>
12 <li><p>The third part is an additional one, although scientifically speaking it is called the “pattern of change”. The code for the third part starts<em>after</em>each iteration of the loop. That is, after the code is executed from the body of the loop. Usually the counter variable is changed there.</p>
12 <li><p>The third part is an additional one, although scientifically speaking it is called the “pattern of change”. The code for the third part starts<em>after</em>each iteration of the loop. That is, after the code is executed from the body of the loop. Usually the counter variable is changed there.</p>
13 <p>In our case, we indicated that after each iteration of the loop, the variable i should increase by one.</p>
13 <p>In our case, we indicated that after each iteration of the loop, the variable i should increase by one.</p>
14 for (var i = 0; i &lt; 5;<b>i = i + 1</b>) { }</li>
14 for (var i = 0; i &lt; 5;<b>i = i + 1</b>) { }</li>
15 </ol><p>We have analyzed how the for loop is structured in theory. Now let’s write a program for printing all pages. The number of pages is stored in the variable totalPages.</p>
15 </ol><p>We have analyzed how the for loop is structured in theory. Now let’s write a program for printing all pages. The number of pages is stored in the variable totalPages.</p>
16 <p>By the way, it is possible to create a counter in a loop without var. This possibility exists for backwards compatibility and came to us from the legacy versions of the language. It worked then, although such code was considered illiterate. But in the new version of JavaScript (ES6), this entry will not work. In the new version, strict mode is enabled by default, which forbids creating variables without var, including the loop counter. Do not forget about var and write the code correctly right away.</p>
16 <p>By the way, it is possible to create a counter in a loop without var. This possibility exists for backwards compatibility and came to us from the legacy versions of the language. It worked then, although such code was considered illiterate. But in the new version of JavaScript (ES6), this entry will not work. In the new version, strict mode is enabled by default, which forbids creating variables without var, including the loop counter. Do not forget about var and write the code correctly right away.</p>