A for loop is a means of stepping through repeated actions. Let's take a closer look at how it works.
Typically, parts of a for loop perform the following steps:
1. Set initial value.
2. Setting the step with which the loop variable will change
3. Setting the end value.
3. Perform loop actions.
4. Update the value(s) used in the test.
and then steps 2-4 are repeated until the condition is met. As soon as the condition becomes false, the loop terminates and the statement following the for loop statement is executed.
Let us return to the general form of the loop statement and analyze in more detail all the parts
for *set initial value to variable* to/downto *end value* do begin
/*one statement or block of statements - loop body*/;
end;
Setting a variable to an initial value
is responsible for setting the initial value of the cycle variable (counter), is NOT highlighted with brackets or something else
For example :
i := 0; //the cycle variable i is assigned the initial value equal to zero. With such a record,
//variable i must be declared before the loop
to/downto
This is the step at which the variable used in the loop will change. If we write to, then each iteration the value of the variable will increase by 1, if downto - decrease by 1
End value
is the last value at which the body of the loop will still be executed. For example, if we set the last value to 100, then at 100 our loop will still be executed, and at 101 it won’t.
Let's practice writing the title of the for loop