Goto and continue in JAVA, break implementation Goto__java

Source: Internet
Author: User
Tags goto
The GOTO keyword appears early in the programming language. In fact, Goto is the ancestor of the program control structure of assembly language: "If condition A,
Jump here, or jump there. " If you read the assembly code generated by almost any compiler, you will find that the program control contains many
Jump. However, Goto is at the level of the source jump, so incur a bad reputation. If the program always jumps from one place to another
Side, there is also any way to identify the process of the code. With the advent of Edsger Dijkstra's famous "Goto evil" theory, goto
Disfavor.
In fact, the real problem is not the use of Goto, but the abuse of Goto. And in some rare cases, Goto is the organizational control
The best means of the process.
Although Goto is still a reserved word in Java, it is not formally used in the language; Java has no goto. However, at break and
Continue these two keywords, we can still see some of the shadow of Goto. It does not belong to a jump, but interrupts the looping language
A method of a sentence. They are included in the goto question because they use the same mechanism: tags.
A "label" is an identifier followed by a colon, as follows:
Label1:
For Java, the only place to use the label is before the loop statement. Further, it actually needs to be immediately ahead of the loop statement--
It is unwise to place any statements between the label and the Loop. The only reason to set the label before the loop is that we want to nest another
A loop or a switch. This is because the break and continue keywords usually only interrupt the current loop, but if used with the tag, they
is interrupted to the place where the label exists. As shown below:
Label1:
Outer Loop {
Internal Loop {
//...
Break 1
//...
Continue 2
//...
Continue Label1; 3
//...
Break Label1; 4
}
}
In condition 1, the break interrupts the internal loop and ends at the outer loop. In Condition 2, the continue is moved back to the beginning of the internal loop. But
In Condition 3, continue Label1 interrupts both internal and external loops and moves to Label1. Subsequently, it is actually continuing to
Loop, but it starts with the outer loop. In condition 4, the break Label1 also interrupts all loops and returns to the Label1, but not heavy
New into the loop. In other words, it actually stops two loops completely.
Here is an example of a For loop:

: Labeledfor.java     //java ' labeled for loop     public class Labeledfor { &N Bsp      public static void Main (string[] args) {           &
Nbsp;int i = 0;             outer://Can ' t have statements here      & nbsp      for (; true;) {//infinite loop                 inner://Can ' t have Statem Ents here                 for (; i < i++) { &nbsp ;
                 prt ("i =" + i);                     if (i = = 2) {                         prt ("COntinue ");                         
Continue                    &nbsp}     & nbsp;               if (i = 3) {      
                 prt ("break");                         i+ +; Otherwise I never                     &
nbsp;   //gets incremented.                         
Break                    &nbsp}     & nbsp;               if (i = 7) {         
               prt ("continue outer");                         i+ +; Otherwise I never                     &
nbsp;   //gets incremented.                         
Continue outer;                    &nbsp}     & nbsp;               if (i = 8) {      
                 prt ("Break outer");        &NBSP;&NBSP;&NBSp
             break outer;                    &nbsp}     & nbsp;               for (int k = 0; k < 5; k++) {   & nbsp;                    if (k = = 3) {                          
  &NBSP;PRT ("continue inner");                         
    continue Inner;                        &nbsp} & nbsp;                   }                 &nbsp             }              //Can ' t break or Continue             //to labels Here         }         static void prt (String s) {&NBSP;&NB Sp
         system.out.println (s);         }     ///:~


This uses the PRT () method that is already defined in other examples.
Note that the break interrupts the for loop, and the increment expression does not execute until the end of the For loop is reached. Because the break skipped the pass
Increase the expression, so the increment will be executed directly in the case of i==3. In the case of i==7, the continue outer statement will also reach the top of the loop
, and it also skips increments, so it's also directly incremented.

The following are the output results:
i = 0
Continue inner
i = 1
Continue inner
i = 2
Continue
i = 3
Break
i = 4
Continue inner
i = 5
Continue inner
i = 6
Continue inner
i = 7
Continue outer
i = 8
Break outer
If there is no break outer statement, there is no way to find out the path of the outer loop in an internal loop. This is because the break itself is only
Can interrupt the inner loop (same for continue).
Of course, if you want to exit the method while interrupting the loop, simply use a return.
The following example shows you the tagged break and the use of the continue statement in the while loop:
: Labeledwhile.java     //java ' labeled while ' Loop     public class Labeledwhile {&N bsp;       public static void Main (string[] args) {        
    int i = 0;             outer:while (True) {        &
nbsp;       prt ("Outer while Loop");                 while (True) {     & nbsp
             i++;
                    prt ("i =" + i);                     if (i = = 1) {                         prt ("
Continue ");  &nbsP
                     continue;                    &nbsp}     & nbsp;               if (i = 3) {      
                 prt ("continue outer");                         
Continue outer;                    &nbsp}     & nbsp;               if (i = 5) {      
                 prt ("break");                         break;                    &nbsp}     & nbsp;               if (i = 7) {      
                 prt ("Break outer");                         
Break outer;                    &nbsp}     & nbsp;           }            &nbsp} & nbsp;       }         static void prt (String s) {   &NB
sp;        system.out.println (s);

         }     ///:~


The same rule applies to while:
(1) A simple continue will return to the beginning (top) of the inner loop and continue execution.
(2) The tagged continue will reach the position of the label and re-enter the loop immediately following the tag.
(3) Break interrupts the current loop and moves away from the end of the current label.
(4) A tagged break interrupts the current loop and moves away from the end of the loop indicated by that label.
The output of this method is at a glance:
Outer while loop
i = 1
Continue
i = 2
i = 3
Continue outer
Outer while loop
i = 4
i = 5
Break
Outer while loop
i = 6
i = 7
Break outer
The point to keep in mind is that the only place in Java where labels are used is to have nested loops and to break or continue multiple nesting levels
Other times.
In Dijkstra's "Goto evil" theory, he is most opposed to the label, not Goto. As the number of labels in a program increases,
He found more and more opportunities to produce errors. tags and goto make it difficult to make static analysis of the program. This is because they are in the process of executing
Many "circles" have been introduced into the process. Fortunately, Java tags do not cause problems in this area because their active sites are limited
Death, you cannot pass the control of the program everywhere in a special way. This also raises an interesting question: by limiting the ability of statements, the reverse

It can make a language feature more useful.


From the Java programming idea

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.