Switch case statement learning notes in Javascript

Source: Internet
Author: User
Tags switch case

Switch case is a qualified data in js, which is much simpler than if else. Next I will introduce my understanding of js switch case.

In the if Condition Statement, only one logical condition can exist. if multiple conditions exist, you can use nested if statements to solve the problem. However, this method increases the complexity of the program, reduce program readability.

The switch flow control statement can perfectly solve these problems. Its basic structure is as follows:

Switch statement syntax

The Code is as follows:

Switch (conditional expression)
{
Case constant:
{
Statement;
}
Case constant:
{
Statement B;
}
Case constant:
{
Statement c;
}
...
Case constant:
{
Statement f;
}
Default:
{
Statement n;
}
}

Switch statement syntax

Execute each statement after case sequentially, and finally execute statement n Under default.
The statement after each case can be multiple, but you need to use {} to include
The values after each case must be different.


Where a is a numeric or numeric data, the value of a is equivalent to a1, a2 ,...... Comparison: If a is equal to a value, execute the statement following the corresponding data, and when the keyword break is encountered, the program jumps out of the switch structure. If the value of a is not found, execute the statement under the keyword default.

Switch case statement:

The Code is as follows:

Var year = 8;
Var army = "", msg = "";
Switch (parseInt (year )){
Case 0:
Army = "civilian ";
Break;
Case 1:
Army = "private ";
Break;
Case 2:
Army = "Attach ";
Break;
Case 3:
Case 4:
Case 5:
Army = "Level 1 sergeant ";
Break;
Case 6:
Case 7:
Case 8:
Army = "Level 2 sergeant ";
Break;
Default:
If (year> 8)
Army = "senior and secondary officers ";
}

Msg + = "Age:" + year + "n ";
Msg + = "conclusion:" + army;
Alert (msg );


Example

The Code is as follows:
Var a = 3;
Switch ()
{
Case 0:
{
Document. write ("www ");
}
Case 1:
{
Document. write ("dreamdu ");
}
Case 2:
{
Document. write ("com ");
}
Case 3:
{
Document. write ("www. bKjia. c0m ");
}
Default:
{
Document. write ("http://www.bKjia. c0m ");
}
}

Result

Www. bKjia. c0m
Http://www.bKjia. c0m.
Complete switch statement syntax
The above results are generally not what we want. If the statement after a case is executed, the switch statement should be jumped out. This is the true switch multi-branch selection statement. You can add a break (jump out) to the end of each branch. The following section describes break.

The Code is as follows:
Switch (conditional expression)
{
Case constant:
{
Statement;
}
Break;
Case constant:
{
Statement B;
}
Break;
Case constant:
{
Statement c;
}
Break;
...
Case constant:
{
Statement f;
}
Break;
Default:
{
Statement n;
}
}

When the value of "expression" is equal to the constant after a case, the statement executed after the constant is executed, and then the switch Branch selection statement is jumped out, when the constants after all cases do not conform to the "expression", execute the statement n after default.

Example

The Code is as follows:
Var a = 3;
Switch ()
{
Case 0:
{
Document. write ("www ");
}
Break;
Case 1:
{
Document. write ("dreamdu ");
}
Break;
Case 2:
{
Document. write ("com ");
}
Break;
Case 3:
{
Document. write ("www. bKjia. c0m ");
}
Break;
Default:
{
Document. write ("http://www.bKjia. c0m ");
}
}

Look at this function:

The Code is as follows:

Function selectArtileType (typeNum) // typeNum is a passed-in parameter.
{
 
Switch (typeNum)
{
Case "0": // when typeNum = "0"
Document. getElementById ("c_1"). style. display = "none ";
Document. getElementById ("c_2"). style. display = "";
Break;
Case "1": // when typeNum = "1"
Document. getElementById ("c_1"). style. display = "";
Document. getElementById ("c_2"). style. display = "none ";
Break;
Case "2": // when typeNum = "2"
Document. getElementById ("c_1"). style. display = "";
Document. getElementById ("c_2"). style. display = "";
Break;
Default "3": // when any other value is used
Document. getElementById ("c_1"). style. display = "none ";
Document. getElementById ("c_2"). style. display = "none ";
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.