If you are interested to learn about the for loop java
Java Break
You have already seen the break
statement used in an earlier chapter of this tutorial. It was used to “jump out” of a switch
statement. The break
statement can also be used to jump out of a loop. The break statement in java is used to terminate from the loop immediately. When a break statement is encountered inside a loop, the loop iteration stops there, and control returns from the loop immediately to the first statement after the loop. Basically, break statements are used in situations when we are not sure about the actual number of iteration for the loop, or we want to terminate the loop based on some condition. This example stops the loop when i is equal to 4:
Syntax :
break;
Example
for (int i = 0; i < 10; i++) { if (i == 4) { break; } System.out.println(i); }
In Java, a break statement is majorly used for:
- To exit a loop.
- Used as a “civilized” form of goto.
- Terminate a sequence in a switch statement.
Using break as a Form of Goto
Java does not have a goto statement because it provides a way to branch in an arbitrary and unstructured manner. Java uses a label. A Label is used to identify a block of code.
Syntax:
label:
{
statement1;
statement2;
statement3;
.
.
}
Now, the break statements can be used to jump out of the target block. We cannot break to any label which is not defined for an enclosing block.
Syntax:
break label;
Example:
// Java program to demonstrates using break with goto class GFG { public static void main(String args[]) { // First label first: for (int i = 0; i < 3; i++) { // Second label second: for (int j = 0; j < 3; j++) { if (i == 1 && j == 1) { // Using break statement with label break first; } System.out.println(i + " " + j); } } } }
Output
0 0
0 1
0 2
1 0
Using break to terminate a sequence in a switch statement.
The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. The break statement is used inside the switch to terminate a statement sequence. The break statement is optional. If omitted, execution will continue on into the next case.
Syntax:
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}
Example
// Java program to demonstrate using break to terminate a // sequence in a switch statement. class GFG { public static void main(String args[]) { int i = 2; switch (i) { case 0: System.out.println("i is zero."); break; case 1: System.out.println("i is one."); break; case 2: System.out.println("i is two."); break; default: System.out.println("Invalid number"); } } }
Output
i is two.
Java Continue
The continue
statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop. The continue statement in Java is used to skip the current iteration of a loop. We can use continue statement inside any types of loops such as for, while, and do-while loop. Basically continue statements are used in the situations when we want to continue the loop but do not want the remaining statement after the continue statement. This example skips the value of 4:
Example
for (int i = 0; i < 10; i++) { if (i == 4) { continue; } System.out.println(i); }
Syntax:
continue;
Using continue to continue a loop
Using continue, we can skip the current iteration of a loop and jumps to the next iteration of the loop immediately.
Example:
// Java program to demonstrates the continue // statement to continue a loop class GFG { public static void main(String args[]) { for (int i = 0; i < 10; i++) { // If the number is 2 // skip and continue if (i == 2) continue; System.out.print(i + " "); } } }
Output
0 1 3 4 5 6 7 8 9
Break and Continue in While Loop
You can also use break
and continue
in while loops:
Break Example
int i = 0; while (i < 10) { System.out.println(i); i++; if (i == 4) { break; } }
Continue Example
int i = 0; while (i < 10) { if (i == 4) { i++; continue; } System.out.println(i); i++; }
Difference between break and continue:
Break | Continue |
---|---|
The break statement is used to terminate the loop immediately. | The continue statement is used to skip the current iteration of the loop. |
break keyword is used to indicate break statements in java programming. | continue keyword is used to indicate continue statement in java programming. |
We can use a break with the switch statement. | We can not use a continue with the switch statement. |
The break statement terminates the whole loop early. | The continue statement brings the next iteration early. |
It stops the execution of the loop. | It does not stop the execution of the loop. |