Introduction to Do While Loop in Python
In almost all computer languages we have a do-while loop. A do-while loop is one of among the control flow statement which executes the block of code at least once and executes block repeatedly depending on the condition given at the end in while statement. But python doesn’t have the do-while loop, though python doesn’t have it explicitly we can emulate it using break/continue/if statement. Python does not require to do while loop because it can be done with just a while loop and also there is no proper way to define in the python syntax like the statement: indented block this type of indentation pattern was never reached by do while loop in python compound statement.
Flowchart of Do-While Loop
In most of the computer programming languages, unlike while loops which test the loop condition at the top of the loop, the do-while loop plays a role of control flow statement similar to while loop which executes the block once and repeats the execution of block based on the condition given in the while loop the end.
Syntax of do-while
} while (condition);
In this syntax, the condition appears at the end of the loop, so the statements in the loop execute at least once before the condition is checked. In a while loop, we check it at the beginning of the loop. If the condition is true it jumps to do, and the statements in the loop are again executed. This is repeated until the condition is false. While loop in python has the syntax of the form:
Syntax of while
The above statements can be a single statement or block of statements. The expression is a condition and if the condition is true then it is any non-true value.
So as we are used to do while loops in all basic languages and we want it in python. The do-while loop is important because it executes at least once before the condition is checked. Though python cannot do it explicitly, we can do it in the following way
Syntax while if
# statement (s)
If not condition:
In python, while loop repeatedly executes the statements in the loop if the condition is true. In a while loop, the test condition is checked first and if it is true then the block of statements inside the loop is executed. After one iteration again the test condition is checked and this process is continued until the test condition evaluates to false. The do-while loop which is not in python it can be done by the above syntax using while loop with break/if /continue statements. In this, if the condition is true then while statements are executed if not true another condition is checked by if loop and the statements in it are executed. The break statement is used to bring the program control out of the if loop. In other words, the break is used to abort the current execution of the program.
4.8 (4,289 ratings)
i = 1
i = i + 1
if(i > 5):
In the above example we can see first the statement i=1 is initialized and then we are checking it with a while loop. If the value of the i =1 then we are printing the current value of i. Then the current i value is added with 1 to get the new value of i. This block is repeated till the i value reaches to 5 as this condition (i > 5) is checked in the if loop and this loop stops after i =5 as there is a break statement, which if loop stops.
In the python body of the while, the loop is determined through indentation. As there is no proper indentation for specifying do while loop in python, therefore there is no do-while loop in python but it is done with while loop itself. The body of the while loop starts with indentation and as soon as the unindented line is found then that is marked as the end of the loop.
Conclusion – Do While Loop in Python
As we are very used to do while loop in all other languages as it will first execute statements and then check for the conditions. But in python also we want it to be done, but it cannot as it will not fit the indentation pattern of the python other statements. So in Python, it can be done with a while statement using the break/continue/if statements if the while condition is not satisfied, which is similar to do while loop as in other languages. The while loop in python first checks for condition and then the block is executed if the condition is true. The block is executed repeatedly until the condition is evaluated to false. Thus in python, we can use while loop with if/break/continue statements which are indented but if we use do-while then it does not fit the rule of indentation. Therefore we cannot use the do-while loop in python.
This is a guide to Do while loop in python. Here we discuss the flowchart of Do While Loop in Python with the syntax and example. You may also look at the following article to learn more-