Python While Loops
Python Tutorial » Python While Loops
while loop is used to iterate over a block of code as long as expression or condition is true.
hile loop execute a set of statements as long as a condition is true.
Example: while loop - print i as long as i is less than 8
Output:
1
2
3
4
5
6
7
8
i = 1
while i <= 8:
print(i)
i += 1
while i <= 8:
print(i)
i += 1
Output:
1
2
3
4
5
6
7
8
While loop with else - example
Same as with for loops, while loops can also have an optional else block.
Example:
We have inside loop
We have inside loop
We have inside loop
Inside else
counter = 4
while counter < 7:
print("We have inside loop")
counter = counter + 1
else:
print("Inside else")
Output: while counter < 7:
print("We have inside loop")
counter = counter + 1
else:
print("Inside else")
We have inside loop
We have inside loop
We have inside loop
Inside else
break Statement - example
break statement stop the loop even if the while condition is true:
Example:
1
2
3
4
5
6
7
8
i = 1
while i < 12:
print(i)
if (i == 8):
break
i += 1
Output:while i < 12:
print(i)
if (i == 8):
break
i += 1
1
2
3
4
5
6
7
8
continue Statement
continue statement stop the current iteration, and continue with the next:
Example: continue statement
3
5
6
7
i = 2
while i < 7:
i += 1
if i == 4:
continue
print(i)
Output:while i < 7:
i += 1
if i == 4:
continue
print(i)
3
5
6
7
python while : loop, true, else, true loop, break, loops, loop break, exercices, not, false.
python while loop : break, exercices, timeout, increment, multiple conditions, for range, without body, example programs
Python While Loops - python
Online Editor
This tool makes it easy to create, adjust, and experiment with custom colors for the web.
HTML Templates

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.
CSS HTML Layout

Find here examples of creative and unique website layouts.
Free CSS HTML Menu

Find here examples of creative and unique website CSS HTML menu.