【Kotlin】5回目 言語基礎(フローコントロール)
目次
①if文
②when文
③forループ
④whileループ
①if文
// Traditional usage var max = a if (a < b) max = b // With else var max: Int if (a > b) { max = a } else { max = b } // 式として利用できる。 val max = if (a > b) a else b
②when文
JavaのSwitch-case文と同じ様な使い方。
when (x) { 1 -> print("x == 1") 2 -> print("x == 2") else -> { // Note the block print("x is neither 1 nor 2") } }
③forループ
式として使える。
for (item in collection) print(item)
通常はブロックで書く。
for (item: Int in ints) { // ... }
④whileループ
Javaと一緒。
while (x > 0) { x-- } do { val y = retrieveData() } while (y != null) // y is visible here!