IT Band of Brothers Java Grammar classic case of flow control statements

Use this cycle continue to ignore the rest of the sentence

continue and break functions somewhat similar, the difference is just continue to ignore this cycle the rest of the sentence, and then the next cycle begins and does not end the cycle; and break the cycle itself is completely terminated. The following program demonstrates the use of continue.

public class ContinueDemo{

    public static void main(String[] args){

         for(int i = 0; i<3; i++){

              System.out.println ( "the value of i is" + i);

              if(i == 1){

                   continue;

              }

              System.out.println ( "continue after the output statement");

         }

    }

}

Run the above procedures, you will see the results shown in FIG. 20.

597c72d696f54f2097c3b14c3b00445f.png

Figure 20 ContinueDemo operating results

 

From the point of view to run the above results, when i is equal to 1, the program does not output "output of the continue statement" string, as to continue program execution, ignore this loop when the continue statement code. From this point of view, if a continue statement on the last line of a single cycle, the continue statement makes no sense, because it just ignores blank, do not ignore any program statement.

Similarly the break, may continue immediately after a label for the cycle skip the rest of the sentence if the identified tag cycle, the next cycle begins again. For example, the following code.

public class ContinueDemo2{

    public static void main(String[] args){

         outer;

         for(int i = 0; i<5; i++){

              for(int j = 0; j<3; j++){

                   System.out.println ( "i values:" + i + "j values:" + j);

                   if(j == 1){

                         continue outer;

                   }

              }

         }

    }

}

Run the above program can be seen, the value of the loop variable j will not be more than one, because whenever j is equal to 1, continue outer statement ended when the outer loop the loop, directly next cycle begins, the inner loop no chance execution is complete.

And break Similarly, the label must also continue after a valid label, i.e. the label should be placed generally continue previously defined where the outer loop cycle.

 

Use return ended method

The return keyword is not dedicated to ending the cycle, return of function is the end of a method. When a method to execute when a return statement (after the return keyword can be followed by variables, constants, and expressions), this method will be terminated.

Most Java programs are circulating on the method of execution, for example, all circulation procedures described earlier. Once the execution of a return statement in the loop body, the method return statement will end the cycle naturally come to an end. For example the following procedure.

public class ReturnDemo{

    public static void main(String[] args){

         for(int i = 0; i<3; i++){

              System.out.println ( "i values:" + i);

              if(i == 1){

                   return;

              }

              System.out.println ( "output after the return statement");

         }

    }

}

Run the above procedures, the loop is only executed when i is equal to 1, i is equal to 1 when the program is completely finished (when the end of the main method, i.e. the end of the Java program). From the results of this operation, although not specifically return for keyword loop control structure, but a loop through the return statement may indeed end. And continue and break are different, the end of the entire return direct method, no matter how many layers in the return cycle.

Guess you like

Origin www.cnblogs.com/itxdl/p/11262010.html