HOME/Articles/

python types of loops

Article Outline

In this article we introduce Python's loop, the program in general are executed sequentially. Sequentially means in sequential order (top down):

do A
do B
do C

In a variety of programming languages, control structures allows for more complex execution paths.

Loop allows us to perform a statement or group of statements repeatedly, the following is the general form in most programming languages ​​loop statement:

python loop

Python is provided for and while loops (not do..while loop in Python):

Circulation type Description
The while loop determines the given condition is true. When the execution loop, or exit the loop.
For loop Repeat statement
[Nested loop] You can be nested for loop while loop body

The while loop in code:

>>> index = 0
>>> n = 10
>>> while index < n:
...     print(index)
...     index = index + 1
... 
0
1
2
3
4
5
6
7
8
9

The for loop:

>>> for i in range(1,3):
...     print(i)
... 
1
2

The nested for loop:

>>> 
>>> for x in range(1,10):
...     for y in range(1,10):
...         print(f"({x},{y})")

loop control statements

Loop control statements can change the order of statement execution. Python loop control supports the following statement:

Control statements Description
Break Statement Terminates the loop during the execution of statement block, and jump out of the entire cycle
Continue Statement Terminates the current execution loop statement block, out of the cycle, the next cycle execution.
Pass sentence pass an empty statement, in order to maintain the integrity of the program structure.