PythonPython Control StructuresJump Statements -Break/Continue/Pass

Jump Statements: break, continue, and pass

Jump statements are special keywords in Python that alter the normal flow of a loop. They give you the power to jump out of a loop, skip part of a loop, or do nothing at all. Mastering break, continue, and pass will allow you to write more efficient and readable code.


The Three Jump Statements

break: Exiting a Loop Early

The break statement immediately terminates the innermost loop it is in. As soon as break is executed, the program’s control jumps to the very next statement after the loop.

It’s most often used to stop a loop when a specific condition is met, saving unnecessary iterations.

When to Use break:

  • When you find the item you are searching for.
  • When an error condition occurs that makes continuing the loop pointless.
  • When the user signals they want to quit.

Example: Finding the First Multiple of 7

Imagine you want to find the first number divisible by 7 in a list. Once you find it, there’s no need to check the rest.


Pyground

Find the first number divisible by 7 in a list and stop.

Expected Output:

Checking 1...
Checking 12...
Checking 19...
Checking 21...
Found it! 21 is divisible by 7.
Loop finished.

Output:



break vs. continue: A Head-to-Head Comparison

Featurebreakcontinue
ActionTerminates the entire loop.Skips the current iteration.
Control FlowJumps to the code after the loop.Jumps to the next iteration of the loop.
AnalogyHitting the “stop” button on a music player.Hitting the “next track” button.

Visual Example


Pyground

Demonstrate the difference between break and continue when the number 3 is encountered in a loop from 1 to 5.

Expected Output:

--- Using break ---
1
2
Breaking loop

--- Using continue ---
1
2
Skipping iteration
4
5

Output:


💡

Understanding when to use break and continue is key to writing clean and efficient loops. Use break to save time when a result is found, and use continue to simplify logic by filtering out items you don’t need to process.