# 流程控制

# 分支控制

# if语句

判断语句的语法格式为:

if(判断条件){
    执行语句
}
1
2
3

来看下面实例:

<html>

<head></head>

<body>
    <script type="text/javascript">
        var val = 20;
        if (val > 90) {
            console.log("优秀")
        } else if (val > 60) {
            console.log("及格")
        }

        var obj1 = []
        var obj2 = {}
        var obj3 = [1]
        var str1;
        var str2 = "";
        var str3 = "2"

        judgeObj(obj1)
        judgeObj(obj2)
        judgeObj(obj3)

        judgeObj(str1)
        judgeObj(str2)
        judgeObj(str3)


        function judgeObj(obj) {

            function IfNull(obj) {
                console.log("判断对象不会undefined,null,且不为空字符串,对象里面有属性。")
                if (obj != null && obj.length > 0) {
                    console.log("对象不为空");
                }

            }
            function IfNull2(obj) {
                console.log("判断对象不会为undefined,null,且字符串不为空。")
                if (obj) {
                    console.log("对象不为空2")
                }
            }

            IfNull(obj);
            console.log("------")
            IfNull2(obj)
            console.log("----------------")

        }


    </script>
</body>

</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

# switch语句

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>switch语句</title>
    <script>

        var a;
        switch (a) {
            case 1:
                alert("number");
                break;
            case "abc":
                alert("string");
                break;
            case true:
                alert("true");
                break;
            case null:
                alert("null");
                break;
            case undefined:
                alert("undefined");
                break;

        }
    </script>
</head>
<body>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 循环控制

# for语句

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>for语句</title>
    <script>

        var sum = 0;
        for (var i = 1; i <= 100; i ++){
            sum += i;
        }
        alert(sum);

    </script>
</head>
<body>

</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# for/in语句

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>for in语句</title>
    <script>
        var person = { name: "xie", age: 22 };

        var x;
        for (x in person) {
            console.log(x + ":" + person[x]);
        }
    </script>
</head>

<body>
</body>

</html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# forEach语句

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>forEach语句</title>
    <script>
        var temp=[3,2,9,16]
        temp.forEach(function(val,index,array){
            console.log(val+":"+index+":"+array[index])
        })
    </script>
</head>

<body>
</body>

</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# while语句

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>while语句</title>
    <script>

        var sum = 0;
        var num = 1;

        while(num <= 100){
            sum += num;
            num ++;
        }
        console.log(sum);

        var sum2 = 0;
        do{
            sum2+=num;
            num --;
        }while(num>=0);
        console.log(sum2);

    </script>
</head>
<body>

</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# 实例

写一个9*9乘法表:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>99乘法表</title>
    <style>
        td{
            border: 1px solid;
        }

    </style>

    <script>

        document.write("<table  align='center'>");


        for (var i = 1; i <= 9 ; i++) {
            document.write("<tr>");
            for (var j = 1; j <=i ; j++) {
                document.write("<td>");

                document.write(i + " * " + j + " = " + ( i*j) +"&nbsp;&nbsp;&nbsp;");

                document.write("</td>");
            }
            document.write("</tr>");
        }

        document.write("</table>");

    </script>
</head>
<body>

</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37