Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconPython Programming Unlocked for Beginners
Python Programming Unlocked for Beginners

Chapter 4: Control Structures

4.3: Loop Control (break, continue)

When working with loops, there may be situations where you need more control over the execution flow. Sometimes, you may want to terminate the loop prematurely or skip certain iterations of the loop based on certain conditions. 

In Python, you can use the break and continue statements to modify the behavior of for and while loops. The break statement allows you to terminate the loop prematurely when a certain condition is met. For example, if you are searching for a particular value in a list, you can use the break statement to terminate the loop as soon as the value is found, instead of continuing to iterate through the rest of the list.

On the other hand, the continue statement allows you to skip certain iterations of the loop based on a certain condition. For example, if you are iterating through a list of numbers and you only want to process the even numbers, you can use the continue statement to skip over the odd numbers.

In this topic, we will discuss the use of these loop control statements and how they can be used effectively in different scenarios. By the end of this topic, you should have a better understanding of how to use break and continue statements in your Python programs to achieve more precise control over the execution flow of your loops.

4.3.1 break Statement:

The break statement is used to exit a loop prematurely, i.e., before the loop's condition becomes false or before iterating over all items in a sequence. When the break statement is encountered inside a loop, the loop is terminated immediately, and the program continues executing the code following the loop. 

For example, let's say we want to search for a specific number in a list and stop the loop once the number is found:

numbers = [1, 2, 3, 4, 5]
search = 3

for number in numbers:
    if number == search:
        print("Found:", number)
        break

Output:

Found: 3

In this example, the loop stops iterating as soon as the search number is found, saving time and resources.

4.3.2 continue Statement:

The continue statement is used to skip the rest of the code inside a loop for the current iteration and jump to the next iteration. In other words, when the continue statement is encountered, Python skips the remaining code in the loop and moves to the next item in the sequence or evaluates the loop condition again.

For example, let's say we want to print all the numbers from 1 to 10, except for the multiples of 3:

for i in range(1, 11):
    if i % 3 == 0:
        continue
    print(i)

Output:

1
2
4
5
7
8
10

In this example, the loop skips printing the numbers that are divisible by 3 and continues with the next iteration.

In summary, loop control statements like break and continue can provide significant benefits in terms of control flow within loops. The break statement, for example, can be used to exit a loop prematurely if certain conditions are met. This can be particularly useful in situations where you want to stop iterating through a loop as soon as a specific condition is met, or if you want to exit the loop altogether.

Similarly, the continue statement can be used to skip over specific iterations of a loop, which can help to optimize loop performance by avoiding unnecessary computation. By incorporating these control structures into your loops, you can make your code more efficient and readable, while also gaining greater control over the way your programs execute.

Exercise 4.3.1: Print First Five Even Numbers

In this exercise, you will write a Python program that prints the first five even numbers using a for loop and the continue statement.

Instructions:

  1. Create a new Python file or open a Python interpreter.
  2. Use a for loop to iterate through the numbers from 1 to 10 (inclusive).
  3. Use an if statement to check if the current number is odd.
  4. If the number is odd, use the continue statement to skip the rest of the code in the loop and move on to the next iteration.
  5. Print the even number and increment a counter.
  6. When the counter reaches 5, use the break statement to exit the loop.

Your final code should look something like this:

counter = 0

for i in range(1, 11):
    if i % 2 != 0:
        continue
    print(i)
    counter += 1
    if counter == 5:
        break

Output:

2
4
6
8
10

Exercise 4.3.2: Sum of Positive Numbers

In this exercise, you will write a Python program that calculates the sum of all positive numbers in a list using a for loop and the continue statement.

Instructions:

  1. Create a new Python file or open a Python interpreter.
  2. Assign a list of integers (positive and negative) to a variable named numbers.
  3. Initialize a variable named total with the value 0.
  4. Use a for loop to iterate through the numbers in the list.
  5. Use an if statement to check if the current number is negative.
  6. If the number is negative, use the continue statement to skip the rest of the code in the loop and move on to the next iteration.
  7. Add the positive number to the total variable.
  8. Print the value of total after the loop is complete.

