Use and difference of break, continue and break

Source: Internet
Author: User

1. Functions of return statements

(1) return exits from the current method, returns to the statement of the method called, and continues execution.
(2) return returns a value to the statement that calls this method. The data type of the returned value must be the same as the type of the returned value in the method declaration. You can use forced type conversion to ensure that the data type is the same.
(3) return when void is used to declare that the return type is null in the method description, this format should be used without returning any value.

2. Roles of the break statement
(1) The break statement can only be used in the loop body and switch statement body.
(2) When the break appears in the switch statement body of the loop body, it only jumps out of the switch statement body.
(3) When the break appears in the loop body but is not in the switch statement body, it jumps out of the loop body at the current layer after the break is executed.
(4) In the loop structure, the break statement is applied to make the process jump out of the cycle body of the Current layer, so as to end the cycle of the current layer in advance.

3. Functions of the continue statement
(1) The general form of the continue statement is contonue;
(2) its function is to end this cycle, that is, to skip the remaining statements in the cycle body that have not been executed, and then judge the conditions of the cycle again.
(3) Note: The execution of the continue statement does not terminate the entire loop. In the while and do-while loops, The continue statement causes the process to jump directly to the test section of the loop control condition, and then determines whether the loop continues.
(4) In a for loop, when a continue is encountered, skip the remainder statement in the loop body and evaluate the value of "expression 3" in the for statement, then perform the conditional test of expression 2, and determine whether to execute the For Loop Based on the value of expression 2. In the loop body, no matter what statement the continue is, it will be executed according to the above function, which is different from break.

From: http://www.cnblogs.com/huangy/archive/2008/12/16/1356105.html

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.