HTML之JavaScript分支结构

HTML之JavaScript分支结构

if switch

DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <script>
        /*
            if 
                if(表达式){
                    语句1;
                }eles if(){
                    语句2;
                }else{
                    语句3;
                }
                1.非空字符串('xxx')会判断为true,空字符('')串会判断为false
                2.非空对象 会判断为true,null和undefined会判断为false
                3.非0的number会判断为true,0会判断为false

            switch 跟Java语法一样(跟C语言也没啥逻辑区别)
                switch(表达式){
                    case 值1:
                        语句1;
                        break;
                    case 值2:
                        语句2;
                        break;
                    case 值3:
                        语句3;
                        break;
                    default:
                        语句4;
                        break;
                }
         */
        if ("false") {
            console.log('true');
        } else {
            console.log('false');
        }
        //根据月份输出季节
        var month = prompt('请输入月份');//弹窗输入 返回结果为用户在窗口上输入的值,以string类型返回
        console.log(typeof month);
        month = Number.parseInt(month);//将字符串转换为数字
        console.log(typeof month);
        if (month >= 3 && month <= 5) {
            console.log('春季');
        } else if (month == 6 || month == 7 || month == 8) {
            console.log('夏季');
        } else if (month == 9 || month == 10 || month == 11) {
            console.log('秋季');
        } else if (month == 12 || month == 1 || month == 2) {
            console.log('冬季');
        } else {
            console.log('输入错误');
        }
        switch (month) {
            case 3:
            case 4://条件穿透 跟Java一样
            case 5:
                console.log('春季');
                break;
            case 6:
            case 7:
            case 8:
                console.log('夏季');
                break;
            case 9:
            case 10:
            case 11:
                console.log('秋季');
                break;
            case 12:
            case 1:
            case 2:
                console.log('冬季');
                break;
            default:
                console.log('输入错误');
                break;
        }
    script>
head>

<body>

body>

html>

你可能感兴趣的:(前端,HTML,JavaScript,javascript,html,前端)