Js supplemented with small knowledge points (continue, break, ruturn) and continueruturn

Source: Internet
Author: User

Js supplemented with small knowledge points (continue, break, ruturn) and continueruturn

1. continue, break, ruturn

Eg: sum of 1-

$(function (){    $("#hello").click(function ()    {        var iNum = 0;        for (var i = 1; i < 101; i++)        {            iNum += i;        }        alert(iNum);    });});

Result: 5050

Change to break to view the result.

$(function (){    $("#hello").click(function ()    {        var iNum = 0;        for (var i = 1; i < 101; i++)        {            if (i == 5)            {                break;            }            iNum += i;        }        alert(iNum);    });});

Result: 10

 

Conclusion 1: break: jumps out of the entire loop body.

Change to continue and check the result?

 1 $(function () 2 { 3     $("#hello").click(function () 4     { 5         var iNum = 0; 6         for (var i = 1; i < 101; i++) 7         { 8             if (i == 5) 9             {10                 continue;11             }12             iNum += i;13         }14         alert(iNum);15     });16 });

 

Result: 5045, (all except 5)

Conclusion 2: continue skips the cycle of the current condition.

Return can be used in either of the following ways:

The first method is to convert it to return to see the result?

The result is: no result. return ends the method body and directly jumps out of the method body, so it cannot be printed.

The second method of return: a method that returns a value

1 $ (function () 2 {3 $ ("# hello "). click (function () 4 {5 var iNum = 0; 6 for (var I = 1; I <101; I ++) 7 {8 if (I = 5) 9 {10 I = A (I); 11} 12 iNum + = I; 13} 14 alert (iNum); 15 }); 16 // The second method of return is to return A value 17 function A (I) 18 {19 I + = 5; 20 return I; 21} 22 });

 

Result: 5015

Process Analysis:

 

Conclusion 3: return is used. The first method ends the whole body, and the second method returns a value.

 

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.