Kotlin
반복문 — for, while, repeat
Range, withIndex, until, downTo, step 등 Kotlin 반복문의 모든 패턴을 익힙니다.
// 닫힌 범위 1..5 (5 포함)
for (i in 1..5) print("$i ") // 1 2 3 4 5
// 열린 범위 until (5 미포함)
for (i in 1 until 5) print("$i ") // 1 2 3 4
// 역순
for (i in 5 downTo 1) print("$i ") // 5 4 3 2 1
// 스텝
for (i in 0..10 step 2) print("$i ") // 0 2 4 6 8 10
// 컬렉션 순회
val fruits = listOf("🍎", "🍌", "🍇")
for (fruit in fruits) println(fruit)
// 인덱스와 값 동시에
for ((idx, fruit) in fruits.withIndex()) {
println("$idx: $fruit")
}
// while
var n = 5
while (n > 0) { print("$n "); n-- }
// repeat
repeat(3) { i -> println("반복 $i") }