Java difference in the break, continue with the return of

A, break statement.

break statement : switch using the occasion mainly statements and loops structure.

Use the break statement to note:

<1> in the structural loop break statement, if a break statement is executed, then exit the loop, then performs the following loop structure the first statement. If you use the break statement in multiple nested loops, when the implementation of the break statement, exit the loop structure is that it has no effect outer loops .

<2> If the cyclic structure there switch statement, break and used in a switch statement, when performing the break statement switch statement, only out of the switch, the outer loop does not exit the structure .

Illustrated by the following cases:

1 break statement:

for (int i = 0; i < 10; i++) {  
            if (i == 6) { 
                break;  
               // 在执行i==6时强制终止循环,i==6不会被执行  
                }
            System.out.println(i);  
      }

The output is 012,345; after 6 will not be output.

2 break statement:

If you add a little code above the code to see if fully understand the break usage.

int index = 0;
for (int i = 0; i < 10; i++) {  
      if (i == 6) {
         index=i;
         break;  
               // 在执行i==6时强制终止循环,i==6不会被执行  
           }     
    }
System.out.println(index); //是在for循环外部

In this case, index 6 is the output value.

Will not feel a bit puzzled, if conditions i == 6 clearly has been terminated, but also why the index is 6 assigned it?

The reason is that: the code is executed one by one, when added to the for loop i 6, if the execution is determined, index = i since this statement on the BREAK, is performed first, assigned to the index i = 6, then BREAK performed, the termination loops, if statements are not executed is determined (but prior to the termination has been assigned to the index), therefore the out of the loop, the output index is 6.

Two, continue statement

continue statement : it is quite similar to break, are mainly used for switch statements and loop structure. But there are differences , break statement is directly terminate the loop, execute the statement outside the loop; continue statement did not really withdraw from circulation, but only ends the execution of this loop, so when in use continue to pay attention to this.

continue: terminate the current cycle, but not out of the loop (in a loop continue behind the statement is not executed), continue down the implementation cycle according to cycling conditions.

for (int i = 0; i < 10; i++) {  
    if (i == 6)  {
        continue;  
      // i==6不会被执行,而是被中断了    
       } 
       System.out.println(i);  
   }

The output is 012345789; 6 not only output.

Three, return statement

return statement : is using the occasion to withdraw the function returns to the calling of the function, if the main () function, then the end of the run the entire program.

Use the return statement should pay attention to:

<1> return to exit from the current process, return to the statement of the method of the call, continue. Meaning that the current method exit, continue down the implementation.

<2> return statement returns a value to the caller of the method, return data type value must be consistent with the type declaration of the method return value.

<3> may also return later with no arguments, no arguments that returns empty, in fact, the main purpose is to want to interrupt function is executed, returns to the calling function at.

<4> The method returns the value of the void, from out of a determination, must return.

 

 

 

 

 

 

 

 

 

Published 20 original articles · won praise 2 · Views 1592

Guess you like

Origin blog.csdn.net/weixin_42132733/article/details/105106585