📌 01. if문
fun maxBy( a:Int, b:Int ) : Int {
if(a>b) return a
else retrun b
}
// return타입 생략식
fun maxBy( a:Int, b:Int ) = if(a > b) a else b
📌 02. when문
fun test( score:Int ) {
when(score) {
in 90..100 -> println("nice")
in 70..90 -> println("not bad")
in 50..70 -> println("bad")
}
// 변수 초기화에도 사용 가능
val str = when(score) {
in 90..100 -> "my score is ${score}"
in 70..90 -> "my score is ${score}"
in 50..70 -> "my score is ${score}"
else -> "My test score is lower than 50."
}
print("${str}")
}
when문이 변수 초기화에 사용될 때, else는 반드시 포함시켜야 한다.
'Kotlin' 카테고리의 다른 글
Day05_반복문 (0) | 2022.09.17 |
---|---|
Day03_문자열 템플릿 (0) | 2022.09.14 |
Day02_변수 선언 (0) | 2022.09.14 |
Day01_함수 (0) | 2022.09.14 |