Kotlin基础(二)--- 控制流(if,when, for, while)
2017-10-24 11:10
435 查看
1、 If 表达式
在Kotlin中,if是一个表达式,它返回一个值。跟java不同,它没有三元操作符(condition ? then : else)。
// 常规的使用 var max = a if (a < b) max = b // 带 else 的用法 var max: Int if (a > b) { max = a } else { max = b } // 作为表达式 val max = if(a>b) a else b
if 分支中的最后一行的表达式作为返回值:
val max = if (a > b) { print("Choose a") a } else { print("Choose b") b }
注 :如果使用 if 作为表达式而不是语句,比如 返回值或者将其赋值给一个变量,这种情况下,if 表达式需要 有
else分支
2、When 表达式
when 代替了C语言风格中的 switch 操作。其最简单的形式如下:when (x) { 1 -> print("x == 1") 2 -> print("x == 2") else -> { // Note the block print("x is neither 1 nor 2") } }
when 将它的参数和所有的分支条件顺序比较,直到某个分支满足条件;
when 既可以被当做表达式使用也可以被当做语句使用。如果作为表达式使用,符合条件分支的值就是整个表达式的值;如果作为语句使用,则忽略单个分支的值。(和 if 相似,每个分支作为一个代码块,最后一行表达式的值就是它的值)。
如果其它分支都不满足条件,将执行 else 分支。如果when 作为表达式使用,else 分支式必须的,除非编译器检测出分支条件满足了所有可能的情况。
如果很多分支需要用相同的方式处理,则可以把多个分支条件放在一起,用逗号分隔:
when (x) { 0, 1 -> print("x == 0 or x == 1") else -> print("otherwise") }
可以使用任意的表达式(不仅仅是常量)作为分支条件:
when (x) { parseInt(s) -> print("s encodes x") else -> print("s does not encode x") }
也可以检测一个值在(in)或者不在(!in)一个区间或者集合中:
when (x) { in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") }
另一种可能性是检测一个值是(is)或者不是(!is)一个特定类型的值。注意: 由于智能转换,你可以访问该类型的方法和属性而无需 任何额外的检测
fun hasPrefix(x: Any) = when(x) { is String -> x.startsWith("prefix") else -> false }
when 也可以用来取代 if-else if链。 如果不提供参数,所有的分支条件都是简单的布尔表达式,而当一个分支的条件为真时则执行该分支:
when { x.isOdd() -> print("x is odd") x.isEven() -> print("x is even") else -> print("x is funny") }
3、For 循环
for 循环可以对任何提供迭代器(iterator)的对象进行遍历,语法如下:for (item in collection) print(item)
循环体可以是一个代码块:
for (item: Int in ints) { // ... }
如上所述:for 可以循环遍历任何提供了迭代器的对象。
有一个成员函数或者扩展函数iterator(),它返回一个类型
有一个成员函数或者扩展函数next(),并且
有一个成员函数或者扩展函数hasNext()返回 Boolean.
如果你想要通过索引遍历一个数组或者一个 list,你可以这么做:
for (i in array.indices) { print(array[i]) }
注意这种”在区间上遍历”会编译成优化的实现而不会创建额外对象。
或者你可以用库函数 withIndex:
for ((index, value) in array.withIndex()) { println("the element at $index is $value") }
4、While循环
while 和 do..while 的使用同其他语言一样:while (x > 0) { x-- } do { val y = retrieveData() } while (y != null) // y is visible here!
相关文章推荐
- Kotlin 中的 if、when、for、while
- Kotlin基础 if,for,while语句
- 分享下学习Kotlin的历程-开始写逻辑吧 if/for/while/when
- Kotlin学习(三)—— 基本类型,包,控制流:if、when、for、while,Break和continue
- Python基础语法(2)——字典、函数定义使用、控制流(if&for&while)
- Python语法基础_控制流语句_if、while、for
- Kotlin-1.3-控制流程(if, when, for, while)
- Kotlin学习(三)—— 基本类型,包,控制流:if、when、for、while,Break和continue
- [shell基础]——if/for/while/until/case 语句
- Python基础--循环for、while,判断if
- expect语法基础: while、for 循环、if 语句的用法示例
- 【if-while-case-for-until】控制流结构
- expect语法基础: while、for 循环、if 语句的用法示例
- python控制流语句-while,for,if
- Python if for else elif while break loop pass 控制流工具
- VII Python(3)基础知识(if、while、for、interator、generator、文件、pickle)
- Python基础(条件判断和循环) if elif else for while break continue;
- Python3学习笔记【基础3--list,tuple,if条件判断,for/while循环,dict字典,set集合】
- 黑马程序员————java基础if,for,while
- Python3学习(一)-基础、数据类型、变量、字符串和编码、list&tuple、if、for、while、dict、set、函数与参数