Kotlin 简明教程
Kotlin - if…else Expression
Kotlin if…else 表达式在任何其他现代计算机编程中,就像 if…else 表达式一样工作。因此,我们从 Kotlin 中提供 if…else 语句入手。
Kotlin if…else expressions works like an if…else expression in any other Modern Computer Programming. So let’s start with our traditional if…else statement available in Kotlin.
Syntax
传统 if…else 表达式的语法如下:
The syntax of a traditional if…else expression is as follows:
if (condition) {
// code block A to be executed if condition is true
} else {
// code block B to be executed if condition is false
}
这里 if 语句得到执行,并且给定 condition 得到选中。如果该条件被评为 true ,则代码块 A 得到执行,否则程序转到 else 部分,代码块 B 得到执行。
Here if statement is executed and the given condition is checked. If this condition is evaluated to true then code block A is executed, otherwise program goes into else part and code block B is executed.
Example
你可以尝试以下示例:
You can try the following example:
fun main(args: Array<String>) {
val age:Int = 10
if (age > 18) {
print("Adult")
} else {
print("Minor")
}
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Minor
Kotlin if…else Expression
Kotlin if…else 也可以用作返回一个值得表达式,并且该值可以被分配给变量。以下是 Kotlin if…else 表达式的简单语法:
Kotlin if…else can also be used as an expression which returns a value and this value can be assigned to a variable. Below is a simple syntax of Kotlin if…else expression:
Syntax
val result = if (condition) {
// code block A to be executed if condition is true
} else {
// code block B to be executed if condition is false
}
如果你将其用作表达式(例如,返回其值或分配给变量),则 else 分支是强制性的。
If you’re using if as an expression, for example, for returning its value or assigning it to a variable, the else branch is mandatory.
Examples
fun main(args: Array<String>) {
val age:Int = 10
val result = if (age > 18) {
"Adult"
} else {
"Minor"
}
println(result)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Minor
当 if 只有一个语句时,你可以省略大括号 {}:
You can ommit the curly braces {} when if has only one statement:
fun main(args: Array<String>) {
val age:Int = 10
val result = if (age > 18) "Adult" else "Minor"
println(result)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Minor
你可以在 if…else 块中包括多语句,在这种情况下,最后一个表达式作为块的值返回。尝试以下示例:
You can include multiple statements in if…else block, in this case the last expression is returned as the value of the block. Try the following example:
fun main(args: Array<String>) {
val age:Int = 10
val result = if (age > 18) {
println("Given condition is true")
"Adult"
} else {
println("Given condition is false")
"Minor"
}
print("The value of result : ")
println(result)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Given condition is false
The value of result : Minor
Kotlin if…else…if Ladder
你可以使用 else if 条件来指定当第一个条件为假时的新的条件。
You can use else if condition to specify a new condition if the first condition is false.
Syntax
if (condition1) {
// code block A to be executed if condition1 is true
} else if (condition2) {
// code block B to be executed if condition2 is true
} else {
// code block C to be executed if condition1 and condition2 are false
}
Example
fun main(args: Array<String>) {
val age:Int = 13
val result = if (age > 19) {
"Adult"
} else if ( age > 12 && age < 20 ){
"Teen"
} else {
"Minor"
}
print("The value of result : ")
println(result)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
The value of result : Teen
Kotlin Nested if Expression
Kotlin 允许将 if 表达式放在另一个 if 表达式中。这称为 nested if 表达式
Kotlin allows to put an if expression inside another if expression. This is called nested if expression
Syntax
if(condition1) {
// code block A to be executed if condition1 is true
if( (condition2) {
// code block B to be executed if condition2 is true
}else{
// code block C to be executed if condition2 is fals
}
} else {
// code block D to be executed if condition1 is false
}
Example
fun main(args: Array<String>) {
val age:Int = 20
val result = if (age > 12) {
if ( age > 12 && age < 20 ){
"Teen"
}else{
"Adult"
}
} else {
"Minor"
}
print("The value of result : ")
println(result)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
The value of result : Adult