break and continue in Java


Use break to exit loops and continue to skip to the next iteration.

Source Code

for (int i = 0; i < 10; i++) {
    if (i == 4) {
        break; // Exits the loop
    }
    if (i == 2) {
        continue; // Skips the current iteration
    }
    System.out.println(i);
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments