Kotlin
조건문
Jinny96
2022. 7. 22. 19:06
if
fun max1(a : Int, b : Int) : Int {
if(a > b){
return a
} else {
return b
}
}
fun max2(a : Int, b : Int) : Int = if(a > b) a else b
max1과 max2는 같은 기능을 한다.
Java를 비롯해 Kotlin은 삼항연산자가 없다.
when
fun checkNum(score : Int) {
//리턴이 없을 때
when(score) {
1 -> println("score is 1")
2 -> println("score is 2")
3,4 -> println("score is 3 or 4")
else -> println("I don't know")
}
//리턴이 있을 때
var b : Int = when(score) {
1 -> 1
2 -> 2
else -> 3
}
when(score) {
in 90..100 -> println("Your score is 90 ~ 100")
in 10..80 -> println("Your score is 10 ~ 80")
else -> println("Your score is not in 10 ~ 100")
}
}
in 90..100은 90과 100을 포함한다.