"break", "continue", and "pass" Statements

Sometimes it is necessary to exit a loop abruptly when a certain condition is met. The "break" statement is sometimes useful for such an application. Consider this code:

              strval              =              'forever'              for                              letter                            in                              strval              :                              if                              letter                            ==              'e'              :                              break              print              (              'The letter e has been found'              )            

In this case, the break statement terminates the loop if the character e  is found in the strval string.

On the other hand, encountering a given condition within a loop may require skipping over a set of commands and effectively "fast-forward" to the next iteration. The "continue" statement is useful for these applications.

Finally, the "pass" statement within a loop is a null statement that does nothing. It is sometimes useful when placed in a program where code is intended but has not yet been written. Read this page and practice the examples.

The break Statement

The break statement is used to break out of a loop statement i.e. stop the execution of a looping statement, even if the loop condition has not become False or the sequence of items has not been completely iterated over.

An important note is that if you break out of a for or while loop, any corresponding loop else block is not executed.

Example (save as break . py ):

              while                                          True              :                              s                            =                              input              (              'Enter something : '              )                                          if                              s                            ==                                          'quit'              :                                          break                                          print              (              'Length of the string is'              ,                              len              (              s              ))                                          print              (              'Done'              )            

Output:

              $ python                            break              .              py                            Enter                              something                            :                                          Programming                                          is                              fun                            Length                                          of                              the                            string                                          is                                          18                                          Enter                              something                            :                                          When                              the work                            is                                          done                                          Length                                          of                              the                            string                                          is                                          21                                          Enter                              something                            :                                          if                              you wanna make your work also fun              :                                          Length                                          of                              the                            string                                          is                                          37                                          Enter                              something                            :                                          use                                          Python              !                                          Length                                          of                              the                            string                                          is                                          11                                          Enter                              something                            :                              quit                            Done            


How It Works

In this program, we repeatedly take the user's input and print the length of each input each time. We are providing a special condition to stop the program by checking if the user input is 'quit' . We stop the program by breaking out of the loop and reach the end of the program.

The length of the input string can be found out using the built-in len function.

Remember that the break statement can be used with the for loop as well.

Swaroop's Poetic Python
The input I have used here is a mini poem I have written:

              Programming                                          is                              fun                            When                              the work                            is                                          done                                          if                              you wanna make your work also fun              :                                          use                                          Python              !            

The continue Statement

The continue statement is used to tell Python to skip the rest of the statements in the current loop block and to continue to the next iteration of the loop.

Example (save as continue . py ):

              while                                          True              :                              s                            =                              input              (              'Enter something : '              )                                          if                              s                            ==                                          'quit'              :                                          break                                          if                              len              (              s              )                                          <                                          3              :                                          print              (              'Too small'              )                                          continue                                          print              (              'Input is of sufficient length'              )                                          # Do other kinds of processing here...            

Output:

              $ python                            continue              .              py                            Enter                              something                            :                              a                            Too                              small                            Enter                              something                            :                                          12                                          Too                              small                            Enter                              something                            :                              abc                            Input                                          is                                          of                              sufficient length                            Enter                              something                            :                              quit            


How It Works

In this program, we accept input from the user, but we process the input string only if it is at least 3 characters long. So, we use the built-in len function to get the length and if the length is less than 3, we skip the rest of the statements in the block by using the continue statement. Otherwise, the rest of the statements in the loop are executed, doing any kind of processing we want to do here. Note that the continue statement works with the for loop as well.

Summary

We have seen how to use the three control flow statements - if , while and for along with their associated break and continue statements. These are some of the most commonly used parts of Python and hence, becoming comfortable with them is essential. Next, we will see how to create and use functions.


Source: https://python.swaroopch.com/control_flow.html
Creative Commons License This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 License.

Last modified: Tuesday, February 16, 2021, 12:34 PM