Break, continue, return usage in a looping statement

Source: Internet
Author: User
Tags terminates
Using the break exit loop, you can forcibly exit the loop using the break statement, ignoring any other statements and loop condition tests in the loop body. When a break statement is encountered in a loop, the loop is terminated and program control restarts after the loop statement. For example, if the For loop is designed to execute from 0 to 99, then output 0 to 99 of these numbers, but when I equals 10 o'clock, the break statement terminates the program. So the program will only output 0 to 10. When you use the break statement in a series of nested loops, it simply terminates the innermost loop.
The continue then stops executing the current iteration, then returns to the start of the loop and begins a new iteration. Continue is used to skip a portion of a statement in the loop body, that is, not execute this part of the statement

The return statement is used to explicitly return from a method. That is, the return statement causes the program control to return to the method that called it. Therefore, it is categorized as a jump statement.

Although the return statement is discussed in detail at the beginning of chapter 7th, it is briefly described here. At any time in a method, the return statement can be used to make the executing branch return to the method that called it.

The following example illustrates this point. In the following example, because the Java runtime calls main (), the return statement causes the program execution to return to the Java runtime System.

Demonstrate return. Class Return {public static void main (String args[]) {Boolean t = true; System.out.println ("Before the Return"); if (t) return; Return to caller System.out.println ("This won ' t execute."); The program results are as follows: Before the return. As you can see, the last println () statement was not executed. Once the return statement is executed, program control is passed to its caller. The last point: in the above program, the IF (t) statement is necessary. Without it, the Java compiler will mark the unreachable code error because the compiler knows that the last println () statement will never be executed. To prevent this error, the example can be executed, where the IF statement is used to "deceive" the compiler.


How do I encounter 2 for,break only to exit the nearest for?

Not absolutely, if there is no label, break is the loop where it ends. If there is a label, for example:

Outer

for () {

Inner

for () {

Break outer;

}}

This break can end two loops (labels are only used when nesting)


In MATLAB, the break command forces the inner for or while statement containing the break to be forced to terminate, immediately out of the structure, executing the command after end, and the break command is generally used in conjunction with the IF structure.


2 for loop nesting, the inner loop has an if end selection structure, if there is a break, then break jumps out of the inner for Loop into the outer for.

The If is usually not required to "jump out".

Suppose we have a statement that jumps out if it's called Breakif.
So if your program looks like this,
If a
Statement 1;
Breakif;
Statement 2;
Else b
End
Then Statement2 is meaningless here, because it never executes.

And if you erase Statement2, write
If a
Statement 1;
Breakif;
Else b
End
Then Breakif is superfluous, because when the statement1 is executed, it has already exited if.

So, if you find yourself needing to use this BREAKIF statement, you should first consider whether the structure of your own program can be improved.

In addition, the function of break is to end (or "jump") "Current layer loop" so that the next executed statement is the first statement after the loop. The word "cycle", mentioned on the 8 floor, is estimated to mean the same meaning, but in the case of multi-layered loops, the word may be misunderstood as the "outermost loop" (the largest loop).

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.