Python Looping Techniques


In this section we will learn the correct use of break and continue statement to control the execution of the looping construct.

A number of forms of loops can be created in Python using the break and continue statements in for and while loop. The different forms created are described in detail below:

 

The infinite loop

An infinite loop can be created using the while loop. This can be done when we set a condition that the condition in while loop always remain true. When the condition in while loop always remain true then the loop will never terminate and will become infinite loop.

 

EXAMPLE to create an infinite loop using while loop

Consider the following example, in which an infinite loop is created using while loop, the loop will never terminate. If you want to break the loop or exit from the loop press ctrl + c:

CODE

>>> while True:

          n = int(input(“enter a number “))

          print(“The number you entered is: “, n)

OUTPUT

enter a number 3

The number you entered is:  3

enter a number 5

The number you entered is:  5

enter a number 6

The number you entered is:  6

enter a number 7

The number you entered is:  7

enter a number 8

The number you entered is:  8

enter a number 9

The number you entered is:  9

enter a number 3

enter a number 65

The number you entered is:  65

The number you entered is:  3

enter a number 9

The number you entered is:  9

enter a number 3

The number you entered is:  3

enter a number 3

The number you entered is:  3

enter a number 89

The number you entered is:  89

enter a number 65

The number you entered is:  65

enter a number 34

The number you entered is:  34

enter a number 7

The number you entered is:  7

enter a number 34

The number you entered is:  34

enter a number 78

The number you entered is:  78

enter a number 23

The number you entered is:  23

enter a number 67

The number you entered is:  67

enter a number

enter-a-number

In this way, the loop will continue to execute and becomes infinite. If you want to terminate the loop you should press ctrl + c. The loop can also be terminated if the user enters a character, because the input was of int type. This will generate an error message and loop will stop to execute.

Consider the following part of the output in which we entered a character to stop the loop:

OUTPUT

enter a number 34

The number you entered is:  34

enter a number 7

The number you entered is:  7

enter a number 34

enter a number 23

The number you entered is:  23

The number you entered is:  34

enter a number 78

The number you entered is:  78

enter a number 23

The number you entered is:  23

enter a number 67

The number you entered is:  67

enter a number 3

The number you entered is:  3

enter a number r

Traceback (most recent call last):

  File “<pyshell#16>”, line 2, in <module>

    n = int(input(“enter a number “))

ValueError: invalid literal for int() with base 10: ‘r’

valueerror

 

Loop with condition at the top

This type of loop can be created using the while loop. In this type of loop the condition is written on the top of the loop and the loop functions like a normal loop without any break statement. The loop will be terminated when the condition at the top of the loop becomes false.

 

Flowchart of Loop with condition at the top

The following is the flowchart of the loop with condition at top:

flowchart-of-loop-with-condition-at-the-top

 

EXAMPLE of loop with condition at top:

Consider the following example in which we are using the while loop with condition at the top to add the whole numbers up to the number that will be entered by the user:

CODE

>>> n = int(input(“Enter a number: “))

Enter a number: 5

>>> sum = 0

>>> j = 0

>>> while j<=n:

          sum = sum + j

          j = j+1

>>> print (“The sum of natural numbers is: “, sum)

OUTPUT

The sum of natural numbers is:  15

the-sum-of-natural-numbers-is-15

In the above example, a number is entered by the user, the counter variable of while loop is initialized at 0 because we are finding the sum of whole numbers the loop will be executed till j becomes smaller than and equal to n and n is 5 (entered by user). Inside the body of the loop, we are adding j to sum and storing the resulting value in the variable sum and j is incremented by 1. At last outside the body of the loop the variable sum is printed that had the value of 15.

 

Loop with condition in the middle

The loop with condition at the middle can be created by using an infinite loop with a conditional break statement that is using break statement inside the ‘if’ statement between the body of the loop.

 

Flowchart of loop with condition in middle

flowchart-of-loop-with-condition-in-middle

EXAMPLE of loop with condition in the middle

Consider the following example in which we will be taking the input from the user until 0 is encountered using the loop with condition in the middle:

CODE

>>> y = 0

>>> while True:

          n = int(input(“Enter a number: “))

          if y == x:

                    break

          print(“Terminating the loop, 0 is encountered”)

OUTPUT

Enter a number: 3

enter-a-number-3

In the above example, an infinite loop is defined that has a condition in the middle that if the number entered by the user becomes equal to 0 or y then the loop must break.

 

Loop with condition at the bottom

In the structure when loop with condition at the bottom is defined then the loop must be executed at once at least. This looping structure is similar to the do while loop in C++ programming language in which the loop must be executed at once because the condition was checked at the end of the body of the loop.

A loop with condition at the bottom can be created by using an infinite loop and a conditional break statement that is break inside the ‘if’ statement at the end of the body of loop.

 

Flowchart of loop with condition at bottom

flowchart-of-loop-with-condition-at-bottom

 

EXAMPLE with condition at the bottom

Consider the following example, in which the loop with condition at the bottom is demonstrated and we will be taking the input from the user. The loop will terminate when 0 is encountered using the loop with condition at the bottom, in this way the loop will be executed at once at least:

CODE

>>> y = 0

>>> while True:

          n = int(input(“Enter a number: “))

          print(“Terminating the loop, 0 is encountered”)

          if y == x:

                   break

OUTPUT

Enter a number: 3

In the above example, an infinite loop is defined that has a condition at the end that if the number entered by the user becomes equal to 0 or y then the loop must break. The loop will be executed at least once because the condition is at the bottom and if the user enters a 0 then the loop will be terminated and control will be transferred outside the body of the loop.