Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Loop Statements

Python Loop Statements

In this chapter, we will introduce Python's loop statements. Under normal circumstances, a program executes sequentially.

Programming languages provide various control structures that allow more complex execution paths.

Loop statements allow us to execute a statement or group of statements multiple times. The following is the general form of a loop statement in most programming languages:

Python provides for loops and while loops (there is no do..while loop in Python):

Loop types Description
while loop Execute the loop body while the given condition is true; otherwise, exit the loop body.
for loop Repeatedly execute statements
Nested Loops You can nest a for loop inside a while loop body

Loop control statements

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

Control statement Description
break statement Terminate the loop during the execution of the statement block and break out of the entire loop
continue statement Terminate the current loop during the execution of the statement block, skip that iteration, and proceed to the next iteration.
The pass statement pass is a null statement, used to maintain the integrity of the program structure.