五、Kotlin——条件控制、循环控制

条件控制是每门编程语言中必不可少的,一般就是使用我们所熟知的if-else,来作为我们代码逻辑选择条件控制。在Java中一般使用 if-else和 switch-case 来作为条件控制,而在 Kotlin 中则是使用 if-else 和 when 来作为条件控制。 立

Tips:Kotlin中没有switch-case 。

一、if 表达式

1、带返回值 if 表达式

在 Kotlin 中,if是一个表达式所以它会返回一个值,表达式的值为表达式作用域内最后一行的值。这一点和Java 是不同的,在 Java 中 if 仅仅是语句。

Kotlin 复制代码
fun maxOf(a: Int, b: Int): Int{
    if (a > b){
        return  a
    }else{
      return  b
    }
}

fun main() {
    val res = maxOf(10,22)
    
}

2、 if 表达式替代三目运算符

因为在 Kotlin 中 if 表达式是带有返回值的、所以在 Kotlin 中是不需要三目运算符(xxx?xxx:xxx);因为 if 表达式这些都能做到。

而在 Kotlin 中则可以直接使用 if 表达式来使用:

kotlin 复制代码
/// 返回if 
fun maxOf(a: Int, b: Int): Int{
    return  if (a > b){
        a
    }else{
        b
    }
}
//简写
fun  maxOf(a: Int,b: Int): Int{
    return if (a>b) a else b
}

二、when 表达式

在 Kotlin 中使用 when 表达式替代了类似C语言的 switch-case 语句。

when 将它的参数与所有的分支条件顺序比较,直到某个分支满足条件。 when 既可以被当做表达式使用也可以被当做语句使用。如果它被当做表达式,符合条件的分支的值就是整个表达式的值,如果当做语句使用,则忽略个别分支的值。(像 if一样,每一个分支可以是一个代码块,它的值是块中最后的表达式的值。)

Tips:如果其他分支都不满足条件将会求值 e/se 分支。如果 when 作为一个表达式使用,则必须有e/se 分支,除非编译器能够检测出所有的可能情况都已经覆盖了

Kotlin 复制代码
fun eval2(number: Number):String=when(number) {
    is Int -> "this is int number"
    is Double -> "this is double number"
    is Float -> "ths is float number'
    is Long -> "this is long number"
    is Byte -> "this is byte number"
    is Short -> "this is Short number"
    else -> "invalid number"
}
kotlin 复制代码
// when 同样是带有返回值的// when 将它的参数与所有的分支条件顺序比较,直到某个分支满足条件
fun eval2(number:Number):String=when(number) {
    is Int -> "this is int number"
    is Double -> "this is double number'"
    is Float -> {
        println("the number is float")
        "ths is float number"
    }
    is Byte -> "this is byte number"
    is Short -> "this is Short number"
    else -> "invalid number"
}

三、循环控制

循环控制语句也是每门语言不可缺少的一部分,一般就是我们所熟知的for、while、do-while 。 迟 Kotlin 循环其实几乎和 Java中 的一模一样。

1、for 循环

for 循环 可以对任何提供迭代器(iterator)的对象进行遍历,for 循环仅以唯一一种形式存在,和 Java的 foreach循环一致。其写法 for item in elements 和 C# 一样。和 Java 类似。

Kotlin 复制代码
val  array = mutableListOf<Int>(1,2,3,4,5,6,8)
for (item in array){
    println(item)
}

2、while 和 do-while 循环

Kotlin 中 while 和 do-while 循环,它们的语法和 Java 中相应的循环没什么区别:

Kotlin 复制代码
val  array = mutableListOf<Int>(1,2,3,4,5,6,8)

var  index = 0
while (index < array.size){
    println("The $index element is ${array[index++]}")
}
println("空格")

 // do - while 先执行 一次循环体,再看循环条件是否满足。满足继续执行,不满足则退 
index = 0
do {
    println("The $index element is ${array[index++]}")
}while (index < array.size)

3、区间表达式

Kotlin 复制代码
// ..  遍历区间,注意Kotlin的区间的包含或是闭合的。
for (i in 1 .. 10){
    println("$i")
}
// 1 2 3 4 5 6 7 8 9 10

// until 不包括最后一个元素
for (i in 1 until  10){
    println("$i")
}
// 1 2 3 4 5 6 6 7 8
Kotlin 复制代码
 // downTo //是指从10 ... 1
  for ( i in 10 downTo 1){
      println("$i")
  }
// 10 9 8 7 6 5 4 3 2 1

  // step 步长
  for ( i in 10 downTo 1 step  2 ){
      println("$i")
  }
  // 10 8 6 5 4 2
相关推荐
JMchen1232 小时前
Android后台服务与网络保活:WorkManager的实战应用
android·java·网络·kotlin·php·android-studio
crmscs2 小时前
剪映永久解锁版/电脑版永久会员VIP/安卓SVIP手机永久版下载
android·智能手机·电脑
localbob2 小时前
杀戮尖塔 v6 MOD整合版(Slay the Spire)安卓+PC端免安装中文版分享 卡牌肉鸽神作!杀戮尖塔中文版,电脑和手机都能玩!杀戮尖塔.exe 杀戮尖塔.apk
android·杀戮尖塔apk·杀戮尖塔exe·游戏分享
机建狂魔2 小时前
手机秒变电影机:Blackmagic Camera + LUT滤镜包的专业级视频解决方案
android·拍照·摄影·lut滤镜·拍摄·摄像·录像
hudawei9962 小时前
flutter和Android动画的对比
android·flutter·动画
lxysbly4 小时前
md模拟器安卓版带金手指2026
android
儿歌八万首5 小时前
硬核春节:用 Compose 打造“赛博鞭炮”
android·kotlin·compose·春节
消失的旧时光-19438 小时前
从 Kotlin 到 Dart:为什么 sealed 是处理「多种返回结果」的最佳方式?
android·开发语言·flutter·架构·kotlin·sealed
有位神秘人8 小时前
kotlin与Java中的单例模式总结
java·单例模式·kotlin
Jinkxs8 小时前
Gradle - 与Groovy/Kotlin DSL对比 构建脚本语言选择指南
android·开发语言·kotlin