Previous | Next


Looping is about how you can repeat a block of statements until some condition is met. With a while loop, the mechanism for repeating a set of statements allows execution to continue for as long as a specified logical expression evaluates to true.

while <expression>:
  <while-processing code>

Let’s try the following sample code:

x = 0
while x < 10:
  print x
  x += 1

The result:

0
1
2
3
4
5
6
7
8
9

The while loop check the value condition of x so that it stop to loop if x is greater than 10. Otherwise, it continue to execute the inner block of statements where it will print the value of x and increase the value of x by 1 every time it execute the block of statement.

 


Previous | Next