아카이브로 돌아가기

TIL

20221026

for-infor-in 구문의 기본 형태컬렉션 타입을 활용한 for-in 구문튜플 타입(Tuple) : 여러가지 타입의 복수 값들을 그룹화한 단일 값whilewhile 구문의 기본 형태컬렉션 타입을 활용한 while 구문repeat-whilerepeat-while 구문

Velog 원문 보기

반복문

  1. for-in
  • for-in 구문의 기본 형태
for item in items {
    /* 실행 구문 */
}
  • 컬렉션 타입을 활용한 for-in 구문
var integersArray = [2, 4, 6]
for integer in integersArray {
    print(integer)
}
// 2
// 4
// 6
// (3번 반복 실행)

let englishDictionary = ["elephant": "코끼리", "lion": "사자"]
for (english, korean) in englishDictionary {
    print("\(english): \(korean)")
}
// elephant: 코끼리
// lion: 사자
// (2번 반복 실행)
// Dictionary의 item은 key와 value로 구성된 튜플 타입
  • 튜플 타입(Tuple) : 여러가지 타입의 복수 값들을 그룹화한 단일 값
  1. while
  • while 구문의 기본 형태
while 조건 {
    /* 실행 구문 */
}
// 조건에는 반드시 boolean 값
  • 컬렉션 타입을 활용한 while 구문
var integersArray = [2, 4, 6]

while integersArray.count > 1 {
    integersArray.removeLast()
}
// [2, 4]
// [2]
// (2번 반복 실행)
  1. repeat-while
  • repeat-while 구문의 기본 형태
repeat {
    /* 실행 구문 */
} while 조건
// 조건에는 반드시 boolean 값
  • 컬렉션 타입을 활용한 repeat-while 구문
var integersArray = [2, 4, 6]

repeat {
    integersArray.removeLast()
} while integersArray.count > 1
// [2, 4]
// [2]
// (2번 반복 실행)
아카이브로 돌아가기