Breaking and Continuing Loops

Tutorial 4 of 5

1. Introduction

In this tutorial, we’ll explore the use of "Break", "Continue", and "Pass" statements which are powerful tools in Python for controlling the flow of loops. They allow us to manipulate our loops in interesting ways.

What you will learn:

  • The purpose and usage of "Break", "Continue", and "Pass" statements
  • How to use these statements in your loops
  • How these statements can modify the flow of your loops

Prerequisites:

  • Basic understanding of Python (variables, data types)
  • Familiarity with loops in Python (For and While loops)

2. Step-by-Step Guide

Break Statement

The "break" statement is used to exit or "break" a loop. Once a break statement is encountered, the loop is terminated and the control moves to the next line of code after the loop.

Example:

for i in range(10):
    if i == 5:
        break
    print(i)

In the above snippet, the loop will terminate when i equals 5 and hence, only numbers from 0 to 4 will be printed.

Continue Statement

The "continue" statement is used to skip the rest of the code inside a loop for the current iteration only. The loop does not terminate but continues with the next iteration.

Example:

for i in range(10):
    if i == 5:
        continue
    print(i)

In this case, when i equals 5, it skips the print statement for that iteration and continues with the loop. Hence, it will print numbers from 0 to 9 except 5.

Pass Statement

The "pass" statement is a placeholder and is used when the syntax requires a statement but you do not want any command or code to execute.

Example:

for i in range(10):
    if i == 5:
        pass
    print(i)

The "pass" statement in the example doesn't affect the loop's output and it will print numbers from 0 to 9.

3. Code Examples

Let's look at some practical examples:

Example 1: Using break

for letter in 'Python':
    if letter == 'h':
        break
    print('Current Letter:', letter)

In this example, the loop will break when it encounters the letter 'h', so it only prints 'P', 'y', 't'.

Example 2: Using continue

for letter in 'Python':
    if letter == 'h':
        continue
    print('Current Letter:', letter)

In this case, the loop will skip the print statement when it encounters the letter 'h', so it prints 'P', 'y', 't', 'o', 'n'.

Example 3: Using pass

for letter in 'Python':
    if letter == 'h':
        pass
    print('Current Letter:', letter)

The "pass" statement in the example doesn't affect the loop's output and it will print all the letters in 'Python'.

4. Summary

In this tutorial, we covered the use of "Break", "Continue", and "Pass" statements in Python. We learned that:

  • "Break" is used to terminate a loop
  • "Continue" is used to skip the current iteration and continue with the next
  • "Pass" is used as a placeholder when a statement is required syntactically but no action needs to be performed

For further learning, you can explore how these statements work with nested loops.

5. Practice Exercises

Exercise 1: Write a Python program to print numbers from 1 to 10, but stop the loop if a number is divisible by 4.

Exercise 2: Write a Python program that prints all letters in the string 'Pythonic', but skips the letter 'o'.

Exercise 3: Write a Python program where you need to pass if the number is less than 0, print the number if it is between 0 and 10, and break if it is greater than 10 in the number list [-1, 4, 6, 7, 12].

Solutions:

Exercise 1:

for i in range(1, 11):
    if i % 4 == 0:
        break
    print(i)

Exercise 2:

for letter in 'Pythonic':
    if letter == 'o':
        continue
    print(letter)

Exercise 3:

numbers = [-1, 4, 6, 7, 12]
for number in numbers:
    if number < 0:
        pass
    elif number > 10:
        break
    else:
        print(number)