반복문

반복문

  • for-in
  • while
  • repeat-while

소스코드

for-in 구문

기존 언어의 for-each 구문과 유사합니다. Dictionary의 경우 이터레이션 아이템으로 튜플이 들어옵니다. 튜플에 관해서는 Swift Language Guide의 Tuples 부분을 참고하면 되겠습니다.

for-in 구문의 기본 형태

for item in items {
    /* 실행 구문 */
}

for-in 구문의 사용

var integers = [1, 2, 3]
let people = ["yagom": 10, "eric": 15, "mike": 12]

for integer in integers {
    print(integer)
}

// Dictionary의 item은 key와 value로 구성된 튜플 타입입니다
for (name, age) in people {
    print("\(name): \(age)")
}

while 구문

while 구문의 기본 형태

while 조건 {
    /* 실행 구문 */
}

while 구문의 사용

while integers.count > 1 {
    integers.removeLast()
}

repeat-while 구문

기존 언어의 do-while 구문과 형태 및 동작이 유사합니다

repeat-while 구문의 기본 형태

repeat {
    /* 실행 구문 */
} while 조건

repeat-while 구문의 사용

repeat {
    integers.removeLast()
} while integers.count > 0

관련문서

by yagom


facebook : https://www.facebook.com/yagompage
facebook group : https://www.facebook.com/groups/yagom/

p.s 제 포스팅을 RSS 피드로 받아보실 수 있습니다.
RSS Feed 받기  

댓글 남기기

Close