Python Loops | Types, Advantages and Loop Control Statements

Here, we have discussed about the python loops, advantages of loop and loop control statements. Loops are the statements that allows us to iterate..

In this article we will discuss about the loop in python, advantages of loop and Python loop control statements.

Python loops and types of loop in python

What is Loop in Python?

Python Loops are the statements that allows us to iterate over a block of code or sequence of elements for as long as the condition is satisfied. In simple words, we can say that with loop we can execute one or more than one statements for multiple times. Until the end of sequence, loop continues to iterate over.

Python Loops flowchart

Types of Python loops

There are three types of Loops in python that are mentioned as follows;

S.No Type Description
1. for loop To repeat or iterate over a sequence of elements and objects.
2. while loop To execute a block of code multiple times until the condition satisfied.
3.nested loop To write one loop inside another loop.

Now let's learn these Python loops with more depth;

Python For loop

With python, the programmers use for loop to iterate over or repeat sequence of objects or other elements that are iterable. Simply we can say that, for loop is used to transverse over tuple, list, structure, etc.

Python For loop flowchart

Python For loop Example;

number = [1, 2, 3]

for x in number:

  print(x) 

Output

1

2

3

Python While loop

Python while loop allows us to execute a particular block of code or statements for multiple times until the condition is satisfied. And in while the number of repetition or iteration are indefinite.

Python while loop flowchart

Python While loop Example;

i = 2

while i < 9:

  print(i)

  i += 2

Output

2

4

6

8

Python Nested loop

Python Nested loop allows to write a loop inside the other loop statement. And the loop that is inside or outside can be of any type i.e. it can be either while loop or for loop.

Python Nested loop flowchart

Python Nested loop Example;

num1 = [1, 2, 3]

num2 = [4, 5, 6]

for x in num1:

 for y in num2:

    print(x, y)

Output

1 4

1 5

1 6

2 4

2 5

2 6

3 4

3 5

3 6

Advantages of Python Loops

Now, we will learn the advantages of python loops;

• Loop allows us to transverse over tuple, list, structure, or any data structure.

• With loops, there is no need to write a code again and again.

• Loop allows use the re-usability of the codes or statements.

Python Loop Control Statements

Loop control statements are those statements which are used to change the normal execution. Simply, we can say that the loop control statements are used to change the normal sequence flow. It is generally used to stop execution or to skip the repetition. In python, there are three types loop control statements which are discussed as follows;

Python Break Statement

The Python break statement is used to terminate or stop the execution and to bring out the control from the loop and transfer the execution to the next immediate statement after that loop.

Python Break statement Example;

for i in range(10):

  if i > 5:

    break

  print(i)

Output

0

1

2

3

4

5

Python Continue Statement

The Python continue statement is used to end the present iteration after the completion of condition and to immediately starts the iteration of next loop. We cannot bring out the control from the loop by using continue statement.

Python Continue statement Example;

for i in range(10):

  if i == 5:

    continue

  print(i)

Output

0

1

2

3

4

6

7

8

9

Python Pass Statement

Pass statement is used while we don't want to perform anything after the completion of the condition. It generally used when we don't want functionality for current condition but it can be used in future.

Python Pass statement Example;

x = 1

y = 2

if y > x:

  pass

Conclusion

Above we have discussed about the python loops, advantages of loop and Python loop control statements. Loops are the statements that allows us to iterate over a block of code for as long as the condition is satisfied. There are three types of Loops in python i.e., for loop, while loop and nested loop. And there are three types loop control statements which are break statement, continue statement and pass statement.