How to Emulate a Do-while loop in Python?
Last Updated :
25 Apr, 2024
We have given a list of strings and we need to emulate the list of strings using the Do-while loop and print the result. In this article, we will take a list of strings and then emulate it using a Do-while loop.
Do while loop is a type of control looping statement that can run any statement until the condition statement becomes false specified in the loop. In do while loop the statement runs at least once no matter whether the condition is false or true.
Example:
Input : ["geeksforgeeks", "C++", "Java", "Python"]
Output: geeksforgeeks
C++
Java
Loop has exited.
Explanation : Here, we take a string of list as input and emulate the list using Do-while loop.
How to Emulate a Do-while loop in Python?
Below, are the methods to Emulate a Do-while loop in Python.
- Using a Conditional Break
- Using a Flag Variable
Emulate a Do-while loop Using a Conditional Break
In this example, the below code creates a list of strings and enters an infinite loop. Within the loop, it prints each string from the list once. It then breaks out of the loop and prints a message confirming the loop has exited.
Python3
# Example list of strings
strings_list = ["geeksforgeeks", "C++", "Java", "Python", "C", "MachineLearning"]
# Start the loop
while True:
# Code block to execute at least once
print("Strings in the list:")
for string in strings_list:
print(string)
# Exit the loop
break
# Code block outside the loop
print("Loop has exited.")
OutputStrings in the list:
geeksforgeeks
C++
Java
Python
C
MachineLearning
Loop has exited.
Emulate a Do-while loop Using a Flag Variable
In this example, below code starts by printing a message indicating that it's going to display the strings in the list. It then employs a while loop with a flag variable to iterate over each string in the list and print it. The loop continues until the index exceeds the length of the list, at which point the flag is set to False, and the loop terminates.
Python3
# Example list of strings
strings_list = ["geeksforgeeks", "C++", "Java", "Python", "C", "MachineLearning"]
print("Strings in the list:")
# Using a while loop with a flag variable
flag = True
index = 0
while flag:
# Execute the code block at least once
current_string = strings_list[index]
print(current_string)
# Increment the index
index += 1
# Set the flag to False if the loop should terminate
if index >= len(strings_list):
flag = False
# Code block outside the loop
print("Loop has exited.")
OutputStrings in the list:
geeksforgeeks
C++
Java
Python
C
MachineLearning
Loop has exited.
Similar Reads
How to Parallelize a While loop in Python? Parallelizing a while loop in Python involves distributing the iterations of a loop across multiple processing units such as the CPU cores or computing nodes to execute them concurrently. This can significantly reduce the overall execution time of the loop, especially for tasks that are CPU-bound or
2 min read
How to Kill a While Loop with a Keystroke in Python? While loops are used to repeatedly execute a block of code until a condition is met. But what if you want the user to stop the loop manually, say by pressing a key? Then you can do so by pressing a key. This article covers three simple ways to break a while loop using a keystroke:Using KeyboardInter
2 min read
Add items to List While Iterating - Python Python has many ways to append to a list while iterating. List comprehension is a compact and efficient way to append elements while iterating. We can use it to build a new list from an existing one or generate new values based on a condition.Pythona = [1, 2, 3] a += [i + 4 for i in range(3)] print(
2 min read
Loop Through a List using While Loop in Python In Python, the while loop is a versatile construct that allows you to repeatedly execute a block of code as long as a specified condition is true. When it comes to looping through a list, the while loop can be a handy alternative to the more commonly used for loop. In this article, we'll explore fou
3 min read
How to Check End of For Loop in Python In Python, we often use loops to iterate over a collection like list or string. But sometimes, we may want to know when the loop has reached its end. There are different ways to check for the end of a for loop. Let's go through some simple methods to do this.Using else with For Loop (Most Efficient)
2 min read
How to start a for loop at 1 - Python Python for loops range typically starts at 0. However, there are cases where starting the loop at 1 is more intuitive or necessary such as when dealing with 1-based indexing in mathematics or user-facing applications.Let's see some methods to start a for loop at 1 in Python.Using range() with a Star
2 min read