11.md 3.6 KB
Newer Older
W
wizardforcel 已提交
1
# Kotlin `if`表达式
W
wizardforcel 已提交
2 3 4

> 原文: [https://www.programiz.com/kotlin-programming/if-expression](https://www.programiz.com/kotlin-programming/if-expression)

W
wizardforcel 已提交
5
#### 在本文中,您将借助示例学习在 Kotlin 中使用`if`表达式。
W
wizardforcel 已提交
6

W
wizardforcel 已提交
7
## `if...else`的传统用法
W
wizardforcel 已提交
8

W
wizardforcel 已提交
9
`if...else`的语法是:
W
wizardforcel 已提交
10 11 12 13 14 15 16 17 18 19

```kt
if (testExpression) {
   // codes to run if testExpression is true
}
else {
  // codes to run if testExpression is false
}
```

W
wizardforcel 已提交
20
如果将`testExpression`求值为`true`,则`if`执行代码的特定部分。 它可以具有可选的`else`子句。 如果`testExpression`为假,则执行`else`子句中的代码。
W
wizardforcel 已提交
21 22 23

* * *

W
wizardforcel 已提交
24
### 示例:`if...else`的传统用法
W
wizardforcel 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

```kt
fun main(args: Array<String>) {

    val number = -10

    if (number > 0) {
        print("Positive number")
    } else {
        print("Negative number")
    }
}
```

运行该程序时,输出为:

```kt
Negative number

```

* * *

W
wizardforcel 已提交
48
## Kotlin `if`表达式
W
wizardforcel 已提交
49

W
wizardforcel 已提交
50
与 Java(以及其他许多编程语言)不同,`if`可以在 Kotlin 中用作表达式; 它返回一个值。 **推荐阅读**[Kotlin 表达式](/kotlin-programming/statement-expression#expressions "Kotlin Expression")
W
wizardforcel 已提交
51 52 53 54 55

* * *

这是一个例子:

W
wizardforcel 已提交
56
### 示例:Kotin `if`表达式
W
wizardforcel 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

```kt
 fun main(args: Array<String>) {

    val number = -10

    val result = if (number > 0) {
        "Positive number"
    } else {
        "Negative number"
    }

    println(result)
}
```

When you run the program, the output will be:

```kt
Negative number

```

当使用`if`作为表达式时,`else`分支是必需的。

* * *

如果`if`的主体只有一个语句,则花括号是可选的。 例如,

```kt
fun main(args: Array<String>) {
    val number = -10
    val result = if (number > 0) "Positive number" else "Negative number"
    println(result)
}
```

W
wizardforcel 已提交
94
这类似于 Java 中的[三元运算符](/java-programming/ternary-operator "Java ternary operator.")。因此,在 Kotlin 中没有三元运算符。
W
wizardforcel 已提交
95 96 97

* * *

W
wizardforcel 已提交
98
### 示例:具有多个表达式的`if`块
W
wizardforcel 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

如果`if`分支的块包含多个表达式,则最后一个表达式作为该块的值返回。

```kt
fun main(args: Array<String>) {

    val a = -9
    val b = -11

    val max = if (a > b) {
        println("$a is larger than $b.")
        println("max variable holds value of a.")
        a
    } else {
        println("$b is larger than $a.")
        println("max variable holds value of b.")
        b
    }
    println("max = $max")
}

```

When you run the program, the output will be:

```kt
-9 is larger than -11.
max variable holds value of a.
max = -9
```

W
wizardforcel 已提交
130
**推荐阅读***Kotlin `when`语句*
W
wizardforcel 已提交
131 132 133

* * *

W
wizardforcel 已提交
134
## Kotlin `if..else..if`阶梯
W
wizardforcel 已提交
135

W
wizardforcel 已提交
136
您可以使用`if..else...if`阶梯在 Kotlin 中的许多块中返回一个代码块。
W
wizardforcel 已提交
137 138 139

* * *

W
wizardforcel 已提交
140
### 例如:`if..else..if`阶梯
W
wizardforcel 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

```kt
fun main(args: Array<String>) {

    val number = 0

    val result = if (number > 0)
        "positive number"
    else if (number < 0)
        "negative number"
    else 
        "zero"

    println("number is $result")
}

```

W
wizardforcel 已提交
159
该程序检查`num`是正数,负数还是零。
W
wizardforcel 已提交
160 161 162

* * *

W
wizardforcel 已提交
163
## Kotlin 嵌套`if`表达式
W
wizardforcel 已提交
164

W
wizardforcel 已提交
165
一个`if`表达式可以位于另一个`if`表达式的嵌套块内。
W
wizardforcel 已提交
166 167 168

* * *

W
wizardforcel 已提交
169
### 示例:嵌套`if`表达式
W
wizardforcel 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

该程序计算三个数字中最大的数字。

```kt
fun main(args: Array<String>) {

    val n1 = 3
    val n2 = 5
    val n3 = -2

    val max = if (n1 > n2) {
        if (n1 > n3)
            n1
        else
            n3
    } else {
        if (n2 > n3)
            n2
        else
            n3
    }

    println("max = $max")
}
```

When you run the program, the output will be:

```kt
max = 5

```