Module: Loop statement with condition - while


Problem

1/20

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

Theory Click to read/hide

A loop with a condition. while
statement 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 unknown? And this happens quite often.
 
Example
Remembering the translation 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 can we implement the algorithm if we don't know the exact number of iterations?

For such cases in programming, there is a loop statement with a condition. 
In the Python programming language, a conditional loop statement begins with the word while and has the following structure.
while (<condition>) 
{
  loop body
}

As we already know:
- condition is an expression that can evaluate to either true or false (as in a conditional statement);
- loop body are commands to be repeated, they are indented.

How does the operator work?
1. First, the program evaluates the condition in brackets after the word while. If the condition is met (true), then the program executes the statement(s) contained in the loop body.
2. After the execution of the loop body is completed, the program returns to check the condition and checks it again.
3. These actions (checking the condition - executing the loop body) are repeated until the condition becomes false.
4. If the condition is not immediately met (false) during the first execution of this statement, 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, in a loop, the value of a variable used in a condition may be incremented.

An example of a program that we will call "Silent count".
Let's make the program count instead of us, for example, up to 10.
The program should output the phrases "Start" and "Finish", and between these actions display the numbers that are being calculated.

For example, like this:
Start
1 2 3 4 5 6 7 8 9 10
Finish

A program using a while loop would look like this: using System; class Program {     static void Main() {         Console.WriteLine("Start");         int i = 1;         while (i <= 10) {             Console.WriteLine(i);             i++;         }         Console.WriteLine("Finish");     } } In this program, we assign a value to the variable i = 1 - the starting point.
Further, as long as the value of the variable i is not greater (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 i  variable by 1 - this operator affects the value of the condition in brackets. The variable i  is incremented, meaning that at some point the condition i <= 10 will become false. This will happen when i is 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, that is,  Console.WriteLine("Finish").

Problem

Run the program, analyze the result of its work.