Python do-while loop with example

In programming, we use a loop for executing the block of statements repeatedly until the loop control statement becomes false. A do-while loop is basically somewhat similar to a while loop but with a basic difference.

The do-while loop is a type of post-tested loop. In the do-while loop, the loop control statement is at the end of the loop, whereas in while loop the loop control statement is at the start of the loop. Because of this structure, this kind of loop guarantees that the statements inside the loop body will be executed at least once even if the loop control statement is false.

Syntax of do-while loop is given below:

do {

#block of statements

}

while (loop control statement)

 

Sadly in Python, we don’t have any kind of do while loop. Sometimes we want to use the do-while loop because of it’s post-testing property, but we can emulate it. We can emulate the do-while loop by using a while loop that is explicitly available in Python.

 

Let us consider a do while loop example from C:

Case 1 : The loop-control statement is true.

#include<stdio.h>
#include<conio.h>
main ()
{
int n=5;
do{ 	//loop body
  printf("%d\n", n);
  n = n + 1;
} while(n <= 9); 	//loop-control statement
getch();
}

Output :

5
6
7
8
9

Explanation :- Since the loop-control statement is true the output is 5 6 7 8 9. But let’s see what will happen if we take the loop-control statement as false.

Case 2 : The loop-control statement is false.

#include<stdio.h>
#include<conio.h>
main ()
{
int n=5;
do{ 	//loop body
  printf("%d\n", n);
  n = n + 1;
} while(n <= 1); 	//loop-control statement
getch();
}

Output :

5

Explanation :- Here since the loop-control statement is false, the statements inside the loop body have executed just once as per the structure of the do-while loop.

Let us emulate the same code in Python:

Case 1 : The loop-control statement is true.

n= 5
while True:    #loop body
    print(n)
    n = n + 1
    if(n > 9): #if condition
        break

Output :

5
6
7
8
9

Explanation :- Here in this code, inside the loop the if condition is checking the value of n. While n<=9, the statements above the if condition inside the loop is executing normally.

But when the value of n becomes n>9, the if condition becomes true and statement inside the if block executes that is to exit from the loop body. So we came up with the similar output as we got in the previous C program in case 1.

Also, read: While loop in Python with example

Case 2 : The loop-control statement is false.

n= 5
while True:    #loop body
    print(n)
    n = n + 1
    if(n <=1): #if condition
        continue
    else:
        break

Output :

5

Explanation :- Here inside the while loop, we are printing the value of n and then we have incremented the n value. But in this case, the if condition is false so the statement inside the else block is now being executed that to exit from the loop body. And again we came up with a similar output as we got in the previous C program in case 2.

Leave a Reply

Your email address will not be published. Required fields are marked *