Operators(if, else if, else, ternary operator, switch)

💡 原文约200字/词,阅读约需1分钟。
📝

内容提要

文章介绍了条件语句的用法。首先,用if-else语句判断变量x的值:小于5输出“Less than 5”,等于10输出“Equal to 10”,否则输出“Greater than 5”。接着,介绍三元运算符语法:条件 ? ifTrue : ifFalse。最后,用switch语句判断变量day的值,当day为3时输出“Wednesday”。

🎯

关键要点

  • 使用if-else语句判断变量x的值
  • x小于5时输出'Less than 5'
  • x等于10时输出'Equal to 10'
  • 其他情况下输出'Greater than 5'
  • 三元运算符的语法为:条件 ? ifTrue : ifFalse
  • 使用switch语句判断变量day的值
  • 当day为3时输出'Wednesday'
  • default情况下输出'Invalid day'
➡️

继续阅读