FAQ: Loops - Loop Control: Break

This community-built FAQ covers the “Loop Control: Break” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Scientist: Analytics Specialist
Computer Science
Analyze Financial Data with Python
Build Chatbots with Python
Build Python Web Apps with Flask
Learn Data Analysis for your Business
Machine Learning/AI Engineering Foundations
Data Engineer
Data Scientist: Natural Language Processing Specialist
Data Science Foundations
Data Scientist: Inference Specialist
Data Scientist: Machine Learning Specialist
Business Intelligence Data Analyst

CS101 Livestream Series
Learn Python 3

FAQs on the exercise Loop Control: Break

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Here is the code that I am trying to make sense of:

dog_breeds_available_for_adoption = ["french_bulldog", "dalmatian", "shihtzu", "poodle", "collie"]
dog_breed_I_want = "dalmatian"

for dog_breed in dog_breeds_available_for_adoption:
  print(dog_breed)
  if dog_breed == dog_breed_I_want:
    break
  print("They have the dog I want!")

During the second loop iteration, I assumed that the dog_breed (dalmatian) would print first, and then the loop would break because of the if statement. And then after this break, They have the dog I want! would print as the final output because it is the final line of code. Here is what I expected to see:

french_bulldog
dalmation
They have the dog I want!

But the output is actually:

french_bulldog
They have the dog I want!
dalmatian

Why the flip flop between the output’s second and third lines? The order of my code clearly shows that the dog_breed should always print first.

Unlike some other languages, indentation is very important in Python.

# Code:
for dog_breed in dog_breeds_available_for_adoption:
  print(dog_breed)
  if dog_breed == dog_breed_I_want:
    break
  print("They have the dog I want!")

# Output:
# "french_bulldog"
# "They have the dog I want!"
# "dalmatian"

In the first iteration of the loop, the loop variable dog_breed is assigned the string "french_bulldog". The first print statement outputs this string. The condition is false, so we don’t break. The second print statement is also in the loop’s body (because it has the same indentation as the other statements in the loop’s body). Therefore, the string "They have the dog I want!" is printed and the first iteration ends.
In the second iteration, dog_breed is "dalmatian". It is printed. The if condition is true, so we break out the of the loop.

Contrast this with the following:

# Code:
for dog_breed in dog_breeds_available_for_adoption:
  print(dog_breed)
  if dog_breed == dog_breed_I_want:
    break
print("They have the dog I want!")

# Output:
# "french_bulldog"
# "dalmatian"
# "They have the dog I want!"

In the first iteration, dog_breed is "french_bulldog". The string is printed. The condition is false, so we don’t break. There are no more statements in the loop’s body, so the first iteration ends.
In the second iteration, dog_breed is "dalmatian". The condition is true, so we break out of the loop.
Once the loop has ended, the final print statement is executed and the string "They have the dog I want!" is printed. This print statement is outside the loop (notice the indentation level).

If we had picked a dog_breed_I_want which wasn’t present in the dog_breeds_available_for_adoption list, then all the breeds would have been printed by the loop. We wouldn’t break out of the loop. Once the whole list was traversed, the loop would end and then the final print statement (which is outside the loop) would print. It would be a misleading print because they don’t have the desired dog. We would have to edit our code to avoid that, but for the purpose of the exercise, they are just giving a demonstration of how break statements work.

1 Like