반복문

Swift/문법 2021. 1. 27. 14:09

for in

for item int items {

code

}

 

for integers in integers {

print(integer)

}  

 

* Dictionary의 item은 key와 value로 구성된 튜플 타입

for (name, age) in people {   // name에는 key값이, age에는 value값이 들어옴

print("\(name): \(age)")

}

 

 

while

- 조건문처럼 조건 부분에 ()는 선택사항

- 조건 부분에는 다른 값이 아니라 꼭 Bool 값이 들어가야 함

 

while integers.count > 1 {

integers.removeLast()

}

 

 

repeat while

- do while과 비슷함

 

repeat {

integers.removeLast()   // 먼저 실행이 된 후에

} while integers.count > 0   // while에서 조건을 체크한 이후에 계속 반복을 실행

'Swift > 문법' 카테고리의 다른 글

옵셔널 추출(Optional Unwrapping)  (0) 2021.01.27
옵셔널(Optional)  (0) 2021.01.27
조건문  (0) 2021.01.27
함수 고급  (0) 2021.01.27
함수 기본  (0) 2021.01.27
Posted by khon98
,