Skip to main content
Python Do-While loop

Python do while with Example

Python doesn’t have do-while loop. However, you can achieve a similar effect by using a while loop and a break statement.

This Example helps to create a program that helps to execute ‘do while loop’ with an example. There are no of loops available to iterate over elements/items but do-while does not have.

Whats Do-While Loop

A do-while loop is a loop that executes its body at least once, and then continues to repeat the loop until a certain condition is met.

The sample Structure:

while True:
    # Code to be executed
    if condition:
        break

The break statement is used to exit the loop when the condition is met.

What is loop

A loop statement allows us to execute a statement or group of statements multiple times. The loop helps to execute a block of code several times.

Python Loops Types

Python programming has two types of loops.

  • for loop
  • while loop

General Python Loop Example

The other programming has do-while loop. The do-while syntax:

do {
//statement
} while (condition);

You can achieve the same thing by following the python code:-

no_loop = 1
while True:
  print(no_loop)
  no_loop = no_loop + 1
  if(no_loop > 5):
    break

The above code will output the numbers 1 through 5, demonstrating that the loop executes at least once and continues to repeat until the condition np_loop == 5 is met.

Output:

1
2
3
4
5

You can also checkout other python tutorials:

Leave a Reply

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