Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 126 - 2609_Logicode
P. 126

    THE JUMP STATEMENTS

                      Sometimes, there is a situation when the control of the program needs to be transferred out of
                      the loop body, even if all the values of the iterations of the loop have not been completed. For this
                      purpose, jump statements are used in Python. Python offers two jump statements—break and
                      continue, which are used within loops.

                      THE BREAK STATEMENT

                      The break statement is used for exiting the program control out of the loop. The break statement
                      stops the execution of the loop and program flow continues to the statement after the loop. A
                      single break statement will break out of only one loop.

                      Syntax:
                          break
                      Program: To print first 12 whole numbers. Stop printing if number 7 is encountered.

                      On running the above program, you will get the following output:

                      THE CONTINUE STATEMENT
                      The continue statement is used inside loops. When a continue statement is encountered inside

                      a loop, control of the program jumps to the beginning of the loop for next iteration, skipping the
                      execution of rest of the statements inside the loop for the current iteration.

                      124   Premium Edition-VIII

| 121 122 123 124 125 126 127 128 129 130 131 |