JavaScript ---- flow control statements

## special syntax

1, the statement ends with; if a line is only one statement; can be omitted (not recommended) 
2, the definition of a variable using the var keyword, you can not use

* Use: defined variables are local
  * do: Defined Variables is a global variable (not recommended)


## flow control statements
  . 1, the else IF ... ...
  2, switch
    
. 1, in java, type of the switch can receive data: byte, int, short, char , enumeration ( 1.5), strings (1.7)
      * Switch (variable):
        * Case Found:
    2, in JS,
Switch may receive any type of data
<script>
    var a = null;
    switch (a) {
        case 1:
            alert("number");
            break;
        case "abc":
            alert("string");
            break;
        case true:
            alert("boolean");
            break;
        case null:
            alert("null");
            break;
        case undefined:
            alert("undefined");
            break;
    }
</script>
  3、while
<script>
    //1~100求和
    var num = 1;
    var sum = 0 ;
    while (num <= 100){
        sum+=num;
        num++;
    }
    document.write(sum);
</script>
  4、do...while
  5、for
<script>
    //1~100求和
    var sum = 0;
    for (var i =0; i <= 100; i++){
        sum += i;
    }
    alert(sum);
</script>
 

## Exercise: do a multiplication table

Results are as follows:

Code:

<! DOCTYPE HTML > 
< HTML lang = "EN" > 
< head > 
    < Meta charset = "UTF-. 8" > 
    < title > multiplication table </ title > 
    < style > 
        TD { 
            border : 1px Solid ; 
        } 
    </ style > 
</ head > 
< Script > 
    // multiplication table for nested do loop 
    document.write ( " <= Table align = left '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");
            document.write("</td>");
        }
        document.write("</tr>");
    }
    document.write("</table>");
</script>
<body>
</body>
</html>

 

Guess you like

Origin www.cnblogs.com/21seu-ftj/p/12287329.html