Please note, this is a STATIC archive of website www.w3schools.com from 05 May 2020, cach3.com does not collect or store any user information, there is no "phishing" involved.
THE WORLD'S LARGEST WEB DEVELOPER SITE

Java break Keyword

❮ Java Keywords


Example

End the loop when i is equal to 4:

for (int i = 0; i < 10; i++) {
  if (i == 4) {
    break;
  }
  System.out.println(i);
}

Run example »


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:

int i = 0;
while (i < 10) {   System.out.println(i);   i++;   if (i == 4) {     break;   } }

Run example »


Related Pages

Use the continue keyword to end the current iteration in a loop, but continue with the next.

Read more about for loops in our Java For Loops Tutorial.

Read more about while loops in our Java While Loops Tutorial.

Read more about break and continue in our Java Break Tutorial.


❮ Java Keywords