「Kotlin入門」制御構文if式のサンプル

構文
val 変数名 = if(条件式) {
}

使用例1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
val kk = if (bb < 32) {
"this is aa if"
} else {
"this is bb else"
}
val kk = if (bb < 32) { "this is aa if" } else { "this is bb else" }
val kk = if (bb < 32) {
  "this is aa if"
} else {
  "this is bb else"
}

使用例2

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
var target: Int
if (x > y) {
target = x
} else {
target = y
}
var target: Int if (x > y) { target = x } else { target = y }
var target: Int
if (x > y) {
    target = x
} else {
    target = y
}

使用例
val target = if (x > y) x else y

Kotlin

Posted by arkgame