Your final code should look something like this:

numbers = [1, -2, 3, -4, 5, -6, 7]
total = 0

for number in numbers:
    if number < 0:
        continue
    total += number

print(total)

Output:

16

Exercise 4.3.3: Find the First Factor

In this exercise, you will write a Python program that finds the first factor of a given number using a for loop and the break statement.

Instructions:

  1. Create a new Python file or open a Python interpreter.
  2. Assign an integer value to a variable named n.
  3. Use a for loop to iterate through the numbers from 2 to n (inclusive).
  4. Use an if statement to check if the current number is a factor of n.
  5. If the number is a factor, print it and use the break statement to exit the loop.

Your final code should look something like this:

n = 20

for i in range(2, n + 1):
    if n % i == 0:
        print("First factor:", i)
        break

Output:

First factor: 2

These exercises help you practice using the break and continue loop control statements in Python to control the flow of your programs and accomplish various tasks.

4.3: Loop Control (break, continue)

When working with loops, there may be situations where you need more control over the execution flow. Sometimes, you may want to terminate the loop prematurely or skip certain iterations of the loop based on certain conditions. 

In Python, you can use the break and continue statements to modify the behavior of for and while loops. The break statement allows you to terminate the loop prematurely when a certain condition is met. For example, if you are searching for a particular value in a list, you can use the break statement to terminate the loop as soon as the value is found, instead of continuing to iterate through the rest of the list.

On the other hand, the continue statement allows you to skip certain iterations of the loop based on a certain condition. For example, if you are iterating through a list of numbers and you only want to process the even numbers, you can use the continue statement to skip over the odd numbers.

In this topic, we will discuss the use of these loop control statements and how they can be used effectively in different scenarios. By the end of this topic, you should have a better understanding of how to use break and continue statements in your Python programs to achieve more precise control over the execution flow of your loops.

4.3.1 break Statement:

The break statement is used to exit a loop prematurely, i.e., before the loop's condition becomes false or before iterating over all items in a sequence. When the break statement is encountered inside a loop, the loop is terminated immediately, and the program continues executing the code following the loop. 

For example, let's say we want to search for a specific number in a list and stop the loop once the number is found:

numbers = [1, 2, 3, 4, 5]
search = 3

for number in numbers:
    if number == search:
        print("Found:", number)
        break

Output:

Found: 3

In this example, the loop stops iterating as soon as the search number is found, saving time and resources.

4.3.2 continue Statement:

The continue statement is used to skip the rest of the code inside a loop for the current iteration and jump to the next iteration. In other words, when the continue statement is encountered, Python skips the remaining code in the loop and moves to the next item in the sequence or evaluates the loop condition again.

For example, let's say we want to print all the numbers from 1 to 10, except for the multiples of 3:

for i in range(1, 11):
    if i % 3 == 0:
        continue
    print(i)

Output:

1
2
4
5
7
8
10

In this example, the loop skips printing the numbers that are divisible by 3 and continues with the next iteration.

In summary, loop control statements like break and continue can provide significant benefits in terms of control flow within loops. The break statement, for example, can be used to exit a loop prematurely if certain conditions are met. This can be particularly useful in situations where you want to stop iterating through a loop as soon as a specific condition is met, or if you want to exit the loop altogether.

Similarly, the continue statement can be used to skip over specific iterations of a loop, which can help to optimize loop performance by avoiding unnecessary computation. By incorporating these control structures into your loops, you can make your code more efficient and readable, while also gaining greater control over the way your programs execute.

Exercise 4.3.1: Print First Five Even Numbers

In this exercise, you will write a Python program that prints the first five even numbers using a for loop and the continue statement.

