Javascript jump statement break continue return

Source: Internet
Author: User
Break statement:
The break statement causes the running program to immediately exit the loop contained in the innermost layer or exit a switch statement. Because it is used to exit a loop or switch statement, this form of break statement is valid only when it appears in these statements.

If the termination condition of a loop is very complex, it is much easier to use the break statement to implement certain conditions than to use a loop expression to express all the conditions.

<SCRIPT type = "text/JavaScript">
        For (VAR I = 1; I <= 10; I ++ ){
                If (I = 6) break;
                Document. Write (I );
        }
        // Output result: 12345
</SCRIPT>

Continue statement:
The continue statement is similar to the break statement. The difference is that it is not to exit a loop, but to start a new iteration of the loop.

The continue statement can only be used in the loop body of the while statement, do/while statement, for statement, or for/in statement. It may cause errors when used elsewhere!

<SCRIPT type = "text/JavaScript">
        For (VAR I = 1; I <= 10; I ++ ){
                If (I = 6) continue;
                Document. Write (I );
        }
        // Output result: 1234578910
</SCRIPT>

Return Statement:Exit from the current function and return a value from that function.

UseReturnStatement to terminate the execution of a function and return the value. IfParametersOmitted, or not in the functionReturnWhen the statement is executed, the undefined value is assigned to the expression that calls the current function.

The return statement is used to specify the value returned by the function. The return statement can only appear in the function body. syntax errors may occur anywhere else in the code!
When a return statement is executed, function execution stops even if there are other statements in the function body!

 

 

 

 

Example:

<SCRIPT type = "text/JavaScript">
 VaR STR = "javascript CSS "; // String variable
 VaR strlength = Str. length;// String Length
 VaR AA = 0;                // Number of subscript at the string position
 For (I = 0; I <Str. length; I ++)// For Loop
 {
   If ("A" = Str. charat (I ))// If LoopCharat () method if I = a position in the string
   {
     AA = I + 1;             //Starting from 1
     Break;              // Skip the loop
   }
   
 }
 Document. Write ("string" + STR + "the letter A is:" + AA );
</SCRIPT>

Related Article

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.