Python Loops

In general, statements are performed sequentially: In a function, the first statement is executed at first, followed by the second, and so on. There may be a situation when it's important to execute a block of code various numbers of times.

Programming languages provide a lot of control structures that allow for more complicated execution paths.

Python programming language provides the various categories of loops to handle looping requirements which are described below:

Sr.No. Loop Type & Explantation
1 while loop

Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body.

2 for loop

Executes a sequence of statements various times and abbreviates the code that manages the loop variable.

3 nested loops

It is possible for you to use one or more loop inside any another while, for or do..while loop.

Loop Control Statements

Loop control statements transform execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are demolished.

Python supports various control statements which are described as follows:

Sr.No. Control Statement & Explanation
1 break statement

Terminates the loop statement and transfer execution to the statement instantly following the loop.

2 continue statement

Causes the loop to avoid the remainder of its body and immediately retest its condition before reiterating.

3 pass statement

The pass statement in Python is beneficial when a statement is needed syntactically but you do not want any command or code to execute