Kotlin 강좌 6 - 반복문과 증감연산자

dyeon-dev·2023년 10월 7일
0

Kotlin

목록 보기
4/17
post-thumbnail

반복문

  • while
  • do while
fun main() {
    var a = 0
    do {
        println(++a)
    }
    while (a<5)
}
  • for
fun main() {
    for(i in 0..9 step 3) {
        print(i)
    }
}
fun main() {
    for(i in 9 downTo 0) {
        print(i)
    }
}
fun main() {
    for(i in 'a'..'e') {
        print(i)
    }
}

흐름제어와 논리연산자

외부반복문에 레이블 이름@기호를 달고 break문에서 @과 레이블 이름을 달아주면 레이블이 달린 반복문을 기준으로 break를 시켜준다.

fun main() {
    loop@for(i in 1..10) {
        for(j in 1..10) {
            if(i==1 && j==2) break@loop
            println("i: $i, j: $j")
        }
    }
}

i: 1, j: 1

0개의 댓글