The choice structure of JavaScript learning (2)--switch

Source: Internet
Author: User

Switch BASIC syntax structure:

switch (expression) {case
            value 1: statement;
            Case value 2: sentence;
            Case Value 3: sentence;
                ...
            Default:
                statement;
        

Working principle: first match with the expression, match the success of the following case with the value of the execution of the corresponding statement. If none match, the default defaults are executed. Small Example 1

         var number=4;
        Switch (number) {Case
            1:alert ("Number1");
            Case 2:alert ("number2");
            Case 3:alert ("Number3");
            Case 4:alert ("Number4");
            Default:
                alert ("Default");
        

Operation Effect:
Small Example 2: Implementing Arithmetic

        var a=10,b=5;
        var ch= "+";
        Switch (CH) {case
            "+": alert (a+b);
            Case "-": Alert (a-b);
            Case "*": Alert (a*b);
            Case "/": Alert (A/b);

            Default:
                alert ("Please enter the correct operation symbol");
        }

Operation Effect:
Break and Continue description

Break is to exit the current loop
Continue is to exit the current loop and continue the following loop

The function of the break statement
(1) The break statement can only be used in the loop body and in the switch statement body.
(2) When a break appears in a switch statement body in the loop body, the function 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, after the execution of the break, jump out of the loop body of the layer.
(4) In the loop structure, the break statement is applied to make the process jump out of the loop of the layer, thus ending the cycle in advance.

Continue statement Action
(1) The general form of the continue statement is: Contonue;
(2) Its function is to end this cycle, that is, skip the remaining statements in the body of this cycle, and then again the condition of the cycle of determination.
(3) Note: Executing the CONTINUE statement does not terminate the entire loop. In the while and do-while loops, the Continue statement causes the process to skip directly to the test section of the loop control condition
, and then decide whether the loop continues.
(4) in the For loop, after encountering continue, skip the remaining statements in the loop body, evaluate the expression 3 in the For statement, then perform the conditional test of expression 2, and finally determine whether the for loop executes based on the value of expression 2. In a circular body, regardless of the statement component in which the continue is used, it is performed according to the above function, which differs from the break.

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.