JavaScript中的if條件判斷語句

if條件判斷語句:

#條件成立,執行什麼程式碼;條件不成立,執行什麼程式碼


結構一:只判斷真(true),條件為假,什麼都不做

if(條件判斷:判斷結果是一個布林值)

{

條件為真(true),執行的程式碼

}


結構二:既判斷真,也判斷假

if (條件判斷)

{

條件為真,執行的程式碼

}else

{

條件為假,執行的程式碼

}


#結構三:多條件判斷

#if(條件1)

{

程式碼1;

}else if(條件2)

{

程式碼2;

}else if(條件3)

{

#程式碼3;

##}else

{

如果以上條件都不成立,則執行程式碼;

}

注意:雖然有多個條件,但各條件之間是「或」的關係。每時每刻,只能有一個條件成立,不能同時滿足多個條件。

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
        //给一个成绩
        var score=89;
        //判断成绩所属级别
        if(score<60){
            document.write("对不起,没有及格");
        }else if (score>=60&&score<70){
            document.write("刚好及格");
        }else if(score>=70&&score<80){
            document.write("成绩良好");
        }else if(score>=80&&score<90){
            document.write("成绩优秀");
        }else if(score>=90&&score<100){
            document.write("试卷这么难,这样的成绩简直逆天");
        }else{
            document.write("对不起,你的成绩超出系统判断范围,请重新输入");
        }
        
        </script>
    </head>
    <body>
    </body>
</html>


#

繼續學習
||
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> //给一个成绩 var score=89; //判断成绩所属级别 if(score<60){ document.write("对不起,没有及格"); }else if (score>=60&&score<70){ document.write("刚好及格"); }else if(score>=70&&score<80){ document.write("成绩良好"); }else if(score>=80&&score<90){ document.write("成绩优秀"); }else if(score>=90&&score<100){ document.write("试卷这么难,这样的成绩简直逆天"); }else{ document.write("对不起,你的成绩超出系统判断范围,请重新输入"); } </script> </head> <body> </body> </html>
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!