Kotlin 조건문과 반복문 (Conditional Statements and Loops in Kotlin)
조건문 (Conditional Statements)
Kotlin에서는 if, else if, else를 사용하여 조건문을 작성할 수 있습니다.
기본적인 if-else 예시:
val number = 10
if (number > 0) {
println("양수입니다.")
} else if (number < 0) {
println("음수입니다.")
} else {
println("0입니다.")
}
반복문 (Loops)
Kotlin에서 반복문으로는 for와 while을 사용할 수 있습니다.
for 루프 예시:
val numbers = listOf(1, 2, 3, 4, 5)
for (num in numbers) {
println(num)
}
while 루프 예시:
var count = 0
while (count < 5) {
println("현재 카운트: $count")
count++
}
when 표현식 (When Expression)
when 표현식을 사용하여 여러 조건을 처리할 수 있습니다.
when 표현식 예시:
val day = 3
when (day) {
1 -> println("월요일")
2 -> println("화요일")
3 -> println("수요일")
else -> println("나머지 요일")
}
Kotlin의 조건문과 반복문을 활용하여 다양한 로직을 구현할 수 있습니다.
