五、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
相关推荐
千里马学框架1 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台1 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone1 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc1 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo1 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077001 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
ai2work1 天前
ch23 综合复刻:从零做一个最小可用版本(capstone)
kotlin
其实防守也摸鱼1 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
ai2work1 天前
ch21 签名、校验与发版
kotlin
AFinalStone1 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui