How Can You Emulate Do-While Loops in Python?

How Can You Emulate Do-While Loops in Python? https://realpython.com/python-do-while/ 2022-06-20T14:00:00+00:00 In this tutorial, you'll learn how to emulate do-while loops in Python. The most common technique to do this is to create an infinite while loop with a conditional statement that controls the loop and jumps out of it using a break statement.

If you came to Python from a language like C, C++, Java, or JavaScript, then you may be missing their do-while loop construct. A do-while loop is a common control flow statement that executes its code block at least once, regardless of whether the loop condition is true or false. This behavior relies on the fact that the loop condition is evaluated at the end of each iteration. So, the first iteration always runs.

One of the most common use cases for this type of loop is accepting and processing the user’s input. Consider the following example written in C:

#include <stdio.h> int main() { int number;  do {  printf("Enter a positive number: "); scanf("%d", &number); printf("%d\n", number);  } while (number > 0);  return 0; } 

This small program runs a dowhile loop that asks the user to enter a positive number. The input is then stored in number and printed to the screen. The loop keeps running these operations until the user enters a non-positive number.

If you compile and run this program, then you’ll get the following behavior:

Enter a positive number: 1 1 Enter a positive number: 4 4 Enter a positive number: -1 -1 

The loop condition, number > 0, is evaluated at the end of the loop, which guarantees that the loop’s body will run at least once. This characteristic distinguishes do-while loops from regular while loops, which evaluate the loop condition at the beginning. In a while loop, there’s no guarantee of running the loop’s body. If the loop condition is false from the start, then the body won’t run at all.

One reason for having a do-while loop construct is efficiency. For example, if the loop condition implies costly operations and the loop must run n times (n ≥ 1), then the condition will run n times in a do-while loop. In contrast, a regular while loop will run the costly condition n + 1 times.

Python doesn’t have a do-while loop construct. Why? Apparently, the core developers never found a good syntax for this type of loop. Probably, that’s the reason why Guido van Rossum rejected PEP 315, which was an attempt to add do-while loops to the language. Some core developers would prefer to have a do-while loop and are looking to revive the discussion around this topic.

In the meantime, you’ll explore the alternatives available in Python. In short, how can you emulate do-while loops in Python? In this tutorial, you’ll learn how you can create loops with while that behave like do-while loops.

In Short: Use a while Loop and the break Statement

The most common technique to emulate a do-while loop in Python is to use an infinite while loop with a break statement wrapped in an if statement that checks a given condition and breaks the iteration if that condition becomes true:

while True: # Do some processing... # Update the condition... if condition: break 

Read the full article at https://realpython.com/python-do-while/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]