Module: Loop statement with condition - while


Problem

1/21

The construction of a loop statement with a condition (while)

Theory Click to read/hide

When studying the for loop, we said that if the number of repetitions of any actions is known, then you can shorten the program using the for loop operator. But what if the number of repetitions is not known? And this happens quite often.
For example, let's remember the conversion of a number from a decimal number system to any other: we need to divide the number (and then the result of division) by the base of the number system until we get zero in the answer. How many times we will share is unknown. And there are quite a lot of programs that implement such an algorithm. 
How is this implemented?
For such cases in programming, there is a loop operator with a condition. 
In the Pascal programming language, a conditional loop statement begins with the word while and has the following structure.

while <condition> do begin
  loop body
end
As we already know:
- a condition is an expression, the result of which can be either true or false (as in a conditional statement)
- the loop body is the commands that need to be repeated
- begin and end can be omitted if the loop body consists of only one operator

How the operator works:
1. First, the program evaluates the condition after the word while. If the condition is met (true), then the program executes the statement(s) contained in the loop body.
2. As in the for loop, if the loop body contains only one statement, then the words begin and end that highlight the loop body can be omitted.
3. After the execution of the loop body is completed, the program returns to check the condition and checks it again.
4. These actions (checking the condition - executing the loop body) are repeated until the condition becomes false.
5. If at the first execution of this operator, the condition is not immediately satisfied (false), then the program will never execute the loop body.

Analyzing the work of this operator, it should become clear that the loop body must contain an operator that affects the condition.
For example, a loop can increment the value of a variable used in a condition.

An example of a program we'll call "Silent count"
Let's make the program count instead of us, for example, up to 10.
The program should output the phrase, "Start" and "Finish", and between these actions display the numbers that are being calculated.
Like this, 
Start
1 2 3 4 5 6 7 8 9 10
Finish
A program using a while loop would look like this.
var i: integer;
begin
    writeln('Start');
    i := 1;
    while i <= 10 do begin
        write(i, ' ');
        i += 1; //Operator that affects the change of the variable in the condition    
    end;
    writeln();
    writeln('Finish');
end.
In this program, we assign a value to the variable i := 1 - the origin
Further, as long as we have the value of the variable i is not greater than (that is, less than or equal to) the value we need, we 
 1 - display the value of the variable i 
 2 - increase the value of the variable i by 1 - this operator affects the value of the condition in brackets. The variable i is incremented, i.e. at some point the condition i<=10 will become false. This will happen when i becomes equal to 11. In this case, the loop body will no longer be executed, and the program will execute the next statement after the loop, i.e. writeln();
writeln('Finish');

Problem

Run the program, look at the result of its work