Wednesday, 28 August 2019

Jump statements in Python 3

Jump statements in Python 3


In Python, there are 2 types of Jump Statement:- break Statement and continue Statement

These 2 statements are used with in loops to jump out of the loop iterations.

The break Statement

The break statement enables a program to skip over a part of the code. A break statement terminates the loop.

Syntax of break statement:

while <test-condition>:
                statement 1
                if <condition>:
                                break
                statement 2
                statement 3
statement 4
statement 5


Example of break statement in Python 3

example of python 3 break




Here the code intends to divide 10 pairs of numbers by inputting 2 numbers ‘a’ and ‘b’ in each iteration. If the number ‘b’ is zero, the loop is immediately terminated displaying message ‘Division by Zero Error! Aborting!’ otherwise the numbers are repeatedly input and their quotients are displayed.


Question: Write a program to input some numbers repeatedly and print their sum. The program ends when the users say no more to enter i.e. normal termination or program aborts when the number entered is less than 0.
Answer:

Python 3 break statement




Infinite Loops and break statement

Sometimes, we create infinite loops purposely by giving an expression which always remain true, but for such purposely created infinite loops, they incorporate some condition inside the loop body on which they can break out of the loop using break statement.

For example

a=2
while True:
           print (a)
           a*=2
           if a>100:
                break

The continue statement

The continue statement is another jump statement like the break statement as both statements skip over a part of the code. But the continue statement is somewhat different from break. Instead of forcing termination, the continue statement forces the next iteration of the loop to take place, skipping any code in between.

Question: Write a program to display difference between break and continue statements?

Answer:


continue statement in python

The else Statement in loop | Python loops along with else clause.

Both loops of Python have an else clause which is different from else of if-else statements. The else of a loop executes only when the loop ends normally i.e. only when the loop is ending because the while loop’s test condition has resulted in false or the for loop has executed for the last value in sequence.

The else clause of a Python loop executes when the loop terminates normally, not when the loop is terminating because of a break statement.
The else clause of a loop appears at the same indentation as that of loop keyword while or for.

Syntax of Python loops along with else clause.

for <variable-name> in <sequence>:
                Statement 1
                Statement 2
else:
                statement




while <test-condition>:
                statement 1
                statement 2
else:
                statement



For example

for a in range (1,4)
                print (“Element is”, end = ’ ’)
                print(a)
else:
                print (“Ending loop after printing all elements of sequence”)

Output

Element is 1
Element is 2
Element is 3
Ending loop after printing all elements of sequence.


Here, the last statement is printed because the else clause of given for loop executed when the for loop was terminating normally.


Now consider the same loop as above but with a break statement

for a in range (1,4)
       if a%2==0:
              break
      print (“Element is”, end = ’ ’)
      print(a)
else:
                print (“Ending loop after printing all elements of sequence”)

Output

Element is 1

Here when ‘a’ has the value 2, the break statement will execute and loop terminate. Hence just one element will be printed.
As break terminate the loop, the else clause also did not execute, so no line after the printing of elements.


No comments:

Post a Comment

Recent Post

Python Project | Banking System | Project on Banking System using python

  PYTHON PROJECT Banking System import csv import pandas as pd import matplotlib.pyplot as plt import sys import datetime found=False def ne...