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";

Task
Run the program, look at the result of its work
C++
1
#include<iostream>    
2
using namespace std;    
3
main() {      
4
	int i;      
5
	cout << "Start\n";      
6
	i=1;      
7
	while(i<=10)      
8
	  {      
9
	  	cout << i <<" ";      
10
	  	i++;   // An operator that affects the change of a variable in a condition 
11
	  }      
12
	cout << "\nFinish";      
13
}      
Your last submission is saved in the editor window.
     

Results:

All results: