在Kotlin中,if表达式返回一个值。它用于控制程序结构的流程。 在Kotlin中有各种类型的if表达式。
if(condation){
//code statement
}
else{
//code statement
}
fun main(args: Array) {
val num1 = 10
val num2 = 20
val result = if (num1 > num2) {
"$num1 is greater than $num2"
} else {
"$num1 is smaller than $num2"
}
println(result)
}
输出:10 is smaller than 20
当出现此类表达式时可以移除大括号,实现类似三元运算符效果(kotlin不支持三元运算符):
fun main(args: Array) {
val num1 = 10
val num2 =20
val result = if (num1 > num2) "$num1 is greater than $num2" else "$num1 is smaller than $num2"
println(result)
}
输出:10 is smaller than 20
fun main(args: Array) {
val num = 10
val result = if (num > 0){
"$num is positive"
}else if(num < 0){
"$num is negative"
}else{
"$num is zero"
}
println(result)
}
输出:10 is positive
Kotlin for循环用于迭代程序的一部分几次。 它遍历数组,范围,集合或提供迭代的任何内容。 Kotlin for循环等同于C#等语言中的foreach循环。
语法:
for (item in collection){
// 循环的主体
}
示例:
fun main(args : Array) {
val marks = arrayOf(80,85,60,90,70)
for(item in marks){
print(item)
}
}
输出:80 85 60 90 70
fun main(args : Array) {
val marks = arrayOf(80,85,60,90,70)
for(item in marks)
print(item)
}
输出:80 85 60 90 70
fun main(args : Array) {
val marks = arrayOf(80,85,60,90,70)
for(item in marks.indices)
println("marks[$item]: "+ marks[item])
}
fun main(args : Array) {
print("for (i in 1..5) print(i) = ")
for (i in 1..5) print(i)
println() = 12345
print("for (i in 1 unit 5) print(i) = ")
for (i in 1 unit 5) print(i)
println() = 1234
print("for (i in 5..1) print(i) = ")
for (i in 5..1) print(i) // prints nothing
println() = " "
print("for (i in 5 downTo 1) print(i) = ")
for (i in 5 downTo 1) print(i)
println() = 54321
print("for (i in 5 downTo 2) print(i) = ")
for (i in 5 downTo 2) print(i)
println() = 5432
print("for (i in 1..5 step 2) print(i) = ")
for (i in 1..5 step 2) print(i)
println() = 135
print("for (i in 5 downTo 1 step 2) print(i) = ")
for (i in 5 downTo 1 step 2) print(i)
println() = 531
}
语法:
while(condition){
// 循环主体
}
示例:
fun main(args: Array){
var i = 1
while (i<=5){
print(i)
i++
}
}
输出:1 2 3 4 5
语法:
do{
// do块的主体
}
while(condition);
示例:
fun main(args: Array){
var i = 1
do {
println(i)
i++
}
while (i<=5);
}
输出: 1 2 3 4 5
Kotlin when表达式是一个返回值的条件表达式。 Kotlin when表达式用于替换switch语句
示例:
fun main(args: Array){
var number = 4
var numberProvided = when(number) {
1 -> "One"
2 -> "Two"
3 -> "Three"
4 -> "Four"
5 -> "Five"
else -> "invalid number"
}
println("You provide $numberProvided")
}
when使用大括号的多重声明
fun main(args: Array){
var number = 1
when(number) {
1 -> {
println("Monday")
println("First day of the week")
}
7 -> println("Sunday")
else -> println("Other days")
}
}
when 多个分支
fun main(args: Array){
var number = 8
when(number) {
3, 4, 5, 6 ->
println("It is summer season")
7, 8, 9 ->
println("It is rainy season")
10, 11 ->
println("It is autumn season")
12, 1, 2 ->
println("It is winter season")
else -> println("invalid input")
}
}
在范围内使用when
fun main(args: Array){
var number = 7
when(number) {
in 1..5 -> println("Input is provided in the range 1 to 5")
in 6..10 -> println("Input is provided in the range 6 to 10")
else -> println("none of the above")
}
}
语法:
for(..){
// for主体
if(checkCondition){
break;
}
}
示例:
fun main(args: Array) {
for (i in 1..5) {
if (i == 3) {
break
}
println(i)
}
}
输出:1 2
Kotlin的continue语句用于重复循环,它继续当前程序流并在指定条件下跳过剩余代码。嵌套循环中的continue语句仅影响内部循环。
语法:
for(..){
// for中的if语句上部分主体
if(checkCondition){
continue
}
//for中的if语句下部分主体
}
示例:
fun main(args: Array) {
for (i in 1..3) {
println("i = $i")
if (j == 2) {
continue
}
println("this is below if")
}
}
输出: i = 1
this is below if
i = 2
i = 3
this is below if
···
### return 表达式
示例:
fun returnExample(){
var str: String = “”
if (str.isBlank()){
println(“我退出了该方法”)
return
}
}
输出: 我退出了该方法