首页 / 知识

if...elseif...else语句语法分享

2023-04-11 13:37:00

if...elseif...else语句语法分享
    if...elseif...else语句
 
    使用if....elseif...else语句来选择多个代码块之一来执行。
 
    语法
 
if (condition1)
 
{
 
    当条件 1 为 true 时执行的代码
 
}
 
else if (condition2)
 
{
 
    当条件 2 为 true 时执行的代码
 
}
 
else
 
{
 
  当条件 1 和 条件 2 都不为 true 时执行的代码
 
}
 
    实例
 
    如果时间小于10:00,则生成问候"Goodmorning",如果时间大于10:00小于20:00,则生成问候"Goodday",否则生成问候"Goodevening":
 
if (time<10)
 
{
 
    document.write("<b>早上好</b>");
 
}
 
else if (time>=10 && time<20)
 
{
 
    document.write("<b>今天好</b>");
 
}
 
else
 
{
 
    document.write("<b>晚上好!</b>");
 
}
 
    x的结果是:
 
    今天好

语句语法分享执行条件代码块

最新内容

相关内容

热门文章

推荐文章

标签云

猜你喜欢