Python Loop Control Statements


Python Loop Control Statement

Table Of Contents:

  1. What Is Loop Control Statement?
  2. Break Statement.
  3. Continue Statement.
  4. Pass Statement.

(1)What Is A Loop Control Statement?

  • When you want to get out of the loop in between you can use Loop Control Statements.
  • Loop will run as many times as the condition is satisfying.

  • But, python provides us the ways to skip some iterations or stop the execution in between.

(2) Break Statement

  • The Break statement stops the execution and brings the control out of the loop.
  • You put some conditions and write a Break statement inside the loop.
  • If that condition is satisfying the Break statement will be executed and it will come out of the loop.

Break Syntax:

break

Note:

  • You only need to write the ‘break’ keyword to break the execution of the loop.

Example-1

numbers = [1,2,3,4,5,6,7,8,9,10]
for i in numbers:
    if i == 6:
        break
    print(i)
Output:
1
2
3
4
5

Note:

  • In this program, the for loop will break when it encounters i == 6.
  • And it will come out of the loop.

Example-2

number = 45
while number > 0:
    number = number // 2
    print(number)
    if number == 1:
        break
    
Output:
22
11
5
2
1

Note:

  • In this program, when the number is getting 1 the while loop will break.
  • And it will come out of the loop.

(3) Continue Statement

  • Continue statement is used to skip the current iteration when the condition is met and allows the loop to continue with the next iteration.
  • It does not bring the control out of the loop and unline the break statement.

Continue Syntax:

continue

Note:

  • You only need to write the ‘continue’ keyword to skip the current execution of the loop.

Example-1

numbers = [1,2,3,4,5,6,7,8,9,10]
for i in numbers:
    if i == 6:
        continue
    print(i)
Output:
1
2
3
4
5

7
8
9
10

Note:

  • In this program, when the number is getting 1 , the for loop will skip the current execution.
  • And it go to the next iteration and print.
  • You can see the number 6 is not printed.

Example-2

var = 10                    
while var > 0:              
    var = var - 1
    if var == 5:
        continue
    print('Current variable value :', var)
Output:
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6

Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0

Note:

  • While loop will not execute when the number is 5.
  • It will skit the code below the continue statement.

(4) Pass Statement

  • Sometimes you only declare the functions and in a later stage, you will implement its body.
  • At that time without a function body, it will give you an error.
  • Hence, the ‘pass’ keyword is helpful when the function body is not present.

Pass Syntax:

pass

Example-1

def addition():  
Output:
File "<ipython-input-16-8c308f48e9a2>", line 1
    def addition():
                     ^
SyntaxError: unexpected EOF while parsing

Example-2

def addition():
    pass

Note:

  • Here you can use the ‘pass’ keyword to avoid an error message.
  • In a later stage, you can implement the function body.

Example-3

numbers = [1,2,3,4,5]
for i in numbers:
    if i == 3:
        pass
    print(i)
Output:
1
2
3
4
5

Note:

  • Here the ‘pass’ keyword will not do anything.
  • It will not cause any changes to the flow of execution, it will just pass by..

Leave a Reply

Your email address will not be published. Required fields are marked *