Loop statement with condition - while


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');

Let's try to write a program to solve the following problem:

You must enter a number (let it be less than 3,000,000) and determine the number of digits in it.

Solution idea


Let's start a counter of digits of a number. Initially, the counter is 0. We just need to sequentially cut off the last digit from the number (this can be done by reducing the number by 10 times, using integer division by 10), and each time we need to increase the counter by 1. 
As a result, after we cut off all the digits, in the counter we will get the number of digits in the number.
In another way, the algorithm can be formulated as follows:
UNTIL THE NUMBER IS NOT ZERO, DECREASE IT 10 times and INCREASE THE COUNTER BY 1.
number (n) counter
123 0
12 1
1 2
0 3
The program will look like this.
var n, count: integer;
begin
    read(n);
    count := 0;
    while n <> 0 to begin
        count += 1;
        n := n div 10;
    end;
    writeln('Number - ', n, ' contains ', count, ' digits');
end.
You need to know this program by heart, because. on its basis, many other problems related to the calculation of numbers by digits are solved.

Task

The input of the program is the data stream — a sequence of integers that ends in zero (zero is not included in the sequence). You need to find the sum of the elements of this sequence.
 
Solving algorithm
 sum=0
 input x // enter the first number
 while x != 0 // the input end sign is set in the condition,
 nc // that is, until you enter zero
   sum = sum + x // you can do something with the original number.
                  // You can add a number check for some condition, etc.
   input x // enter the next number
 kts
 print sum //result output