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:
- 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, 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:
print("Start")
i = 1
while i <= 10:
print(i, end=" ")
i += 1 # operator that affects the change of a variable,
# standing in the condition
print() # move the cursor to a new line
print("Finish")
In this program, we assign a value to the variable i = 1
- the starting point.
Further, as long as we have the value of the i
variable 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 variable i
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
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. print()
and print("Finish")
.