kotlin-逻辑控制语句
if…else
在Kotlin中,if表达式返回一个值。它用于控制程序结构的流程。 在Kotlin中有各种类型的if表达式。
- if-else表达式
- if-else
- if-else阶梯表达式嵌套if表达式
传统的if…else语句
if(condation){
//code statement
}
else{
//code statement
}
if-else表达式
fun main(args: Array<String>) {
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<String>) {
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
if-else if-else阶梯表达式
fun main(args: Array<String>) {
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
for
Kotlin for循环用于迭代程序的一部分几次。 它遍历数组,范围,集合或提供迭代的任何内容。 Kotlin for循环等同于C#等语言中的foreach循环。
语法:
for (item in collection){
// 循环的主体
}
示例:
fun main(args : Array<String>) {
val marks = arrayOf(80,85,60,90,70)
for(item in marks){
print(item)
}
}
输出:80 85 60 90 70
如果for循环体仅包含一行语句,则花括号{}可以省略。
fun main(args : Array<String>) {
val marks = arrayOf(80,85,60,90,70)
for(item in marks)
print(item)
}
输出:80 85 60 90 70
基于数组的索引(索引)迭代数组的元素。 例如:
fun main(args : Array<String>) {
val marks = arrayOf(80,85,60,90,70)
for(item in marks.indices)
println("marks[$item]: "+ marks[item])
}
迭代范围
fun main(args : Array<String>) {
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/do…while
while
语法:
while(condition){
// 循环主体
}
示例:
fun main(args: Array<String>){
var i = 1
while (i<=5){
print(i)
i++
}
}
输出:1 2 3 4 5
do…while
- do-while循环类似于while循环,但是有一个关键区别:do-while循环首先执行do块的主体,然后检查while的条件。
- 由于在检查条件之前首先要执行do-while循环的do块,所以do-while循环执行至少一次,即使while内的条件为false也要执行一次。 do-while循环的while语句以;(分号)结尾
语法:
do{
// do块的主体
}
while(condition);
示例:
fun main(args: Array<String>){
var i = 1
do {
println(i)
i++
}
while (i<=5);
}
输出: 1 2 3 4 5
when
Kotlin when表达式是一个返回值的条件表达式。 Kotlin when表达式用于替换switch语句
示例:
fun main(args: Array<String>){
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<String>){
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<String>){
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<String>){
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")
}
}
break,return,continue
break表达式
语法:
for(..){
// for主体
if(checkCondition){
break;
}
}
示例:
fun main(args: Array<String>) {
for (i in 1..5) {
if (i == 3) {
break
}
println(i)
}
}
输出:1 2
continue表达式
Kotlin的continue语句用于重复循环,它继续当前程序流并在指定条件下跳过剩余代码。嵌套循环中的continue语句仅影响内部循环。
语法:
for(..){
// for中的if语句上部分主体
if(checkCondition){
continue
}
//for中的if语句下部分主体
}
示例:
fun main(args: Array<String>) {
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
}
}
输出: 我退出了该方法