天天看點

02運算符與流程控制

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>02運算符與流程控制</title>
</head>
<body>

<script>

    /*1.指派運算符與算術運算符*/
    //1.1 指派運算
    // let a = 5;
    // let b = "heHe"
    // console.log(typeof a,typeof b) // number string

    //1.2 算術運算
    // let a = 5,b = 3;
    // let c = a % b  // + 加、- 減、* 乘、/ 除、% 取餘
    // a += 5 //a = a + 5
    // console.log(a, c)

    /*2.一進制運算符的前置與後置操作*/
    //2.1當是獨立的表達式時,前++和後++效果一樣
    // let n = 1;
    // n++;           // ++n    n = n + 1
    // console.log(n) // 2

    //2.2 ++n傳回的是自增後的值(新值)
    // let n = 1,f = 2;
    // let d = f + ++n; // let d = f + (n = n + 1)
    // console.log(d)   // 4

    //2.3 n++傳回的是自增前的值(原變量值)
    // let n = 1,f = 2;
    // let d = f + n++; // let d = f + n
    // console.log(d)   // 3

    /*3.比較運算符注意事項*/
    // let a = 2;
    // let b = "3";
    // let c = "2";
    // // string b 和 number a 相比,b 自動轉為 number
    // console.log(a < b); // true
    // // == 強制類型轉換比較
    // console.log(a == c); // true
    // // 不強制類型轉換比較
    // console.log(a === c); // false

    /*4.邏輯運算符執行個體詳解*/
    /*
    (1) 邏輯與
    使用 && 符号表示邏輯與,指符号兩端都為 true 時表達式結果為 true。
    (2) 邏輯或
    使用 || 符号表示邏輯或,指符号左右兩端有一方為 true,表達式即成立。
    (3) 邏輯非
    使用 ! 符号表示邏輯非,即原來是 true 轉變為 false,反之亦然。
    */

    /*5.短路運算的妙用*/
    // let a=1,b=0;
    // console.log(a == true,b == false); //true true

    // 真值指派
    // let c=8,d=0;
    // let e = d || c;
    // console.log(e); //8

    // 使用短路特性指派
    // let sex = prompt("請輸入性别") || "保密";
    // console.log(sex)

    /*6.if else*/
    // 當條件為真時執行表達式代碼塊。
    // let status = false;
    // if (status) {
    //     console.log("successfully");
    // } else {
    //     console.log("failed"); // failed
    // }

    /*7.三元表達式*/
    // 是針對 if 判斷的簡寫形式。
    // let n = true ? 1 : 2;
    // console.log(n); //1
    //
    // let f = true ? (1 == true ? 'yes' : 'no') : 3;
    // console.log(f); // yes

    /*8.switch使用注意事項*/
    /*
    (1)可以将 switch 了解為 if 的另一種結構清晰的寫法。
    (2)如果表達式等于 case 中的值,将執行此 case 代碼段
    (3)break 關鍵字會終止 switch 的執行
    (4)沒有任何 case比對時将執行default 代碼塊
    (5)如果case執行後缺少 break 則接着執行後面的語句
    */
    // let name = '視訊';
    // switch (name) {
    //     case '産品':
    //         console.log('産品');
    //         break;
    //     case '視訊':
    //         console.log('視訊'); // 視訊
    //         break;
    //     default:
    //         console.log('default')
    // }

    /*9.while循環控制*/
    // 循環執行語句,需要設定跳出循環的條件否則會陷入死循環狀态。
    // 先判斷條件成立再進入循環
    // let start = 5;
    // while (1<start--) {
    //     console.log(start) // 4 3 2 1
    // }

    /*10.dowhile循環執行個體操作*/
    // 後條件判斷語句,無論條件是否為真都會先進行循環體。
    // 先進入循環再判斷條件
    // let start = 1;
    // do {
    //     console.log(start); // 1 2 3 4 5
    // } while (++start<=5);

    /*11.使用for循環列印楊輝三角*/
    // 可以在循環前初始化初始計算變量。下面是使用for 列印倒三角的示例
    // for (let i = 10; i > 0; i--) {
    //     for (let n = 0; n < i; n++) {
    //         document.write('*');
    //     }
    //     document.write("<br/>");
    // }

    /*12.break-continue與label标簽的使用*/
    //break用于退出目前循環,continue 用于退出目前循環傳回循環起始繼續執行。

    //擷取所有偶數,所有奇數使用 continue 跳過
    // for (let i = 1; i <= 10; i++) {
    //     if (i % 2) continue;
    //     console.log(i); //2 4 6 8 10
    // }

    //擷取三個奇數,超過時使用 break退出循環
    // let count = 0,num = 3;
    // for (let i = 1; i <= 10; i++) {
    //     if (i % 2) {
    //         console.log(i); //1 3 5
    //         if (++count == num) break;
    //     }
    // }

    /*13.for-in與for-of使用方法操作*/
    // 用于周遊對象的所有屬性,for/in主要用于周遊對象,不建議用來周遊數組。

    // // for-in 根據下标取值
    // let web = ["www.baidu.com","www.jd.com"];
    // for(let key in web) {
    //     console.log(web[key]) // www.baidu.com www.jd.com
    // }
    //
    // // for-of 直接取值
    // for(let key of web) {
    //     console.log(key); // www.baidu.com www.jd.com
    // }

</script>
</body>
</html>