Java Beginner: Continue and break

Source: Internet
Author: User

We've already talked about two kinds of loops in Java: For and while loops, this time we'll talk about two special operations on these two loops: Continue and break. In English, continue is the continuation of meaning, and break is the meaning of interruption. In fact, in Java, these two operations are also the meaning of the following, we introduce each of these two operations.

First of all, continue, when the word appears in the loop, meaning is no longer perform the continue after the operation, but to go to the beginning of the cycle where the conditions to judge, if possible, then a new round of the cycle, or exit the loop. The loop here may be a for loop or a while loop. Now suppose we have a while loop in this form:

while (condition 1)

{

Statement 1;

if (condition 2)

{

Statement 2;

Continue

}

Statement 3;

}

We assume that at the beginning of the condition 1 is true, we enter the while loop, first executes the statement 1, and then we judge the condition 2, assuming that the condition 2 is true, then will enter if inside, will execute statement 2, and then execute contnue;

And then what? Note that, due to the relationship between the continue, the program will jump directly to the back of the while after the parentheses to determine the condition 1, if the condition 1 or true, we enter the while, execute statement 1, and then determine the condition 2, if the condition 2 is false, then do not enter the IF statement , so we will not run continue, then we will run statement 3 and then go back to the parentheses after the while to judge the condition 1 ....

I think it is not difficult for the reader to find out at this point that if condition 2 is always true, then statement 3 will never be executed. We are not here for the moment to discuss the practical significance of such operations, as long as the first to understand the principle of continue good. In practical programming, the reader can use the continue characteristics flexibly. (sometimes continue can replace if-else statements in loops, sometimes to circumvent certain actions in certain situations, such as the possibility of using linked lists and tree operations.) )

Similarly, if continue appears in the For Loop, it works the same way, and here is no longer a repeat.


So, what about break? Break is relatively simple, break is the meaning of forced exit, when a break appears in a loop and executed to, then will be forced to quit the loop, remember, is mandatory, unconditional exit. Now suppose we have a for loop like this:

For (...)

{

Statement 1;

Break

Statement 2;

}

Statement 3;

Then, when the program executes to break, it will jump out of the for loop and execute statement 3, of course, Statement 2 will not be executed.

Next, let's write a small program to verify continue and break, in this program we have a random number between 0 and 100, then we use the For loop, I from 0 to 100, if I is that random number, then jump out of the entire loop, if I is even, then print out, If it is odd, then skip. Here are the program code and running results:

Note that because we use random numbers, so each time the result of the operation is different, that is normal, the difference is that the random number is different, so the program jumps out of the time is also different, the extreme situation is that our random number is 0, then will not print out any number, directly out of the loop. Only processing completely are displayed on the screen.

The above small example can basically explain the characteristics of continue and break. In fact, some programmers do not agree with the use of these two operations, think that this will undermine the readability of the program and create a logical confusion, but in my rookie, reasonable use of continue and break, can sometimes simplify the code, and can write more sophisticated programs. In short, the benevolent see, in the Future program design, everyone has their own feelings.

Java Beginner: Continue and break

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.