Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 75 - 2607_Traclpad_Pro_5.1_flipbook_C8
P. 75

    THE continue STATEMENT

                      The continue statement causes the program to skip the rest of the statements in the current block
                      and move to the next iteration of the loop. It immediately transfers control to the evaluation of the
                      test expression of the loop for the next iteration of the loop.

                      Program 6: To demonstrate the use of the continue statement in a loop.

                          Program6.py
                       File  Edit  Format    Run   Options   Window    Help

                       #Program to show the use of the continue statement

                       for number in range(0, 12):
                           if(number == 8):
                               continue

                           print('Number is:', number)
                       print('Out of loop')

                      On running the program, we get the following output:

                          Output

                       Number is: 0
                       Number is: 1
                       Number is: 2

                       Number is: 3
                       Number is: 4
                       Number is: 5
                       Number is: 6
                       Number is: 7
                       Number is: 9

                       Number is: 10
                       Number is: 11
                       Out of loop

                      The example above prints all the numbers except 5 because when the value of i is equal to
                      5, the continue statement placed inside the if statement will force the program to skip the

                      iteration.

                       74   Premium Edition-VIII

| 70 71 72 73 74 75 76 77 78 79 80 |