Break and Continue Statement in Java
Break and Continue Statement in Java
Examples
In this tutorial, you will learn about the Break and Continue statement in
Java with
the help of examples.
Java has some jump statements, which shift the program flow to another part of
the program.
Normally, after the body of a loop has been entered, all the
statements in the body are executed before doing the
next loop test.
The break
and continue statements are used to skip part of a loop or even terminate it
before the
predetermined condition, depending on tests made on the body of the
loop.
It can be also be used to handle errors or some exceptional conditions.
1. Break Statement :
When we use the break statement, the program control resumes at the next
statement following the loop. If the
break statement is inside nested loops, it
brings the control out of the loop containing it.
Syntax :
break;
// Java Program to
import java.io.*;
if (i == 8) {
break;
}
System.out.println("Number is : " + i);
Output :
Number is : 0
Number is : 1
Number is : 2
Number is : 3
Number is : 4
Number is : 5
Number is : 6
Number is : 7
In the above example, the loop is terminated when the value of i is 8. Otherwise,
Its value is further processed
in the
loop.
break label;
import java.io.*;
if (i == 3) {
break a;
}
}
Output :
1 1
1 2
2 1
2 2
2. Continue Statement :
Continue statement is a branching statement. It is used to continue the loop.
When encountered, it causes the rest of the iteration to be skipped and the next
iteration starts.
The continue statement is almost always used in decision-making
statements.
We can use Java continue statement in all types of loops such as for
loop, while loop, and do-while loop.
Syntax :
continue;
import java.io.*;
int i;
if (i == 3) {
continue;
}
System.out.println(i);
Output :
The value 4 is missing in the output, because when the value of variable i is 3,
then the program executes a
continue statement, which makes it jump at the
beginning of for loop for the next iteration, skipping the
statements for the
current iteration.
import java.util.Scanner;
int i = 1, j = 1;
{
if (j == 3) {
j++;
continue;
j++;
}
i++;
Output :
Outer_Loop: 1
Inner_Loop: 1
Inner_Loop: 2
Inner_Loop: 4
Inner_Loop: 5
Outer_Loop: 2
Outer_Loop: 3
Outer_Loop: 4
Outer_Loop: 5
Here, the First Outer Loop condition will be checked, if " i " is greater than or
equal to 5 then the loop
will terminate and
come out of the loop.
If the value of " i " is less than equal to 5 then control goes inside the loop
and print the " i " value
after
that check for the condition in 2nd while loop
if it is true then goes inside the loop and execute statements
otherwise come
out of the loop and continue until 1st while loop becomes false.
In the 2nd while loop we used the "if" condition inside that we used the
continue statement. In case of an
inner loop, it
continues the inner loop only
and then outer loop. So when the value of "j == 3" then the value of "j" is
increased and
the continue statement is executed and skips the iteration of
the inner loop. Hence, the text Inner_Loop: 3 is
skipped from the output.
continue label;
import java.util.Scanner;
// inner loop
if (i == 2 && j == 2) {
continue hashcode;
}
}
Output :
i = 1 j = 1
i = 1 j = 2
i = 1 j = 3
i = 2 j = 1
i = 3 j = 1
i = 3 j = 2
i = 3 j = 3
i = 4 j = 1
i = 4 j = 2
i = 4 j = 3
Previous Tutorial:
Loops in Java
Subscribe
Email : support@hashcodec.com
Tutorials
C Programming
Java Programming
Python Programming
Javascript Programming
Company
About Us
FAQ