How Does C++ break & continue explained 💔 Work?

C++ uses break and continue to control how loops behave, like a kid deciding when to stop playing or skip a turn in a game.

Imagine you're playing a game where you take turns picking toys from a pile. Each toy is a step in a loop. Now, if you pick a toy you don’t like, you might say "continue", that means you skip the rest of what you were doing for that turn and go to the next one.

But if you find your favorite toy, you might shout "break!", that means you stop playing right there and go do something else entirely, like eat ice cream.

How Break Works

When you use break, it's like saying "I'm done with this game!" The loop stops immediately. No more turns, no more toys, just a happy ending for the moment.

How Continue Works

When you use continue, it’s like saying "This turn is boring, I’ll skip it and go to the next one!" You don’t stop playing, but you move on to the next step in the loop, ignoring whatever was happening this time around. C++ uses break and continue to control how loops behave, like a kid deciding when to stop playing or skip a turn in a game.

Imagine you're playing a game where you take turns picking toys from a pile. Each toy is a step in a loop. Now, if you pick a toy you don’t like, you might say "continue", that means you skip the rest of what you were doing for that turn and go to the next one.

But if you find your favorite toy, you might shout "break!", that means you stop playing right there and go do something else entirely, like eat ice cream.

How Break Works

When you use break, it's like saying "I'm done with this game!" The loop stops immediately. No more turns, no more toys, just a happy ending for the moment.

How Continue Works

When you use continue, it’s like saying "This turn is boring, I’ll skip it and go to the next one!" You don’t stop playing, but you move on to the next step in the loop, ignoring whatever was happening this time around.

Take the quiz →

Examples

  1. A break is like stopping a game early if you win, while continue skips the current turn but keeps playing.
  2. In a loop counting from 1 to 10, break stops at 5, and continue skips even numbers.
  3. If you're eating candies in a bag, break means you stop when you find your favorite, and continue makes you skip the sour ones.

Ask a question

See also

Discussion

Recent activity

Categories: Science · C++· loops· control flow