By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
When any program is executed, it runs sequentially. The statement which appears first in the sequence is executed first then the next statement and so on till the last statement of the program. Many times there is a requirement to run same block of code in a program multiple times then there arise a need of a control structure known as loops.
A loop makes a statement or group of statements in a block of code to execute multiple times if the condition is true and exits the loop when the condition becomes false.
Such a loop is illustrated in the below diagram.
If false
Types of Loop in Python In Python programming language, following are the types of loops used to handle looping requirements.
Loop Type
Description
while loop
While loop type, repeats a statement or group of statements while a given condition is true. It tests the condition each time it executes the loop body and it exits the loop when condition becomes false.
for loop
For loop type executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
nested loops
It is a loop within a loop. In Python we can use a while loop in another while or for loop or for loop in another while or for loop.
Loop Control Statements in Python Loop control statements in Python are used to change execution from its normal sequence. When such execution leaves a scope, all automatic objects that were created in that scope are destroyed or removed.
In Python language, the following control statements are supported.
Control Statement
break statement
The break statement in Python is used to terminate the loop statement and transfers execution to the statement immediately following after the end of loop.
continue statement
The continue statement in Python is used to cause the loop to skip the remainder of its body and immediately retest its condition prior to reiterating the looping body.
pass statement
The pass statement in Python is used when a statement is required syntactically but we do not want any command or code to execute on that statement.
Loop and Control Statement Python Code Example
While Statement The syntax of a while loop in Python programming language is as follows.
while expression:
statement(s)
Example code for while loop in Python.
For Statement The syntax of a ‘for’ loop in Python programming language is as follows.
for iterating_var in sequence: statements (s)
Example code for ‘for’ loop in Python.
Nested Loop As explained earlier, it is a loop within a loop.
Below is the syntax for nested for loop in Python.
for iterating_var in sequence: for iterating_var in sequence: statements(s)
statements(s)
Below is the syntax for nested while loop in Python.
while expression: while expression: statement(s)
Below is an example for ‘while nested loop in Python along with its output. Example on control statements in Python
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.