Page 219 - Computer Science Class 11 Without Functions
P. 219

Consider the code explaining the functioning of break statement in a for loop and while loop:

                         for x in range(1,5):                    num=5
                             print(x)                            while num>=0:
                             if x==3:                                print(num*num)
                               break                                 if num%3==0:
                         print("Over")                                 break
                                                                     num=num-1
                                                                 print("Done")

                         Output:                                 Output:

                         1                                       25
                         2                                       16
                         3                                       9
                         Over                                    Done
            In the example of for loop, as the value of x becomes equal to 3, the test condition of if statement evaluates to
            True,  which leads to the execution of break  statement. The control then skips the remaining statements in the
            loop (even though all the values in the range have not been traversed) and moves to the statement immediately
            following the loop (print ("Over")).

            In the example of while loop, the break statement is encountered when the value of num is 3. Therefore, the loop
            terminates and the control shifts to the statement, print("Done").

            If the break statement is inside a nested loop, the innermost loop is terminated and the control shifts to the immediate
            outer loop.
            Program 9.8 Write a program To check whether a number is prime number or not.

              01 '''
              02 objective: To check whether a number is prime.
              03 input:
              04     n: the numbers to be tested for primality
              05 output:
              06     The message indicating whether n is prime
              07 Approach: Given n is prime if it is not divisible by any integer in range(2, n)
              08 '''
              09 n = int(input('Enter n: '))
              10 upperLimit = n
              11 for i in range(2, n):
              12     if n%i == 0:
              13         print(n, 'is not a prime number as', n, '=', i, '*', n//i)
              14         #i divides n
              15         break
              16 else:
              17     print(n, 'is a prime number')
            Sample Output:

             >>> Enter n: 5
                 5 is a prime number
             >>> Enter n: 6
                 6 is not a prime number as 6 = 2 * 3
            In program 9.8, for each value of i, the condition n % i == 0 is evaluated. If the condition n % i == 0 holds, the break
            statement gets executed and the control exits the inner for-loop. Because of the abrupt exit from the for-loop on the
            execution of the break statement, the else clause in the inner for-loop does not get executed. However, if for a given
            value of n, n % i == 0 does not hold for any value of i in range(2, n), then the n must be a prime. In this case, the else
            part of the inner for-loop is executed, and a message is displayed that it is a prime number.


                                                                                              Looping in Python  217
   214   215   216   217   218   219   220   221   222   223   224