The Break Statement
- The break statement can be used to jump out of a loop even when the condition is true.
- The break statement breaks the loop and continues executing all the code after the loop(if any):
OUTPUT
The number is: 0 The number is: 1 The number is: 2 The number is: 3 The number is: 4 The number is: 5
This time though, we want to stop execution of the loop when i become 3. To do so, we are inserting another conditional checking i, and if the test passes, we will break execution of the loop:
OUTPUT
The number is: 0 The number is: 1 The number is: 2
The Continue Statement
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
OUTPUT
The number is: 0 The number is: 1 The number is: 2 The number is: 4 The number is: 5