Instructions:

  1. Create a new Python file or open a Python interpreter.
  2. Use a for loop to iterate through the numbers from 1 to 10 (inclusive).
  3. Use an if statement to check if the current number is odd.
  4. If the number is odd, use the continue statement to skip the rest of the code in the loop and move on to the next iteration.
  5. Print the even number and increment a counter.
  6. When the counter reaches 5, use the break statement to exit the loop.

Your final code should look something like this:

counter = 0

for i in range(1, 11):
    if i % 2 != 0:
        continue
    print(i)
    counter += 1
    if counter == 5:
        break

Output:

2
4
6
8
10

Exercise 4.3.2: Sum of Positive Numbers

In this exercise, you will write a Python program that calculates the sum of all positive numbers in a list using a for loop and the continue statement.

Instructions:

  1. Create a new Python file or open a Python interpreter.
  2. Assign a list of integers (positive and negative) to a variable named numbers.
  3. Initialize a variable named total with the value 0.
  4. Use a for loop to iterate through the numbers in the list.
  5. Use an if statement to check if the current number is negative.
  6. If the number is negative, use the continue statement to skip the rest of the code in the loop and move on to the next iteration.
  7. Add the positive number to the total variable.
  8. Print the value of total after the loop is complete.

Your final code should look something like this:

numbers = [1, -2, 3, -4, 5, -6, 7]
total = 0

for number in numbers:
    if number < 0:
        continue
    total += number

print(total)

Output:

16

Exercise 4.3.3: Find the First Factor

In this exercise, you will write a Python program that finds the first factor of a given number using a for loop and the break statement.

Instructions:

  1. Create a new Python file or open a Python interpreter.
  2. Assign an integer value to a variable named n.
  3. Use a for loop to iterate through the numbers from 2 to n (inclusive).
  4. Use an if statement to check if the current number is a factor of n.
  5. If the number is a factor, print it and use the break statement to exit the loop.

Your final code should look something like this:

n = 20

for i in range(2, n + 1):
    if n % i == 0:
        print("First factor:", i)
        break

Output:

First factor: 2

These exercises help you practice using the break and continue loop control statements in Python to control the flow of your programs and accomplish various tasks.

4.3: Loop Control (break, continue)

When working with loops, there may be situations where you need more control over the execution flow. Sometimes, you may want to terminate the loop prematurely or skip certain iterations of the loop based on certain conditions. 

In Python, you can use the break and continue statements to modify the behavior of for and while loops. The break statement allows you to terminate the loop prematurely when a certain condition is met. For example, if you are searching for a particular value in a list, you can use the break statement to terminate the loop as soon as the value is found, instead of continuing to iterate through the rest of the list.

On the other hand, the continue statement allows you to skip certain iterations of the loop based on a certain condition. For example, if you are iterating through a list of numbers and you only want to process the even numbers, you can use the continue statement to skip over the odd numbers.

In this topic, we will discuss the use of these loop control statements and how they can be used effectively in different scenarios. By the end of this topic, you should have a better understanding of how to use break and continue statements in your Python programs to achieve more precise control over the execution flow of your loops.

4.3.1 break Statement:

The break statement is used to exit a loop prematurely, i.e., before the loop's condition becomes false or before iterating over all items in a sequence. When the break statement is encountered inside a loop, the loop is terminated immediately, and the program continues executing the code following the loop. 

For example, let's say we want to search for a specific number in a list and stop the loop once the number is found:

numbers = [1, 2, 3, 4, 5]
search = 3

for number in numbers:
    if number == search:
        print("Found:", number)
        break

Output:

Found: 3

In this example, the loop stops iterating as soon as the search number is found, saving time and resources.

4.3.2 continue Statement:

The continue statement is used to skip the rest of the code inside a loop for the current iteration and jump to the next iteration. In other words, when the continue statement is encountered, Python skips the remaining code in the loop and moves to the next item in the sequence or evaluates the loop condition again.

For example, let's say we want to print all the numbers from 1 to 10, except for the multiples of 3:

for i in range(1, 11):
    if i % 3 == 0:
        continue
    print(i)

