<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ### **條件表達式:If** if表達式,我們可以分為 * 簡單if語句 * if…else語句 * if…else if多分支語句。 ### **if else語句規則** * if后的括號不能省略,括號里表達式的值須是布爾型 * 如果條件體內只有一條語句需要執行,那么if后面的大括號可以省略。良好的編程風格建議加上大括號。 * 對于給定的if,else語句是可選的,else if 語句也是可選的。 * else和else if同時出現時,else必須出現在else if 之后。 * 如果有多條else if語句同時出現,那么如果有一條else if語句的表達式測試成功,那么會忽略掉其他所有else if和else分支。 * 如果出現多個if,只有一個else的情形,else子句歸屬于最內層的if語句。 * 如果你使用 if 作為表達式而不是語句(例如:返回它的值或者把它賦給變量),該表達式需要有`else`分支。 #### **1.傳統用法,作為if語句** ``` var max = a if (a < b) max = b // With else var max: Int if (a > b) { max = a } else { max = b } ``` #### **2.作為if表達式** >[warning]**注意**:如果 if 作為表達式,顯然必需要有else分支,因為表達式在任何情況下都應有返回值! ``` val max = if (a > b) a else b //if分支還可以是代碼塊,最后一行的表達式作為代碼塊的返回值: val max = if (a > b) { print("Choose a") a } else { print("Choose b") b } ``` ### **簡單if語句** if語句,描述的是,如果條件滿足,就執行某些操作。if語句的語法結構如下: ``` if(條件表達式){ //執行語句 } ``` 條件表達式:必選參數,可以是Boolean型變量或者返回結果為Boolean型的表達式。 執行語句:可選參數,任意的代碼語句。 我們通過案例演示下條件表達式,參考案例: ~~~ fun main(args: Array<String>) { val a = true val b = false val c = "kotlin" val d = 2 if (a) { println("a 是 true") } if (a and b) { println("a 和 b 是true") } if (a or b) { println("a 或 b 是true") } if (c === "kotlin") { println("c 等于 kotlin") } if (d == 2) { println("d 等于 2") } if (1 + 1 == d) { println("1 + 1 = 2") } } ~~~ 運行結果 ``` a 是 true a 或 b 是true c 等于 kotlin d 等于 2 1 + 1 = 2 Process finished with exit code 0 ``` 如果,如果執行語句只有一句話,我們也可以省略大括號,采用如下形式: ``` if(條件表達式) //執行語句,要求只有一句話 ``` 所以,上面的代碼,可以改為如下形式,參考代碼: ~~~ fun main(args: Array<String>) { val a = true val b = false val c = "kotlin" val d = 2 if (a) println("a 是 true") if (a and b) println("a 和 b 是true") if (a or b) println("a 或 b 是true") if (c === "kotlin") println("c 等于 kotlin") if (d == 2) println("d 等于 2") if (1 + 1 == d) println("1 + 1 = 2") } ~~~ 運行結果 ``` a 是 true a 或 b 是true c 等于 kotlin d 等于 2 1 + 1 = 2 Process finished with exit code 0 ``` ### **if…else** `if…else`語句,描述的是,如果條件滿足,就執行操作1,否則就執行操作2。`if…else`語句的語法結構如下: ``` if(條件表達式){ //操作1 }else{ //操作2 } ``` 條件表達式:必選參數可以是Boolean型變量或者返回結果為Boolean型的表達式。 操作1、操作2:可選參數,任意的代碼語句。 比如,我們有一個求最值的例子,“如果a>b,最大值就是a,否則就是b”,我們可以用if…else語句去表示,參考代碼: ~~~ fun main(args: Array<String>) { val x = 10 val y = 11 var max = 0 if (x > y) { max = x } else { max = y } println("最大值是:$max") } ~~~ 運行結果 ``` 最大值是:11 Process finished with exit code 0 ``` 同樣,如果“操作1”、“操作2”只有一行語句,我們也可以省略大括號,采用如下形式: ``` if(條件表達式) //操作1 else //操作2 ``` 所以,我們可以對上面代碼做如下修改: ~~~ fun main(args: Array<String>) { val x = 10 val y = 11 var max = 0 if (x > y) max = x else max = y println("最大值是:$max") } ~~~ 運行結果 ``` 最大值是:11 Process finished with exit code 0 ``` >[info]【注意】:在Kotlin中沒有類似 true? 1: 0 這樣的三元表達式。對應的寫法是使用 if else 語句——`if(true) 1 else 0`,在 Kotlin 中,if 是一個表達式,即它會返回一個值。 因此就不需要三元運算符(條件 ? 然后 : 否則),因為普通的if 就能勝任這個角色。 比較特別的,**在Kotlin里面,針對if…else語句,還可以有返回值,然后可以把if…else的返回值賦值給某一個變量**,也就是可以有如下結構: ``` 變量 =if(條件表達式){ //返回值1 }else{ //返回值2 } ``` 如果滿足條件,變量被賦值為返回值1,不滿足條件,變量被賦值為返回值2。這里是把{}看成了Lambda表達式,Lambda表達式的最后一行表示Lambda表達式的結果。我們在后面章節才去講解Lambda表達式,所以想理解Kotlin這個特性還是有些難度,我們可以先留個印象,學完Lambda表達式,我們回頭在看。針對上面案例,我們可以做如下變換: ~~~ fun main(args: Array<String>) { val x = 10 val y = 11 var max = 0 max = if (x > y) x else y println("最大值是:$max") } ~~~ 運行結果 ``` 最大值是:11 Process finished with exit code 0 ``` 這也看成是kotlin中的三目運算符,語法格式如下`判斷條件 ? 表達式1 : 表達式2`,就如上面代碼中的` max = if (x > y) x else y` 比較特別的,在Kotlin里面,**“條件表達式”還可以用in關鍵字去表示元素是否在區間或者是否在集合中**,看如下案例: ~~~ fun main(args: Array<String>) { val range = 1..8 val list = listOf(1, 2, 3, 4, 5, 6, 7, 8) val x = 5 if (x in range) { println("x 在區間里面") } if (x in list) { println("x 在集合里面") } } ~~~ 運行結果 ``` x 在區間里面 x 在集合里面 Process finished with exit code 0 ``` ### **if…else…if語句** if…else…if語句,描述的是,如果條件滿足1,就執行操作1,如果滿足條件2,就執行操作2,如果滿足條件3,就執行操作3等等。if…else…if語句的語法結構如下: ``` if(條件表達式1){ //操作1 }else if(條件表達式2){ //操作2 }else if(條件表達式3){ //操作3 } .......... else{ } ``` 條件表達式1,2,3…:必選參數可以是Boolean型變量或者返回結果為Boolean型的表達式。 操作1, 2,3…,可選參數,任意的代碼語句。 我們通過一個案例簡單演示下,參考代碼: ~~~ fun main(args: Array<String>) { val score = 99 if (score >= 85 && score <= 100) { println("評級為:優") } else if (score >= 70 && score < 85) { println("評級為:良") } else if (score >= 60 && score < 69) { println("評級為:中") } else if (score < 60) { println("評級為:差") } } ~~~ 運行結果 ``` 評級為:優 Process finished with exit code 0 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看