Continue Statement
The continue statement is used for the termination of the current iteration and not the whole loop. The continue statement can include an option optional label that allows the program to jump to the next iteration of a labeled loop statement instead of the current loop. In case, the continue statement needs to be nested within this labeled statement. Leftover iterations are executed even if the continue keyword is encountered in a loop. Continue can never be used with the switch and label statements and is used only with the loops. Continue statement in any loop resumes the control to the next iteration once encountered.
Facts About Continue
- A continue can appear only in loop (for, while, do) statements.
- Continue is not used to terminate the execution of loop. Continue causes early execution of the next iteration of the enclosing loop.
- The continue statement can appear only in loops. You will get an error if this appears in switch statement.
- Continuing statement inside the nested loop allows the skip of the current iteration and execution of the next iteration of the innermost loop.
- When a continue statement is encountered, it gets the control to the next iteration of the loop.
- A continue statement does not terminate the loop; it causes the loop to go to the next iteration. All iterations of the loop are executed even if continue is encountered. The continue statement is used to skip statements in the loop that appear after the continue
- Break keyword allows us to move out of the loop, it does not allow the continuation of the loop.
Break Statement
Break statement or break keyword is used to terminate and transfer the control to the next statement when encountered inside a loop or switch case statement. Whenever a break statement is encountered, execution of that loop or switch statement ends abruptly. A break statement is used in various loops like for, while, do while, foreach loop and the switch case statement. Upcoming statements or leftover iterations are not executed after the break statement is encountered in a loop. Break statement in any loop, switch and label do not resume the execution of iterations once encountered.
Facts About Break
- A break can appear in both switch and loop (for, while, do) statements.
- Break is used to terminate the execution of the enclosing loop.
- The break statement can be used in both switch and loop statements.
- A break statement inside the nested loop allows the termination of the innermost loop and the control remains inside the outermost loop (it will not affect the outmost loop).
- When a break statement is encountered, it terminates the block and gets the control out of the switch or loop.
- A break causes the switch or loop statements to terminate the moment it is executed. Loop or switch ends abruptly when break is encountered.
- The continue keyword allows the continuation of the same loop.
Also Read: Difference Between If-else And Switch Case
Difference Between Break And Continue Statement In Tabular Form
CONTINUE STATEMENT | BREAK STATEMENT |
A continue can appear only in loop (for, while, do) statements. | A break can appear in both switch and loop (for, while, do) statements. |
Continue is not used to terminate the execution of loop. Continue causes early execution of the next iteration of the enclosing loop. | Break is used to terminate the execution of the enclosing loop. |
The continue statement can appear only in loops. You will get an error if this appears in switch statement. | The break statement can be used in both switch and loop statements. |
Continuing statement inside the nested loop allows the skip of the current iteration and execution of the next iteration of the innermost loop. | A break statement inside the nested loop allows the termination of the innermost loop and the control remains inside the outermost loop (it will not affect the outmost loop). |
When a continue statement is encountered, it gets the control to the next iteration of the loop. | When a break statement is encountered, it terminates the block and gets the control out of the switch or loop. |
A break causes the switch or loop statements to terminate the moment it is executed. Loop or switch ends abruptly when break is encountered. | A break causes the switch or loop statements to terminate the moment it is executed. Loop or switch ends abruptly when break is encountered. |
Break keyword allows us to move out of the loop, it does not allow the continuation of the loop. | The continue keyword allows the continuation of the same loop. |