Output:

1
2
4
5
7
8
10

In this example, the loop skips printing the numbers that are divisible by 3 and continues with the next iteration.

In summary, loop control statements like break and continue can provide significant benefits in terms of control flow within loops. The break statement, for example, can be used to exit a loop prematurely if certain conditions are met. This can be particularly useful in situations where you want to stop iterating through a loop as soon as a specific condition is met, or if you want to exit the loop altogether.

Similarly, the continue statement can be used to skip over specific iterations of a loop, which can help to optimize loop performance by avoiding unnecessary computation. By incorporating these control structures into your loops, you can make your code more efficient and readable, while also gaining greater control over the way your programs execute.

Exercise 4.3.1: Print First Five Even Numbers

In this exercise, you will write a Python program that prints the first five even numbers using a for loop and the continue statement.

Instructions:

  1. Create a new Python file or open a Python interpreter.
  2. Use a for loop to iterate through the numbers from 1 to 10 (inclusive).
  3. Use an if statement to check if the current number is odd.
  4. If the number is odd, use the continue statement to skip the rest of the code in the loop and move on to the next iteration.
  5. Print the even number and increment a counter.
  6. When the counter reaches 5, use the break statement to exit the loop.

Your final code should look something like this:

counter = 0

for i in range(1, 11):
    if i % 2 != 0:
        continue
    print(i)
    counter += 1
    if counter == 5:
        break

Output:

2
4
6
8
10

Exercise 4.3.2: Sum of Positive Numbers

In this exercise, you will write a Python program that calculates the sum of all positive numbers in a list using a for loop and the continue statement.

Instructions:

  1. Create a new Python file or open a Python interpreter.
  2. Assign a list of integers (positive and negative) to a variable named numbers.
  3. Initialize a variable named total with the value 0.
  4. Use a for loop to iterate through the numbers in the list.
  5. Use an if statement to check if the current number is negative.
  6. If the number is negative, use the continue statement to skip the rest of the code in the loop and move on to the next iteration.
  7. Add the positive number to the total variable.
  8. Print the value of total after the loop is complete.

Your final code should look something like this:

numbers = [1, -2, 3, -4, 5, -6, 7]
total = 0

for number in numbers:
    if number < 0:
        continue
    total += number

print(total)

Output:

16

Exercise 4.3.3: Find the First Factor

In this exercise, you will write a Python program that finds the first factor of a given number using a for loop and the break statement.

Instructions:

  1. Create a new Python file or open a Python interpreter.
  2. Assign an integer value to a variable named n.
  3. Use a for loop to iterate through the numbers from 2 to n (inclusive).
  4. Use an if statement to check if the current number is a factor of n.
  5. If the number is a factor, print it and use the break statement to exit the loop.

Your final code should look something like this:

n = 20

for i in range(2, n + 1):
    if n % i == 0:
        print("First factor:", i)
        break

Output:

First factor: 2

These exercises help you practice using the break and continue loop control statements in Python to control the flow of your programs and accomplish various tasks.

4.3: Loop Control (break, continue)

When working with loops, there may be situations where you need more control over the execution flow. Sometimes, you may want to terminate the loop prematurely or skip certain iterations of the loop based on certain conditions. 

In Python, you can use the break and continue statements to modify the behavior of for and while loops. The break statement allows you to terminate the loop prematurely when a certain condition is met. For example, if you are searching for a particular value in a list, you can use the break statement to terminate the loop as soon as the value is found, instead of continuing to iterate through the rest of the list.

On the other hand, the continue statement allows you to skip certain iterations of the loop based on a certain condition. For example, if you are iterating through a list of numbers and you only want to process the even numbers, you can use the continue statement to skip over the odd numbers.

In this topic, we will discuss the use of these loop control statements and how they can be used effectively in different scenarios. By the end of this topic, you should have a better understanding of how to use break and continue statements in your Python programs to achieve more precise control over the execution flow of your loops.

4.3.1 break Statement:

