Darla SandyKnowledge Contributor
How does the 'break' statement work in Java loops?
How does the 'break' statement work in Java loops?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
The ‘break’ statement in Java is used to terminate the execution of a loop prematurely. When encountered inside a loop (such as ‘for’, ‘while’, or ‘do-while’), the ‘break’ statement immediately exits the loop and continues executing the code after the loop. It effectively jumps out of the loop’s block, bypassing any remaining iterations.
1. for Loop:
“`java
for (int i = 0; i < 5; i++) {
if (i == 3) {
break; // Exit the loop when i equals 3
}
System.out.println(i);
}
“`
2. while Loop:
“`java
int i = 0;
while (i < 5) {
if (i == 3) {
break; // Exit the loop when i equals 3
}
System.out.println(i);
i++;
}
“`
3. do-while Loop:
“`java
int i = 0;
do {
if (i == 3) {
break; // Exit the loop when i equals 3
}
System.out.println(i);
i++;
} while (i < 5);
“`