AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

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
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:
counter = 4
while counter < 7:
   print("We have inside loop")
   counter = counter + 1
else:
  print("Inside else")
Output:
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:
i = 1
while i < 12:
  print(i)
  if (i == 8):
     break
  i += 1
Output:
1
2
3
4
5
6
7
8


continue Statement

continue statement stop the current iteration, and continue with the next:

Example: continue statement
i = 2
while i < 7:
   i += 1
   if i == 4:
     continue
   print(i)
Output:
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
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news 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
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news 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
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.