Swift랑 친해지기/Swift문법정리

10. 반복문

데브킹덕 2021. 12. 21. 16:56

* for -in

* while

* repeat - while

 

1. for - in 구문

- 기존 언어의 for -each 구문과 유사합니다. 

- Dictionary의 경우 이터레이션 아이템으로 튜플이 들어옵니다. 

 

for - in 구조

for item in items{

  /* 실행 구문*/

}

 

코드로 보기

var integers = [1,2,3]                         //integers 타입에 Array 생성

let people = ["yagom":10, "eric":15, "mike": 12]     //integers 타입에 Dictionary 생성

 

for integer in integers{                //for 뒤에 이터레이션(반복)으로 들어올 in 뒤에 이터레이션(반복)으로 돌 콜렉션타입 

   print(integer)

}

 

//Dictionary의 item은 key와 value로 구성퇸 튜플 타입입니다. 

for(name, age) in people{

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

}

 

2. while 구문

while문 구조

while 조건{

  /* 실행 구문*/

}

 

코드로 보기

while integers.count > 1{

  integers.removeLast()

}

 

3. repeat - while 구문

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

 

repeat - while문 구조

repeat{

  /* 실행 구문*/

}while 조건

 

코드로 보기

repeat{

  integers.removeLast()           --------> 먼저 실행 된 이후

} while integers.count > 0           --------> 조건 확인 후 반복

 

 

 

 

'Swift랑 친해지기 > Swift문법정리' 카테고리의 다른 글

12. 옵셔널 추출  (0) 2021.12.22
11.옵셔널  (0) 2021.12.22
09. 조건문  (0) 2021.12.21
08.함수고급  (0) 2021.12.21
07. 함수기본  (0) 2021.12.02