目录

else.. if ladder/nested if 语句

否则......如果梯形图可用于测试多个条件。 以下是相同的语法。

if (boolean_expression1) { 
   //statements if the expression1 evaluates to true 
} else if (boolean_expression2) { 
   //statements if the expression2 evaluates to true 
} else { 
   //statements if both expression1 and expression2 result to false 
} 

使用if ... else语句时,请记住几点。

  • 一个if可以有零个或一个其他的,它必须在任何其他if之后。

  • 如果是的话,if可以有零到多个,并且它们必须在else之前。

  • 一旦else成功,其余的其他if或者其他都将被测试。

例子 (Example)

var num = 2 
if(num > 0) { 
   console.log(num+" is positive") 
} else if(num < 0) { 
   console.log(num+" is negative") 
} else { 
   console.log(num+" is neither positive nor negative") 
}

代码显示值是正数,负数还是零。

成功执行上述代码后,将显示以下输出。

2 is positive
↑回到顶部↑
WIKI教程 @2018