Output
1
2
34
When i = 5, the break statement executes and the loop stops completely.
Continue Statement
The continue statement skips the current iteration and continues with the next iteration.
Example
Output
12
4
5
When i = 3, continue skips that iteration, so 3 is not printed.
Break vs Continue
| break | continue |
|---|---|
| Stops the loop completely | Skips the current iteration |
| Control moves outside the loop | Control moves to the next iteration |
| Used when we want to terminate the loop | Used when we want to skip a particular case |
Key Takeaways
- break completely terminates a loop.
- continue skips the current iteration.
- Use break when you no longer need the loop to continue.
- Use continue when you want to skip a particular iteration.