The difference between break continue return in Java

Source: Internet
Author: User

1. Break

Break: Jumps out of the current loop, but if it is a nested loop, you can only jump out of the current layer of the loop, and only break through the layer to jump out of all loops;

 for  (int  i = 0 ; I < 10 ; I++ if  (i = = 6  ) { break  ;  //  forces the loop to terminate when I==6 is executed, i==6 will not be executed  } System.  out           .println (i); The output is 0  1  2  3  4  5 ; 6 will not output 

2, continue

Continue: Terminates the current loop, but does not jump out of the loop (the statement following the continue in the loop will not be executed) and continues down the loop based on the loop condition.

 for (int0; i++)      {if6)  {          Continue;         // i==6 will not be executed, but be interrupted    .        }        System.  out . println (i);     }  

The output is 0 1 2 3 4 5 7 8 9; only 6 no output

3. Return

(1). Return exits from the current method and returns to the statement of the method that was called to continue execution.
(2). Return returns a value to the statement that invokes the method, and the data type of the return value must match the type of the return value in the declaration of the method.
(3). Return can be followed without parameters, without parameters is returned empty, in fact, the main purpose is to interrupt function execution, return to the calling function.

Special Note: The return value of void method, from a certain judgment to jump, must use return;

The difference between break continue return in Java

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.