Java Java tutorials to share two jump statements

Java tutorial Share Java two jump statements , Java jump statements used to implement the process cycle jump execution of program flow, in Java jump statements break statement and continue statement has two kinds. Next were explained in detail.

1, the Java 's first jump statements: break statement

You can use the break statement in switch conditional statements and loop statements. When it appears in the switch conditional statement, is to terminate a case and out of switch structure. When it appears in the loop, the loop out of the role is performed later in the code. Specific Specific operating procedure to introduce the following cases:


public class Example{

         public static void main(String[] args){

                   int x=1;

                   while (x<=4){

                            System.out. println("x="+x);

                            If (x==3){

                                     break;

                            }

                            x++;

                   }

         }

}



The above results of the program operation is: X = 1; X = 2; X = 3.


In the above case, the value of x by printing while loop, ⅹ value of 3 out of the loop when the break statement. Therefore, print the result did not occur "x = 4".

When a break statement appears in the inner loop of the nested loop, it is only out of the inner loop, if you want to break statement out of the outer loop will need to add tags outer loops. We introduce the following cases:


public class Example{

         public static void main(String[] args){

                   int x= i,j;

                   intcast: for (i=1;i<=9;i++) {

                            for (j=1;j<=i;j++) {

                                     if (i>4) {

                                               break intcast;

                                     }

                                     System.out.print("*");

                            }

                            System.out.print("\n");

                   }

}

The above operating results for the program:

*

**

***

****

Because just in front of the outer loop increases the mark for "Toast". When i> 4, using the break itcast; statement out of the outer loop. So the program only print 4 lines "*."


2, the Java 's second jump statement: continue Statement

continue statement is used in a loop statement, its role is to terminate the present cycle, the next cycle execution. Next, the odd sums of the range of 1 to 100.

public class Example{

         public static void main(String[] args){

                   int sum = 0; // define variables sum, and to remember

                   for (int i=1;i<=100;i++) {

                            if (i% 2 == 0) {// it is an even number, not cumulative

                                     continue; // end this cycle

                            }

                            sum + = i; // for an accumulation sum of i and

                   }

                   System.out.println("sum"=+sum);

}

The above operating results for the program: sum = 2500.

In the above case allows for loop variable i is circulated between 1 and 100, in the cycle, when the value of i is an even number, execute the continue statement end of this cycle, to enter the next cycle. When the value of i is an odd number, sum and i are accumulated, all odd finally obtained between 1 and 100, print "sum = 2500" nested loop statements followed by the continue statement may use the tag end the outer loop, break statement usage and similar, not illustrated here.


Guess you like

Origin blog.51cto.com/14256902/2421104