The break statement is used to exit a loop prematurely, i.e., before the loop's condition becomes false or before iterating over all items in a sequence. When the break statement is encountered inside a loop, the loop is terminated immediately, and the program continues executing the code following the loop. 

For example, let's say we want to search for a specific number in a list and stop the loop once the number is found:

numbers = [1, 2, 3, 4, 5]
search = 3

for number in numbers:
    if number == search:
        print("Found:", number)
        break

Output:

Found: 3

In this example, the loop stops iterating as soon as the search number is found, saving time and resources.

4.3.2 continue Statement:

The continue statement is used to skip the rest of the code inside a loop for the current iteration and jump to the next iteration. In other words, when the continue statement is encountered, Python skips the remaining code in the loop and moves to the next item in the sequence or evaluates the loop condition again.

For example, let's say we want to print all the numbers from 1 to 10, except for the multiples of 3:

for i in range(1, 11):
    if i % 3 == 0:
        continue
    print(i)

Output:

1
2
4
5
7
8
10

In this example, the loop skips printing the numbers that are divisible by 3 and continues with the next iteration.

In summary, loop control statements like break and continue can provide significant benefits in terms of control flow within loops. The break statement, for example, can be used to exit a loop prematurely if certain conditions are met. This can be particularly useful in situations where you want to stop iterating through a loop as soon as a specific condition is met, or if you want to exit the loop altogether.

Similarly, the continue statement can be used to skip over specific iterations of a loop, which can help to optimize loop performance by avoiding unnecessary computation. By incorporating these control structures into your loops, you can make your code more efficient and readable, while also gaining greater control over the way your programs execute.

Exercise 4.3.1: Print First Five Even Numbers

In this exercise, you will write a Python program that prints the first five even numbers using a for loop and the continue statement.

Instructions:

  1. Create a new Python file or open a Python interpreter.
  2. Use a for loop to iterate through the numbers from 1 to 10 (inclusive).
  3. Use an if statement to check if the current number is odd.
  4. If the number is odd, use the continue statement to skip the rest of the code in the loop and move on to the next iteration.
  5. Print the even number and increment a counter.
  6. When the counter reaches 5, use the break statement to exit the loop.

Your final code should look something like this:

counter = 0

for i in range(1, 11):
    if i % 2 != 0:
        continue
    print(i)
    counter += 1
    if counter == 5:
        break

Output:

2
4
6
8
10

Exercise 4.3.2: Sum of Positive Numbers

In this exercise, you will write a Python program that calculates the sum of all positive numbers in a list using a for loop and the continue statement.

Instructions:

  1. Create a new Python file or open a Python interpreter.
  2. Assign a list of integers (positive and negative) to a variable named numbers.
  3. Initialize a variable named total with the value 0.
  4. Use a for loop to iterate through the numbers in the list.
  5. Use an if statement to check if the current number is negative.
  6. If the number is negative, use the continue statement to skip the rest of the code in the loop and move on to the next iteration.
  7. Add the positive number to the total variable.
  8. Print the value of total after the loop is complete.

Your final code should look something like this:

numbers = [1, -2, 3, -4, 5, -6, 7]
total = 0

for number in numbers:
    if number < 0:
        continue
    total += number

print(total)

Output:

16

Exercise 4.3.3: Find the First Factor

In this exercise, you will write a Python program that finds the first factor of a given number using a for loop and the break statement.

Instructions:

  1. Create a new Python file or open a Python interpreter.
  2. Assign an integer value to a variable named n.
  3. Use a for loop to iterate through the numbers from 2 to n (inclusive).
  4. Use an if statement to check if the current number is a factor of n.
  5. If the number is a factor, print it and use the break statement to exit the loop.

Your final code should look something like this:

n = 20

for i in range(2, n + 1):
    if n % i == 0:
        print("First factor:", i)
        break

Output:

First factor: 2

These exercises help you practice using the break and continue loop control statements in Python to control the flow of your programs and accomplish various tasks.