JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

5.4.2 else if

5.4.2 else if


The if/else statement evaluates an expression and executes one of two pieces of code, depending on the outcome. But what about when you need to execute one of many pieces of code? One way to do this is with an else if statement. else if is not really a JavaScript statement, but simply a frequently used programming idiom that results when repeated if/else statements are used:

if (n == 1) {

// Execute code block #1 } else if (n == 2) {

// Execute code block #2 } else if (n == 3) {

// Execute code block #3 } else {

// If all else fails, execute block #4 }

There is nothing special about this code. It is just a series of if statements, where each following ifis part of the elseclause of the previous statement. Using the else ifidiom is preferable to, and more legible than, writing these statements out in their syntactically equivalent, fully nested form:

if (n == 1) {

// Execute code block #1 } else {

if (n == 2) {

// Execute code block #2 } else {

if (n == 3) {

// Execute code block #3 } else {

// If all else fails, execute block #4 } } }

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/31334.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1