Loop control statements in Python

While working with a loop in the program there may be some case where we want to terminate the loop or skip the loop block while some condition occurs. For handling this type of cases we use loop control statements.

There are following types of loop control statements in Python –

  1. break
  2. continue
  3. pass

Contents

  1. break
  2. continue
  3. pass

 


1. break

break statement is use to terminate the loop when specified condition arises. break statement also used when working with infinite loop in program for terminate or exit from loop and resume execution to next statement.

Syntax –

break

Example –

break statement with while loop

num = 1

while num<=5 :
      print("num : ",num)
      if num == 3
          break
      num+=1

print("Bye")

When we execute the above program, it will produce the following output –

num : 1
num : 2
num : 3
Bye

break statement with for loop

languages = ["C","Python","PHP","JAVA","Javascript","jQuery"]

for lang in languages:
    if lang == "JAVA":
       break
    print(lang)

print("Bye")

When we execute the above program, it will produce the following output –

C
Python
PHP
Bye

Terminating the infinite loop

num = 1

while 1:
      if num == 5:
           break
      print("num : ",num)
      num += 1

print("Bye")

When we execute the above program, it will produce the following output –

num : 1
num : 2
num : 3
num : 4
Bye

2. continue

continue statement skips the block of code which execute after it and return the control to the begining of the loop. continue statament is use to skip the block of code in loop when specific condition arises and it return the control to the begining of the loop. continue statatement is used with both while and for loop.

Syntax –

continue

Example –

continue statement with while loop

num = 1

while num<=10 :
      num += 1
      if num == 3 or num == 5
          continue

      print("num : ",num)

print("Bye")

When we execute the above program, it will produce the following output –

num : 2
num : 4
num : 6
num : 7
num : 8
num : 9
num : 10
num : 11
Bye

continue statement with for loop

languages = ["C","Python","PHP","JAVA","Javascript","jQuery"]

for lang in languages:
    if lang == "JAVA":
       continue
    print(lang)

print("Bye")

When we execute the above program, it will produce the following output –

C
Python
PHP
Javascript
jQUery
Bye

3. pass

pass statement is little different from previous statements, when its executes nothing happens. It useful when you are implementing conditional statement or defining method in a program but at the current time you don’t decide that what it will do or a block of code is no longer required in program. By removing block codes and defining pass statement, by using that it no longer interfare within program.

Syntax –

pass

Example –

num = 1

while num<=5 :
      print("num : ",num)
      if num == 3
          print("pass statement block")
          pass
      num+=1

print("Bye")

When we execute the above program, it will produce the following output –

num : 1
num : 2
num : 3
pass statement block
num : 4
num : 5
Bye
Spread the love

Related Post

Be First to Comment

Leave a Reply

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