JavaScript Language 12 and javascript Language 12
When using the if statement, if you encounter many conditions, you should not continue using the if statement. JavaScript provides a more efficient alternative, that is, the switch statement, let's first look at the switch statement template:
<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1=1;switch(value1){case 0:document.write("value1=0");break;case 1:document.write("value1=1");break;case 2:document.write("value1=2");break;default:document.write("value1="+value1);break;}</SCRIPT></BODY></HTML>
The switch statement consists of the following parts:
Conditional expressions, case statements, break statements, and default statements.
1) The switch statement starts with the switch keyword, placing the condition expression in parentheses after the switch keyword.
2) The role of the case statement is to check whether the condition matches (the switch statement can contain N multiple case statements ).
3) The break statement is used to tell JavaScript to stop execution and switch the statement. Of course, you can also omit the break statement. If it is omitted, the statement continues until the break statement or switch statement is executed.
4) The default statement indicates that all case statements are executed when they do not match. Of course, the default statement can also be omitted.
Code after the break statement is omitted:
<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1=1;switch(value1){case 0:document.write("value1=0"+"<br>");case 1:document.write("value1=1"+"<br>");case 2:document.write("value1=2"+"<br>");default:document.write("value1="+value1);break;}</SCRIPT></BODY></HTML>
After the break statement is omitted, we can see that the above is executed downward from case 1 until the break statement at the default statement is stopped (of course, if the break statement from the default statement is also omitted, results are the same as above ).
If the case statement and break statement are reasonably matched, some better logic code can be written.
Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/41318865 sentiment control _