(C++) Loop statement with condition - while


Studying the for loop, we said that if you know the number of repetitions of any actions, you can reduce the program using the for loop operator. But what if the number of repetitions is not known. And this happens quite often.
For instance:
remembering the conversion of a number from the decimal number system to any other, we need to divide the number (and then the division result) by the base of the number system until we get zero in the answer. How many times we will share is not known. And there are a lot of programs that implement such an algorithm.
How is this implemented?
For such a case in programming, there is a loop operator with a condition.
In the C++ programming language, the loop statement with a condition begins with the word while and has the following construction.
while (<condition>){
  loop body
}
As we already know:
- a condition is an expression that can result in either true or false (as in a conditional statement)
- the body of the cycle is the commands that need to be repeated
- curly braces can be omitted if the body of the cycle consists of only one operator

How the operator works:
1. First, the program calculates the condition in brackets after the word while. If the condition is satisfied (true), then the program executes the operator (s) contained in the body of the loop.
2. As in the for loop, if the loop body contains only one statement, then the curly braces 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 re-checks it.
4. These actions (checking the condition - executing the loop body) are repeated until the condition becomes false.
5. If, upon the first execution of this operator, the condition is not immediately satisfied (false), then the program will never execute the loop body.

Analyzing the operation of this operator, it should become clear that in the body of the loop must be an operator that affects the condition.
For example, a loop may increase the value of a variable used in a condition.

An example of a program that we will name "Silent count"
Let's make the program instead of us count, for example, to 10.
The program should display the phrase, "Start" and "Finish", and between these actions display the numbers that are counted.
For example,
Start
1 2 3 4 5 6 7 8 9 10
Finish
A program using a while loop will look like this.
#include<iostream>
using namespace std;
main()
{
	int i;
	cout << "Start\n";
	i=1;
	while(i<=10)
	  {
	  	cout << i <<" ";
	  	i++;  // An operator that affects the change of a variable in a condition
	  }
	cout << "\nFinish";
}
In this program, we assign the value of the variable i = 1 - the origin
Further, while we have the value of the variable i not more (i.e. 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 increases, that is, at some point, the condition i <= 10 becomes false. This will happen when i becomes 11. In this case, the body of the loop will no longer be executed, and the program will execute the next statement after the loop, i.e. cout << "\nFinish";

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


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.
As a result, after we cut off all the numbers, in the counter we get the number of digits in the number.
In another way, the algorithm can be formulated as follows:
While the number is not equal to zero, reduce it by 10 times and increase the counter by 1.
число (n) счетчик
123 0
12 1
1 2
0 3
The program will look as follows.
#include<iostream>
using namespace std;
main()
{
int n, count;
cin >> n;
count = 0;
while (n != 0) 
  {
  count ++;
  n = n / 10;
  }
cout << "Number - " <<n<< " contains " << count << " digits";  
}
This program needs to know by heart, because on its basis, many other problems are solved related to the calculation of numbers by digits.

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