Updated March 17, 2023
Introduction to Do While Loop in Python
The Do While Loop conditional statement is used for an exit level control flow of code implementation that ensures the code block is executed at least once before the control reaches the while condition. In Python, there is no dedicated do while conditional loop statement, and so this function is achieved by creating a logical code from the while loop, if statement, break and continue conditional statements. When the logic of the program is done correctly, depending on the requirement provided, the Do While loop can be imitated perfectly.
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
do
{
Statement(s)
} 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
while expression:
statement (s)
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.
We are used to doing 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
while True:
# statement (s)
If not condition:
break;
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.
Example
i = 1
while True:
print(i)
i = i + 1
if(i > 5):
break
Output:
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, it will first execute statements and then check for the conditions. In python, we also 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 a while loop with if/break/continue statements that are indented, but if we use do-while, it does not fit the indentation rule. Therefore we cannot use the do-while loop in python.
Recommended Articles
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-