In the process of learning the Java language yourself, it is easy to confuse the use of break and continue. In order to facilitate quick access and review, this special stay to learn a note.
Brief
in the main part of any iteration statement, you can use break and continue to control the loop flow. Where the break is used to force out the loop and does not execute the remaining statements in the loop. Instead, continue stops executing the current iteration and then returns to the beginning of the loop and begins the next iteration.
Source Code
The following procedure shows you the examples of break and continue in the for and while loops:
Package com.mufeng.thefourthchapter;
public class Breakandcontinue {public
static void Main (string[] args) {for
(int i = 0; i < i++) {
if (i = = 74) {//out of For loop break
;
}
if (i% 9!= 0) {//Next iteration
continue;
}
System.out.print (i + "");
}
System.out.println ();
int i = 0;
while (true) {
i++;
Int j = i *;
if (j = = 1269) {//out of the loop break
;
}
if (i%!= 0) {//Top of Loop
continue;
}
System.out.print (i + "");}}
Output results
01.0 9 18 27 36 45 54 63 72
02.10 20 30 40
Source Analysis
In this for loop, the value of I will never reach 100, because once I arrives at the 74,break statement, it interrupts the loop. Typically, you use a break only if you do not know when the interrupt condition will be met. As long as I cannot be divisible by 9, the Continue statement causes the execution to return to the very beginning of the loop (which increments the I value). If it is divisible, the value is displayed. The output shows 0 because the 0%9 equals 0.
Finally, you can see an "infinite while loop" situation. However, there is a break statement inside the loop, which aborts the loop. In addition, you will see that the continue statement execution sequence moves back to the beginning of the loop without completing the contents of the Continue statement. (The value is printed only if I can be divisible by 10.) )
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.