cycles. Loop with parameter (for)


Imagine a situation where we need to display the same word on the screen, let's say the word "HELLO", 10 times. What should we do?
You can take and write the command 10 times writeln('HELLO');

But what if you need not 10 times, but 20, 30, 40 times ?, and if 200 times? In this case, copying will take a very long time. And if it is necessary that the user can choose how many times to display information on the screen? 

To cope with this task, we can use a special construction called LOOP

A loop is an algorithmic construction in which a certain sequence of commands is repeated several times.


In the Pascal programming language, there are two kinds of loops: a loop with a variable (for) and a loop with a condition (while)

Let's start our acquaintance with cycles from the first type.

LOOP WITH A VARIABLE OR WITH A KNOWN NUMBER OF STEPS (FOR)

It often happens that we know the number of repetitions of some actions, or we can calculate the number of repetitions using the data known to us. Some programming languages ​​have a command that in Russian sounds like REPEAT (number of times) - that is, we can specify the exact number of repetitions. 

It is interesting to see how this cycle works at the machine level:
1. a certain memory cell is allocated in memory and the number of repetitions is written to it,
2. when the program executes the loop body once, the contents of this cell (counter) is decremented by one.
3. loop execution ends when this cell is zero.

In the programming language Pascal, there is no such construction, but there is a for construction.  

The general form of the for loop statement is as follows:
for *start value* to *end value* do
begin
      /*one statement or block of statements - loop body*/;
end;
This construction requires that we
1. explicitly allocated a memory cell, which will be a counter, and set its initial value
2. Specify how the value in this cell will change.
3. a condition was written under which the loop body will be executed

In the practical part, we will try to display the word Hello 10 times. In the following tasks, we will analyze this construction in more detail.

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

The head of a for loop in Pascal consists of several parts:
1) the word for
2) what variable and what value are we assigning. In this program, this is b := 1, i.e. we assign the value 1 to b.
3) the word to or downto, which indicates the step with which the loop is executed. If we write to, then the variable will increase by 1 every iteration, if downto, then decrease by 1.
4) final value of the variable
5) word do
6) the word  begin  (it can be omitted if there is only one line in the loop body, otherwise it must be written. If you do not write begin, then only the first line will be executed from the entire body of the loop). Let's not forget after the loop body, if we wrote the word begin, write the word end; !

All programs with a for loop that we have written so far cannot be called universal. Because we set the number of repetitions of the loop body ourselves. 
But what if the number of repetitions depends on some other value? For example, the user himself wants to set the number of repetitions of the cycle.
What to do in this case?
Everything is very simple. Instead of numeric start and end values, we can use any variables that can be calculated or set by the user.

For example, we need to display the squares of numbers from 1 to n, where the value of the variable n is entered from the keyboard by the user.
The program will look like this:
 
var i, n :integer; // i – loop variable, n - the maximum number for which we calculate the square
begin
 read(n); // input n from the keyboard
 for i := 1 to n do // loop: for all i from 1 to n - variable i will sequentially take values ​​from 1 to n
  writeln('Kvadrat chisla ', i, ' raven ', i * i); // Outputting the square of a number in a specific format and moving to a new line
end.

When entering the loop, the statement i := 1 is executed, and then the variable i is incremented by one (to) with each step. The loop is executed while the condition i <= n is true. In the body of the loop, the only output statement prints the number itself and its square on the screen according to the specified format.
For squaring or other low exponents, it's better to use multiplication.

Run the program and see the result of its work with different values ​​of the variable n.