JavaScript switch Case Statement Learning notes

Source: Internet
Author: User
Tags case statement switch case

In an IF condition statement, a logical condition can only have one, and if there are multiple conditions, it can be resolved using nested IF statements, but this method increases the complexity of the program and reduces the readability of the program.

Using the switch Process Control statement can solve these problems perfectly, with the following basic structure:

Switch statement syntax

The code is as follows Copy Code

switch (conditional expression)
{
Case constants:
{
Statement A;
}
Case constants:
{
Statement B;
}
Case constants:
{
Statement C;
}
...
Case constants:
{
Statement F;
}
Default
{
Statement N;
}
}

Switch statement Syntax description

Sequentially executes each statement following the case, and then executes the statement n below the default
The statements that follow each case can be multiple, but use {} to include
The values behind each case must be different.


Where A is a numeric or character type data, the value of A is A1, A2 、...... In comparison, if a is equal to one of the values, the statement following the corresponding data is executed, and when the keyword break is encountered, the program jumps out of the switch structure and executes the statement below the keyword default if a value equal to a is not found.

Switch Case statement:

The code is as follows Copy Code

var year=8;
var army= "", msg= "";
Switch (parseint (year)) {
Case 0:
army= "Civilians";
Break
Case 1:
Army= "PVT";
Break
Case 2:
Army= "first-class soldier";
Break
Case 3:
Case 4:
Case 5:
Army= "First Class Petty Officer";
Break
Case 6:
Case 7:
Case 8:
Army= "Level two Petty officer";
Break
Default
if (year>8)
Army= "senior Petty Officer";
}

msg+= "Soldiers:" +year+ "year n";
msg+= "Conclusion:" +army;
Alert (msg);


Example

The code is as follows Copy Code
var a = 3;
Switch (a)
{
Case 0:
{
document.write ("www");
}
Case 1:
{
document.write ("Dreamdu");
}
Case 2:
{
document.write ("com");
}
Case 3:
{
document.write ("Www.111cn.net");
}
Default
{
document.write ("Http://www.111cn.net");
}
}

Results

Www.111cn.net
Http://www.111cn.net
Complete SWITCH statement syntax
The results above are usually not what we want, and if you execute a statement after a case, you should jump out of the switch statement, which is the true switch multiple-branch selection statement. You can add a break (jump) after each branch, and the following section describes the break.

The code is as follows Copy Code
switch (conditional expression)
{
Case constants:
{
Statement A;
}
Break
Case constants:
{
Statement B;
}
Break
Case constants:
{
Statement C;
}
Break
...
Case constants:
{
Statement F;
}
Break
Default
{
Statement N;
}
}

When the value of an expression is equal to a constant following a case, the statement executed after the constant is executed, then the switch branch selection statement is executed, and the statement n after the default is performed when all the constants after the case do not conform to the expression.

Example

The code is as follows Copy Code
var a = 3;
Switch (a)
{
Case 0:
{
document.write ("www");
}
Break
Case 1:
{
document.write ("Dreamdu");
}
Break
Case 2:
{
document.write ("com");
}
Break
Case 3:
{
document.write ("Www.111cn.net");
}
Break
Default
{
document.write ("Http://www.111cn.net");
}
}

Look at this function:

The code is as follows Copy Code

function Selectartiletype (typenum)//typenum is an argument passed in
{

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"://any other value
document.getElementById ("C_1"). style.display= "None";
document.getElementById ("c_2"). style.display= "None";
Break

}

}

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.