The break statement immediately exits the loop, skipping any remaining iterations. Execution jumps to the first line after the loop.
for (int i = 0; i < 100; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
System.out.println("loop ended");
This prints through , then "loop ended." When i reaches , break fires and the loop is done. The numbers through are never printed.
Use break when you're searching for something and want to stop as soon as you find it. Without break, you'd waste time checking the remaining elements even after you have your answer.