End the loop when i
is equal to 4:
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
System.out.println(i);
}
The break
keyword is used to break out a for
loop, a while
loop or a switch
block.
Break out of a while loop:
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
if (i == 4) {
break;
}
}
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.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!