Page 313 - Computer Science V2.0 Class 11
P. 313

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 11.8 Write a function prime(n) to check whether a number is prime number or not.

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

                  >>> prime(12)
                      12 = 2 * 6
                  >>> prime (8)
                      8 = 2 * 4
                  >>> prime(29)
                      29 is a prime number
                  >>> prime(13)
                      13 is a prime number
                  >>> prime(91)
                      91 = 7 * 13

                 In program 11.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.

                 11.5.2 continue Statement
                 Continue statement is also a jump statement, just like the break statement. When a continue statement is
                 encountered, the remaining statement(s) in the loop is skipped and the control jumps to the beginning of the loop for
                 the next iteration. The flowchart in Fig 11.5 illustrates the working of continue statement.
                 Syntax:

                 continue



                                                                                                   Looping in Python  299
   308   309   310   311   312   313   314   315   316   317   318