Tutorials Exercises Get Certified Services Bootcamps Spaces Sign Up Log in
Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R JQUERY DJANGO
Python Tutorial ADVERTISEMENT
Python HOME
Python Intro
Python Get Started
Python While Loops
Python Syntax
Python Comments
Python Variables
Python Data Types ❮ Previous Next ❯
Python Numbers
Python Casting
Python Strings
Python Booleans
Python Loops
Python Operators
Python has two primitive loop commands:
Python Lists
Python Tuples while loops
Python Sets for loops
Python Dictionaries
Python If...Else
Python While Loops The while Loop
Python For Loops
Python Functions With the while loop we can execute a set of statements as long as a condition is true.
Python Lambda
Python Arrays
Python Classes/Objects Example Get your own Python Server
Python Inheritance
Print i as long as i is less than 6:
i = 1
while i < 6:
print(i)
i += 1
Try it Yourself »
Note: remember to increment i, or else the loop will continue forever.
COLOR PICKER
The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i , which we set
to 1.
The break Statement
With the break statement we can stop the loop even if the while condition is true:
Example
Exit the loop when i is 3:
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
Try it Yourself »
ADVERTISEMENT
The continue Statement
With the continue statement we can stop the current iteration, and continue with the next:
Example ADVERTISEMENT
Continue to the next iteration if i is 3:
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
Try it Yourself »
The else Statement
With the else statement we can run a block of code once when the condition no longer is true:
Example
Print a message once the condition is false:
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
Try it Yourself »
Test Yourself With Exercises
Exercise:
Print i as long as i is less than 6.
i = 1
i < 6
print(i)
i += 1
Submit Answer »
Start the Exercise
❮ Previous Log in to track progress Next ❯
ADVERTISEMENT
ADVERTISEMENT
Spaces Upgrade Newsletter Get Certified Report Error
Top Tutorials Top References Top Examples Get Certified
HTML Tutorial HTML Reference HTML Examples HTML Certificate
CSS Tutorial CSS Reference CSS Examples CSS Certificate
JavaScript Tutorial JavaScript Reference JavaScript Examples JavaScript Certificate
How To Tutorial SQL Reference How To Examples Front End Certificate
SQL Tutorial Python Reference SQL Examples SQL Certificate
Python Tutorial W3.CSS Reference Python Examples Python Certificate
W3.CSS Tutorial Bootstrap Reference W3.CSS Examples PHP Certificate
Bootstrap Tutorial PHP Reference Bootstrap Examples jQuery Certificate
PHP Tutorial HTML Colors PHP Examples Java Certificate
Java Tutorial Java Reference Java Examples C++ Certificate
C++ Tutorial Angular Reference XML Examples C# Certificate
jQuery Tutorial jQuery Reference jQuery Examples XML Certificate
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.
W3Schools is Powered by W3.CSS.