In this article, we learn how to simulate do... while loop statements in Python.
do... while loop statement
Many programming languages, such as JavaScript, Java, C# and so on, provide do... While loop statements. The difference between this loop statement and the while loop is that it iterates at least once. Because it checks the loop condition at the end of each iteration and continues the iteration when the loop condition is True.
The following is the pseudo code to implement the do... while loop in Python:
do # code block while condition
Python currently does not support this do... While loop syntax. However, we can simulate this statement by using a while loop plus a break statement.
First, we can specify a condition true for the while loop, for example:
while True: # code block
This method can be used to implement the first execution of the code block. In fact, this is an infinite loop. We need to define the conditions for exiting the loop:
while True: # code block # break out of the loop if condition: break
In the above syntax, the code block will be executed at least once, and the condition will be checked after each iteration.
do... while loop simulation example
If we need to develop a number guessing game, the logic is as follows:
- First, a random number between 0 and 10 is generated.
- Then, repeatedly prompt the user to enter a number. If the entered number is less than or greater than the generated random number, the corresponding prompt is displayed. If the number entered is equal to a random number, end the loop.
The following code uses the while loop to realize the number guessing game:
from random import randint # determine the range MIN = 0 MAX = 10 # generate a secret number secret_number = randint(MIN, MAX) # initialize the attempt attempt = 0 # The first attempt input_number = int(input(f'Enter a number between {MIN} and {MAX}:')) attempt += 1 if input_number > secret_number: print('It should be smaller.') elif input_number < secret_number: print('It should be bigger.') else: print(f'Bingo! {attempt} attempt(s)') # From the second attempt while input_number != secret_number: input_number = int(input(f'Enter a number between {MIN} and {MAX}:')) attempt += 1 if input_number > secret_number: print('It should be smaller.') elif input_number < secret_number: print('It should be bigger.') else: print(f'Bingo! {attempt} attempt(s)')
Run the program and enter some numbers:
Enter a number between 0 and 10:5 It should be bigger. Enter a number between 0 and 10:7 It should be bigger. Enter a number between 0 and 10:8 Bingo! 3 attempt(s)
Since the while loop performs condition checking at the beginning of the iteration, it is necessary to repeatedly write the code that prompts the user to enter and compare numbers twice, once before the loop and once inside the loop.
In order to avoid this kind of repeated code, we can use the simulated do while loop to realize the above functions:
from random import randint # determine the range MIN = 0 MAX = 10 # generate a secret number secret_number = randint(MIN, MAX) # initialize the attempt attempt = 0 while True: attempt += 1 input_number = int(input(f'Enter a number between {MIN} and {MAX}:')) if input_number > secret_number: print('It should be smaller.') elif input_number < secret_number: print('It should be bigger.') else: print(f'Bingo! {attempt} attempt(s)') break
The above code is modified as follows:
- First, the code before the while loop is deleted.
- Secondly, the stop loop break statement is added to stop the loop when the input number is equal to a random number.
summary
- Python does not support do... while loop statements.
- Python can use a while loop plus a break statement to simulate a do... While loop.