Python Loop Statements

This chapter will introduce Python's loop statements. In general, programs are executed sequentially.

Programming languages provide various control structures that allow for 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 the for loop and the while loop (there is no do..while loop in Python):

Loop TypesDescription
while loopExecute the loop body when the given condition is true; otherwise, exit the loop body.
for loopRepeatedly execute statements
Nested loopsYou can nest a for loop inside the while loop body.


Loop Control Statements

Loop control statements can change the order in which statements are executed. Python supports the following loop control statements:

Control statementsDescription
break statementDuring the execution of a statement block, terminate the loop and break out of the entire loop.
continue statementDuring the execution of a statement block, terminate the current loop, break out of this iteration, and execute the next loop iteration.
pass statementpass is an empty statement, used to maintain the integrity of the program structure.
Other Extensions