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.
Examples
- A
breakis like stopping a game early if you win, whilecontinueskips the current turn but keeps playing. - If you're eating candies in a bag,
breakmeans you stop when you find your favorite, andcontinuemakes you skip the sour ones.
Ask a question
See also
- How Does Python break continue pass ⛔ Work?
- How Does C++ while loops explained 🔃 Work?
- How Does While loops in Python are easy! ♾️ Work?
- What is This results in printing numbers from?
- What are infinite loops?