Almost all programming languages offer some features to repeat a certain task. These features are known as loops. For example, if you want to print student names out of the list, you can use a loop instead of using a print statement every time for each student. In this article, we are going to discuss the do-while loop in python. 

A do while loop in python will run or execute a code while the condition remains true. It has a simple syntax.

Syntax

#Syntax of do while loop 
do {

     loop

} while (condition);

The flowchart of do while loop in python

To get the things better, you can follow the flowchart of the loop.

Here, you can see that the loop executes the code while the condition is true and it will stop execution when the condition turns False. If the condition doesn’t turn false, the loop will be a never-ending or infinite loop.

This type of loop is called do while loop in other programming language. But in Python it is generally termed as While loop.

A simple Python do while loop

Well, we are now good with some definitions and working of the loop along with its flowchart. Now, let’s take a simple do while loop example to understand things better.

#Simple example of do while loop
i = 1

while True:
    print(i)
    i = i+1
    if (i >4):
        break
1
2
3
4

Even though python doesn’t explicitly have the do while loop, you can easily emulate as shown above.

Keep the things clear about loop termination –

  • When it encounter a condition which is true/false depending on loop.
  • When we use the break statement.

Let’s put it all together

Till now, we have seen the working of do-while loops in python. Now, let’s take up a problem statement, and using the do-while loop we can code the scenario. Interesting? let’s roll!!!.

We are going create a magic number game –

  • The number should be automatically generated.
  • User will get 3 attempts to guess the number.
  • If the guessed number is right, then message will be displayed.
import random

magic_num = random.randint(0,10)

attempts= 0

while attempts < 3:
    print("Guess a number between 1 and 10: ")
    guess = int(input())
    attempts = attempts +1
    if guess == magic_num:
        break
print('YAY, You have guessed a magic number!')
Guess a number between 1 and 10: 
5
Guess a number between 1 and 10: 
9
Guess a number between 1 and 10: 
1
YAY, You have guessed a magic number!

Yes, we have guessed the magic number in out third attempt. Not bad!.

In this code, we have imported the random module to generate a random number between 1 – 10. Then we have defined a variable to store the random number. Then we have defined a do-while loop, which will count the user attempts. If the attempts go over 3 then the loop will not execute.

Key points

  • Python doesn’t explicitly have a do while loop, but we can emulate it easily.
  • The loop will execute the code and then checks the condition.
  • It will execute if the condition is true.
  • Loop will be terminated if the condition turns false.
  • A simple syntax and easily applicable loop like others.

Ending note

You can see Do while loops easily and directly in other programming languages. But in python, you can use while loop directly, which will get your job done. You can follow the syntax, flowchart, and examples to get things better and easier at your end.

That’s all for now, Happy Python!!!

More read: Stack overflow

Categorized in:

Tagged in:

,