How does 'continue' differ from 'break'?

QuestionsQuestions8 SkillsProPython Control StructuresOct, 01 2025
092

The continue and break statements are both used to control the flow of loops, but they serve different purposes:

continue

  • Purpose: Skips the rest of the current iteration and moves to the next iteration of the loop.
  • Effect: The loop continues running, but the code following the continue statement for that iteration is not executed.
  • Use Case: Useful when you want to skip certain iterations based on a condition without exiting the loop.

Example of continue

for number in range(1, 6):
    if number == 3:
        continue  # Skip the iteration when number is 3
    print(number)

Output:

1
2
4
5

break

  • Purpose: Exits the loop entirely, regardless of the iteration count.
  • Effect: The loop stops running, and control is transferred to the statement immediately following the loop.
  • Use Case: Useful when you want to terminate the loop based on a condition.

Example of break

for number in range(1, 6):
    if number == 3:
        break  # Exit the loop when number is 3
    print(number)

Output:

1
2

Summary

  • continue: Skips to the next iteration of the loop.
  • break: Exits the loop entirely.

If you have any further questions or need more examples, feel free to ask!

0 Comments

no data
Be the first to share your comment!