选择结构---单向,双向,多向,if语句的嵌套

一: 单向选择:if...

  • 语法

if(条件){.......}

  • 举例
<script>
   var score=100;
    if (score>60){alert("那你很棒棒噢~")};
</script>

 

 

二:双向选择:if ...else...

  • 语法

if(条件){.......}

    else{.......}

  • 举例
 var score = 59;
    if (score < 60) {
      alert("补考!");
    } else {
      alert("通过!");
    }

 

三:多向选择:if...else if...else...

  • 语法

if (条件1) {//当条件1为ture时执行的代码}

else if (条件2) {//当条件2为tures时执行的代码}

else {//当条件1和条件2都为false时执行的代码}

  • 举例
var time = 17;
    if (time < 12) {
      document.write("上午好");
    } else if (time >= 12 && time < 18) {
      document.write("下午好");
    } else {
      document.write('晚上好');
    }

 

四:if语句的嵌套

  • 语法

if (条件1)  {if (条件2){条件1和条件2都为ture时}   else {条件1为ture,条件2为false}}

else {if (条件2) {条件1为false,条件2为ture}    else {条件1为false,条件2为false}}

  • 举例
var gender = "女";
    var height = 172;
    if (gender =="男") {
      if (height > 170) {
        document.write("高个子男生");
      } else {
        document.write("矮个子男生");
      }
    } else {
      if (height > 170) {
        document.write("高个子女生");
      } else {
        document.write("矮个子女生");
      }
    }

 

posted @ 2022-04-02 23:24  是贰叁  阅读(167)  评论(0)    收藏  举报