Python While Loop break, continue, else block

Python python-loops

In this post, We are going to understand Python While Loop break, continue, else block with code example.

While loop in Python

While loop runs the code_body(set of statements) until the given_condition is true. In case of condition evaluated to False control come out of loop and loop will terminate. The number of times the loop will run totally depends on the condition.

Syntax for While loop

while given_condition:
 #code_body

How Python While Loop work

  • The first step, a while loop starts whenever an interpreter comes across a while keyword.
  • In the second step, the given_condition is evaluated, If evaluated to True then code_body of while loop executes.
  • In the next step, the condition is evaluated again, if evaluated to True then again code_body will run. The action of condition evaluation and running code_body is executed again and again until the condition is evaluated as false.
  • If the condition is evaluated to False then while loop gets terminated.

Let us understand this with the help of an example.

Example :Python while loop

my_num = 1

while my_num<5:

  square = my_num*my_num

  print('Square of number : ',square)

  my_num = my_num+1
  • At step first, the initial value of my_num = 1. while loop condition (my_num<5) is evaluated as True, Therefore all statements in while loop body will execute.
  • In Second step values of my_num =2, because it increment it by 1 my_num = my_num+1 again the condition(my_num<5) evaluated to true, Therefore all statement in while loop body will execute.
  • This process runs again and again till the value of my_num = 4.
  • In final step, my_num = my_num+1 will be 5,So condition(my_num<5) evaluates to False and loop will terminate.

Output

Square of number :  1
Square of number :  4
Square of number :  9
Square of number :  16

Python While loop with break statement

As the name implies the break statement is to terminate the loop. This is helpful when we would like to break our loop based on a condition. We will understand it with an example of how to use a break statement with a while loop.

number = 1
sum = 0
while number<5:

  if(number==3):

    sum = sum+number

    print('break statement terminated the loop')

    print('sum of number is :',sum)

    break
  number = number+1

Output

break statement terminated the loop
sum of number is : 3

Python While loop with continue statement

The continue statement is used to end the current iteration and jump to next iteration. Inside a while loop if we want to skip an code and want to move to next Iteration then we used the continue statement inside while loop.

number = 1

while number<5:


  if(number%3==0):
   
    continue    
 

  print('number:',number)

  number = number+1

Output

number: 1
number: 2

Infinite Python While Loop

The infinite while loop in which the condition never evaluated to False.It run for infinite number of times.

Example of infinite while loop

number = 1
mutiply = 1
while number<3:
  mutiply = number*mutiply
  print('mutiplication of number : ',mutiply)

This is an example of infinite while loop the value of variable number will never increment and always remain 1 and number<3, therefore condition will never be evaluated to False. So the loop runs an infinite number of times.

Python While Loop with else block

  • While loop in Python has an optional else block gives it more powers, unlike other programming languages.
  • If we have to need to run some code statement after the loop terminates, in such case while….else block come handy.
  • The while..else block execute after while loop termination. if while loop terminated by break statement, while else block would not be executed.

Example :while ….else block

number = 1
mutiply = 1
while number<5:
  
  mutiply = number*mutiply

  number = number +1
else:
  print('loop terminated Successfully')
  print('mutiplication is :',mutiply)

Output

loop terminated Successfully
multiplication is : 24

Here,while ..else block would not execute, we are checking if the number is 3 then break statement is encountered and the loop will terminate.

Example:While…..else with break statement

number = 1
mutiply = 1

while number<5:
  if(number==3):

    mutiply = number*mutiply

    print('break terminatd the loop')

    print('mutiplication is :',mutiply)

    break
  number = number +1

else:

  print('loop terminated Successfully')
  print('mutiplication :',mutiply)

Output

break terminatd the loop
mutiplication is : 3

Conclusion:

In this post,We have understood the while loop in Python with code examples. I hope you will use these loops in your code.