freeCodeCamp
freecodecamp.org › news › python-do-while-loop-example
Python Do While – Loop Example
August 31, 2021 - The while loop, on the other hand, doesn't run at least once and may in fact never run. It runs when and only when the condition is met. So, let's say we have an example where we want a line of code to run at least once. secret_word = "python" counter = 0 while True: word = input("Enter the secret word: ").lower() counter = counter + 1 if word == secret_word: break if word != secret_word and counter > 7: break
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
With the break statement we can stop the loop even if the while condition is true: ... Note: The else block will NOT be executed if the loop is stopped by a break statement. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Videos
What is a while loop in Python?
A while loop in Python is a control flow statement that allows you to repeatedly execute a block of code as long as a specified condition remains true. It's particularly useful when you don't know in advance how many times you need to repeat the action, such as waiting for user input until they provide a correct response.
emitechlogic.com
emitechlogic.com › blog › the while loops in python: an ultimate guide with examples
The While Loops in Python: An Ultimate Guide with Examples - ...
When should I use a while loop instead of a for loop?
You should use a while loop when the number of iterations is not known before the loop starts. While loops are best for scenarios where you need to keep checking a condition until it becomes false. On the other hand, a for loop is more appropriate when you know the exact number of iterations in advance, such as iterating over a list of items.
emitechlogic.com
emitechlogic.com › blog › the while loops in python: an ultimate guide with examples
The While Loops in Python: An Ultimate Guide with Examples - ...
How can I avoid infinite loops when using while loops?
To avoid infinite loops, you need to ensure that the condition controlling the while loop will eventually become false. This typically involves updating the variables or conditions being checked within the loop so that the loop can terminate properly. Forgetting to do this is a common cause of infinite loops.
emitechlogic.com
emitechlogic.com › blog › the while loops in python: an ultimate guide with examples
The While Loops in Python: An Ultimate Guide with Examples - ...
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › do while loop in python
Do While Loop in Python | Emulate Do While Loop in Python(Example)
March 17, 2023 - 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.
Call +917738666252
Address Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Scaler
scaler.com › home › topics › python do while loop
Python Do While Loop - Scaler Topics
December 1, 2023 - Since Python does not explicitly provide its do-while Loop (like C and C++ do), we will have to make a workaround using the existing loops in Python, for-loop, and while-loop. But first, let us look at the control flow for the do-while Loop. A do-while loop in a logic flow diagram (or a flow ...
EmiTechLogic
emitechlogic.com › blog › the while loops in python: an ultimate guide with examples
The While Loops in Python: An Ultimate Guide with Examples - EmiTechLogic
May 4, 2025 - Comparison of while loops and for loops in Python. The diagram illustrates examples of each loop type and provides guidance on when to use while loops versus for loops based on the context and requirements of the task. The primary difference between while loops and for loops is how they control the flow of iteration. While Loop: A while loop continues to execute as long as its condition remains true. The loop does not have a built-in mechanism to determine how many times it will run, which means it can run indefinitely if the condition never becomes false.
Programiz
programiz.com › python-programming › while-loop
Python while Loop (With Examples)
In the above example, we have used a while loop to print the numbers from 1 to 3. The loop runs as long as the condition number <= 3 is True. ... The while loop evaluates condition, which is a boolean expression. If the condition is True, body of while loop is executed.
Top answer 1 of 16
1331
I am not sure what you are trying to do. You can implement a do-while loop like this:
while True:
stuff()
if fail_condition:
break
Or:
stuff()
while not fail_condition:
stuff()
What are you doing trying to use a do while loop to print the stuff in the list? Why not just use:
for i in l:
print i
print "done"
Update:
So do you have a list of lines? And you want to keep iterating through it? How about:
for s in l:
while True:
stuff()
# use a "break" instead of s = i.next()
Does that seem like something close to what you would want? With your code example, it would be:
for s in some_list:
while True:
if state is STATE_CODE:
if "//" in s:
tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
state = STATE_COMMENT
else :
tokens.add( TOKEN_CODE, s )
if state is STATE_COMMENT:
if "//" in s:
tokens.append( TOKEN_COMMENT, s.split( "//" )[1] )
break # get next s
else:
state = STATE_CODE
# re-evaluate same line
# continues automatically
2 of 16
413
Here's a very simple way to emulate a do-while loop:
condition = True
while condition:
# loop body here
condition = test_loop_condition()
# end of loop
The key features of a do-while loop are that the loop body always executes at least once, and that the condition is evaluated at the bottom of the loop body. The control structure show here accomplishes both of these with no need for exceptions or break statements. It does introduce one extra Boolean variable.
Tutorialspoint
tutorialspoint.com › python › python_while_loops.htm
Python - While Loops
If it fails to turn false, the loop continues to run, and doesn't stop unless forcefully stopped. Such a loop is called infinite loop, which is undesired in a computer program. The syntax of a while loop in Python programming language is − ... In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements. The following flow diagram ...
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
Code given below uses a 'while' loop with the condition "True", which means that the loop will run infinitely until we break out of it using "break" keyword or some other logic. ... Note: It is suggested not to use this type of loop as it is a never-ending infinite loop where the condition is always true and we have to forcefully terminate the compiler. Python programming language allows to use one loop inside another loop which is called nested loop. Following example illustrates the concept.
Published February 14, 2026
Problem Solving with Python
problemsolvingwithpython.com › 09-Loops › 09.04-Flowcharts-Describing-Loops
Flowcharts Describing Loops - Problem Solving with Python
The Python code that corresponds to this flowchart is below: # start for i in range(10): print("looping") # end · Below is the description of a program which can be coded with a while loop:
DataCamp
datacamp.com › tutorial › python-while-loop
Python While Loops Tutorial | DataCamp
June 25, 2020 - In the below example, you will create the variable offset with an initial value of 8. Then you will write a while loop that keeps running as long as the offset is not equal to 0. ... You will print out the sentence "correcting...". Next, decrease the value of offset by 1. You can do this with ...
DataCamp
datacamp.com › tutorial › do-while-loop-python
Emulating a Do-While Loop in Python: A Comprehensive Guide for Beginners | DataCamp
January 31, 2024 - To emulate a "do-while" loop in Python, you can use a while loop with a True condition and strategically place a break statement. Here's an example:
GeeksforGeeks
geeksforgeeks.org › python › python-while-loop
Python While Loop - GeeksforGeeks
When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements. Python Continue Statement returns the control to the beginning of the loop. ... # Prints all letters except 'e' and 's' i = 0 a = 'geeksforgeeks' while i < len(a): if a[i] == 'e' or a[i] == 's': i += 1 continue print(a[i]) i += 1
Published December 23, 2025
The Engineering Projects
theengineeringprojects.com › home › blog › python › while loop in python
While Loop in Python - The Engineering Projects
March 30, 2022 - The whole body of the while loop is executed each time it is executed. Python has two keywords, break and continue, to prevent a loop from repeating itself. This statement breaks a loop completely and immediately in Python. Python performs the next statement in the program after the loop body, which ends the current loop iteration. As soon as an expression is evaluated, it is evaluated again to see if the loop will continue or end. The following diagram illustrates the difference between the statements "break" and "continue":