Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn break/continue in a Nested Loop | Nested Loops
Python Loops Tutorial
course content

Course Content

Python Loops Tutorial

Python Loops Tutorial

1. The for Loop
2. The while Loop
3. Nested Loops
4. List and Dictionary Comprehensions

book
break/continue in a Nested Loop

Let's apply the concepts of break and continue to analyze travel costs practically. We'll combine a while loop and a for loop to process expenses across multiple trips.

Example: break in Mixed Nested Loops

Imagine you have multiple trips, and each trip has a list of expenses. If any expense exceeds a specific budget threshold, we will stop processing that trip immediately.

123456789101112131415161718192021222324
# List of trips with their respective expenses travel_costs = [ [100, 150, 300, 50], # Trip 1 [200, 500, 100, 80], # Trip 2 [120, 180, 400, 150] # Trip 3 ] # Budget threshold budget = 200 # Outer while loop to iterate through trips i = 0 while i < len(travel_costs): print(f"Processing expenses for Trip {i + 1}:") # Inner for loop to iterate through expenses for cost in travel_costs[i]: if cost > budget: # If expense exceeds the budget, stop processing the current trip print(f"Expense {cost} exceeds the budget. Stopping this trip.") break print(f"Expense: ${cost}") i += 1 # Move to the next trip print("") # Add a new line for readability
copy

Explanation:

  • Outer loop: iterates through the list of trips using the index i;
  • Inner loop: processes each expense in the current trip;
  • break in the inner loop: if an expense exceeds the budget, the break statement stops processing expenses for the current trip.
Task
test

Swipe to begin your solution

Find the first significant expense. Write a program to:

  • Iterate through each trip's expenses.
  • Stop at the first significant expense greater than $200 using break.
  • Skip expenses less than $100 using continue.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 4
toggle bottom row

book
break/continue in a Nested Loop

Let's apply the concepts of break and continue to analyze travel costs practically. We'll combine a while loop and a for loop to process expenses across multiple trips.

Example: break in Mixed Nested Loops

Imagine you have multiple trips, and each trip has a list of expenses. If any expense exceeds a specific budget threshold, we will stop processing that trip immediately.

123456789101112131415161718192021222324
# List of trips with their respective expenses travel_costs = [ [100, 150, 300, 50], # Trip 1 [200, 500, 100, 80], # Trip 2 [120, 180, 400, 150] # Trip 3 ] # Budget threshold budget = 200 # Outer while loop to iterate through trips i = 0 while i < len(travel_costs): print(f"Processing expenses for Trip {i + 1}:") # Inner for loop to iterate through expenses for cost in travel_costs[i]: if cost > budget: # If expense exceeds the budget, stop processing the current trip print(f"Expense {cost} exceeds the budget. Stopping this trip.") break print(f"Expense: ${cost}") i += 1 # Move to the next trip print("") # Add a new line for readability
copy

Explanation:

  • Outer loop: iterates through the list of trips using the index i;
  • Inner loop: processes each expense in the current trip;
  • break in the inner loop: if an expense exceeds the budget, the break statement stops processing expenses for the current trip.
Task
test

Swipe to begin your solution

Find the first significant expense. Write a program to:

  • Iterate through each trip's expenses.
  • Stop at the first significant expense greater than $200 using break.
  • Skip expenses less than $100 using continue.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt