Java break and continue Statements
Java break and continue statements are used to manage program flow. We can
use them in a loop to control loop iterations. These statements let us to control
loop and switch statements by enabling us to either break out of the loop or jump
to the next iteration by skipping the current loop iteration.
In this tutorial, we will discuss each in details with examples.
Break Statement
In Java, break is a statement that is used to break current
execution flow of the program.
We can use break statement inside loop, switch case etc.
If break is used inside loop then it will terminate the loop.
If break is used inside the innermost loop then break will terminate
the innermost loop only and execution will start from the outer loop.
If break is used in switch case then it will terminate the execution after
the matched case. Use of break, we have covered in our switch case
topic.
Syntax:
jump-statement;
break;
Data Flow Diagram of break statement
, In this example, we are using break inside the loop, the loop will terminate when
the value is 8.
public class BreakDemo1 {
public static void main(String[] args) {
for(inti=1;i<=10;i++){
if(i==8){
break;
}
System.out.println(i);
Java break and continue statements are used to manage program flow. We can
use them in a loop to control loop iterations. These statements let us to control
loop and switch statements by enabling us to either break out of the loop or jump
to the next iteration by skipping the current loop iteration.
In this tutorial, we will discuss each in details with examples.
Break Statement
In Java, break is a statement that is used to break current
execution flow of the program.
We can use break statement inside loop, switch case etc.
If break is used inside loop then it will terminate the loop.
If break is used inside the innermost loop then break will terminate
the innermost loop only and execution will start from the outer loop.
If break is used in switch case then it will terminate the execution after
the matched case. Use of break, we have covered in our switch case
topic.
Syntax:
jump-statement;
break;
Data Flow Diagram of break statement
, In this example, we are using break inside the loop, the loop will terminate when
the value is 8.
public class BreakDemo1 {
public static void main(String[] args) {
for(inti=1;i<=10;i++){
if(i==8){
break;
}
System.out.println(i);