공부/SWIFT

[SWIFT] 제어문( 반복문 while, repeat-while )

알로하리미 2021. 3. 10. 11:10
728x90

1. 개요

특정 조건문은 Bool 타입 이어야한다.

repeat-while은 다른언어에서 do-while 문이라고 보면된다.( 무조건 한번은 실행시킨다. ) 

continue(다음으로 pass), break(중지)로 흐름제어가 가능하다.

 

2. while

조건(condition)이 거짓(false)일때까지 구문(statements)을 반복

var square = 0
var diceRoll = 0
while square < finalSquare {
    // roll the dice
    diceRoll += 1
    if diceRoll == 7 { diceRoll = 1 }
    // move by the rolled amount
    square += diceRoll
    if square < board.count {
        // if we're still on the board, move up or down for a snake or a ladder
        square += board[square]
    }
}
print("Game over!")

 

3. repeat-while

구문(statements)을 최소 한번 이상 실행하고 while 조건이 거짓일 때까지 반복

repeat {
    // move up or down for a snake or ladder
    square += board[square]
    // roll the dice
    diceRoll += 1
    if diceRoll == 7 { diceRoll = 1 }
    // move by the rolled amount
    square += diceRoll
} while square < finalSquare
print("Game over!")

 

아래 url을 통해 제어 전송 구문을 확인 할 수 있다. break, continue, fallthrough

2021.03.10 - [분류 전체보기] - [SWIFT] 제어 전송 구문