Loading…
Everything will be ready in few seconds
- Theory
- Theory
- Comments
Learning the for loop
Excellent! The first part of the driver is executed. The second step: printing all pages of the document.
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:
for (var i = 0; i < 5; i = i + 1) {
// repeated commands or the “loop body”
}
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.
Loop control code must be in parentheses. It consists of three parts, separated by ;
. The meaning of each part is this:
The first part is the preparatory one. Commands are started here once 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.
In the example below, we created the counter variable
i
and set to it the initial value0
. The variable will be equal to this value when the loop begins to work.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 calledi
(from the word index), but it can have any other name. For example, if a loop counts days, the counter variable may be calledday
, and if it counts pages of the document, then it can be calledpage
.for (var i = 0; i < 5; i = i + 1) { }
The second part is a test. It contains a condition and starts before 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.In the example, we indicated that the loop should work until the variable
i
becomes less than5
.for (var i = 0; i < 5; i = i + 1) { }
The third part is an additional one, although scientifically speaking it is called the “pattern of change”. The code for the third part starts after 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.
In our case, we indicated that after each iteration of the loop, the variable
i
should increase by one.for (var i = 0; i < 5; i = i + 1) { }
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
.
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.
- script.js
Thanks! We’ll fix everything at once!
The code has changed, click “Run” or turn autorun on.
Result
Let’s log the pages of the document in the loop.
- Under the variable declaration, add an empty loop to the code
for (;;) { }
. - In the preparatory part of the loop, create a counter variable:
var page = 0
, - in the checking part, set the condition for the execution of the loop:
page < totalPages
, - in the addition part, describe the change in the counter:
page = page + 1
. - And in the body of the loop, add the page print command:
muffin.print(page)
.
Comments