Example
End the loop when i is equal to 4:
java
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
System.out.println(i);
}
Definition and Usage
The break keyword is used to break out a for loop, a while loop or a switch block.
More Examples
Example
Break out of a while loop:
java
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
if (i == 4) {
break;
}
}
Related Pages
Use the continue keyword to end the current iteration in a loop, but continue with the next.