Swift

[ Swift ] ํ๋ฆ„ ์ œ์–ด(2) - ๋ฐ˜๋ณต๋ฌธ ( for-in ๊ตฌ๋ฌธ, while ๊ตฌ๋ฌธ )

Forest Yun 2021. 8. 27. 00:38
728x90

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

 

 

 

 

ํ๋ฆ„์ œ์–ด(2) - ๋ฐ˜๋ณต๋ฌธ

  1. for-in ๊ตฌ๋ฌธ
  2. while ๊ตฌ๋ฌธ
  3. repeat-while ๊ตฌ๋ฌธ
  4. ๊ตฌ๋ฌธ ์ด๋ฆ„ํ‘œ

 

 

 

 

 

 

 


1. for-in ๊ตฌ๋ฌธ

  • ํŠน์ • ๊ตฌ๋ฌธ์„ ํŠน์ • ํšŸ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•ด์„œ ์‹คํ–‰ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์ฆ‰, ๋ฐ˜๋ณตํšŸ์ˆ˜๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • continue, break ๋“ฑ์˜ ์ œ์–ด ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • for-in ๊ตฌ๋ฌธ์€ ์Šค์œ„ํ”„ํŠธ์˜ ๊ธฐ๋ณธ Collection Type์ธ ๋ฐฐ์—ด, ๋”•์…”๋„ˆ๋ฆฌ, ์„ธํŠธ์—์„œ๋„ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ์—ฌ๊ธฐ์„œ ๋”•์…”๋„ˆ๋ฆฌ๋Š” ํ‚ค์™€ ๊ฐ’์˜ ์Œ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹œํ€€์Šค ์•„์ดํ…œ์—์„œ ์ž„์‹œ ์ƒ์ˆ˜๋กœ ์š”์†Œ๊ฐ€ ๋„˜์–ด๊ฐˆ ๋•Œ ํŠœํ”Œ๋กœ ์ „๋‹ฌ๋œ๋‹ค. 
  • swift ๋ฌธ๋ฒ•์„ ๋” ๊ณต๋ถ€ํ•˜๋‹ค๋ณด๋ฉด for-in ๊ตฌ๋ฌธ๋ณด๋‹ค map, filter, flatMap ๋“ฑ์„ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค.
  • ์‹œํ€€์Šค ์•„์ดํ…œ์„ ๋‹ด์„ ์ž„์‹œ ์ƒ์ˆ˜๊ฐ€ ํ•„์š” ์—†๋‹ค๋ฉด ์™€์ผ๋“œ ์นด๋“œ ์‹๋ณ„์ž(_)๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
  • ์ค‘์ฒฉํ•˜์—ฌ ๋ฐ˜๋ณต๋ฌธ์„ ์ž‘์„ฑํ•ด๋„ ๋œ๋‹ค.

 

 

๊ธฐ๋ณธ for-in ๊ตฌ๋ฌธ ์‚ฌ์šฉ ์˜ˆ์ œ

for _ in 1...3 {
    print("์ถœ๋ ฅํ•˜๊ธฐ")
}
//์ถœ๋ ฅํ•˜๊ธฐ
//์ถœ๋ ฅํ•˜๊ธฐ
//์ถœ๋ ฅํ•˜๊ธฐ


for i in 1..<5 {
    if(i%2 == 0){
        print("\(i)๋Š” ์ง์ˆ˜์ž…๋‹ˆ๋‹ค. ")
        continue //๋” ์ด์ƒ ์ง„ํ–‰ํ•˜์ง€ ์•Š๊ณ  ๋‹ค์Œ ์‹œํ€€์Šค๋กœ ์ด๋™
    }
    print("\(i)๋Š” ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.")
}
//1๋Š” ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.
//2๋Š” ์ง์ˆ˜์ž…๋‹ˆ๋‹ค.
//3๋Š” ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค.
//4๋Š” ์ง์ˆ˜์ž…๋‹ˆ๋‹ค.


for i in 0..<2 {
    if i == 0 {
        print("0์ด๋ฉด break")
        break //๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•œ๋‹ค.
    }
    print("i๋Š” \(i)")
}
//0์ด๋ฉด break

 

 

 

 

๋ฌธ์ž์—ด์ด ์‹œํ€€์Šค ์•„์ดํ…œ์ธ ๊ฒฝ์šฐ

//๋ฌธ์ž์—ด์ด ์‹œํ€€์Šค ์•„์ดํ…œ์ธ ๊ฒฝ์šฐ for-in ๊ตฌ๋ฌธ ์˜ˆ์ œ
let introduce: String = "Hi! I'm yh!"

for char in introduce {
    print(char,terminator: ".")
}
//print ๋ฉ”์„œ๋“œ์˜ terminator ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ถœ๋ ฅ ํ›„ ๋งˆ์ง€๋ง‰์— ์ถœ๋ ฅ๋˜๋Š” ๋ฌธ์ž์—ด์ด๋‹ค. ๊ธฐ๋ณธ์€ ์ค„๋ฐ”๊ฟˆ ๋ฌธ์ž "\n"

 

 

 

 

์ปฌ๋ ‰์…˜ ํƒ€์ž…์ด ์‹œํ€€์Šค ์•„์ดํ…œ์ธ ๊ฒฝ์šฐ

https://88yhtserof.tistory.com/22?category=966954 

 

[ SWIFT ] ๋ฐ์ดํ„ฐ ํƒ€์ž… ๊ณ ๊ธ‰ (2) - ์ปฌ๋ ‰์…˜ ํƒ€์ž… <๋ฐฐ์—ด, ๋”•์…”๋„ˆ๋ฆฌ, ์„ธํŠธ>

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ํƒ€์ž… ๊ณ ๊ธ‰(2) - ์ปฌ๋ ‰์…˜ํ˜• ์ปฌ๋ ‰์…˜ ํƒ€์ž… ๋ฐฐ์—ด ๋”•์…”๋„ˆ๋ฆฌ ์„ธํŠธ 1. ์ปฌ๋ ‰์…˜ ํƒ€์ž… Collection Types SWIFT์—์„œ ์ปฌ๋ ‰์…˜ ํƒ€์ž…(Collection Type)์€ ๋ฐ์ดํ„ฐ ๋ฌถ์Œ์„ ์˜๋ฏธํ•œ๋‹ค. SWIFT

88yhtserof.tistory.com

//๋ฐฐ์—ด
let numberArray: [Int] = [1,2,3,4]

for num in numberArray {
    print(num)
}
//1
//2
//3
//4

//๋”•์…”๋„ˆ๋ฆฌ - ํ‚ค์™€ ๊ฐ’ ์Œ์˜ ์ˆœ์„œ ์—†๋Š” ์ปฌ๋ ‰์…˜ ํƒ€์ž…
let contactAddress: [String: Int] = ["renee": 1124, "ari": 478]

for tuple in contactAddress {
    print("\(tuple.key)์˜ ์—ฐ๋ฝ์ฒ˜๋Š” \(tuple.value)์ด๋‹ค.")
}
 
//์„ธํŠธ - ์ˆœ์„œ ์—†์ด ํ•˜๋‚˜์˜ ๋ฌถ์Œ์œผ๋กœ ์ €์žฅํ•˜๋Š” ํ˜•ํƒœ์˜ ์ปฌ๋ ‰์…˜ ํƒ€์ž…
let myInfo: Set<String> = ["yh","123-456", "red", "apple"]

for info in myInfo {
    print(info, terminator: " ")
}

 

 

 

 

 

 

 

 

 


2. While ๊ตฌ๋ฌธ

  • ์ฃผ์–ด์ง„ ์กฐ๊ฑด์‹์ด false๊ฐ€ ๋  ๋•Œ๊นŒ์ง€, ์ฆ‰ ๋ฐ˜๋ณต ํšŸ์ˆ˜๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์ง€ ์•Š์„ ๋•Œ, ์•Œ์ง€ ๋ชปํ•  ๋•Œ while ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • continue, break ๋“ฑ ์ œ์–ด ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ์ค‘์ฒฉํ•˜์—ฌ ๋ฐ˜๋ณต๋ฌธ์„ ์ž‘์„ฑํ•ด๋„ ๋œ๋‹ค.
var num: Int = 5

while num != 0 {
    if(num>0){
        num-=1
        print("ํ˜„์žฌ num์€ \(num)")
        continue
    }
    num+=1
    print("ํ˜„์žฌ num์€ \(num)")
}
ํ˜„์žฌ num์€ 3
ํ˜„์žฌ num์€ 2
ํ˜„์žฌ num์€ 1
ํ˜„์žฌ num์€ 0

 

 

 

 

 

 

 

 

 

 


3. repeat - while ๊ตฌ๋ฌธ

๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ do-while๊ณผ ํก์‚ฌํ•˜๋‹ค. ๋จผ์ € repeat ๋ธ”๋ก์„ ์‹คํ–‰ํ•œ ํ›„ while ๊ตฌ๋ฌธ์˜ ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•˜๋ฉด ๋‹ค์Œ ๋ฐ˜๋ณต์„ ์ง„ํ–‰ํ•  ์ง€ ๊ฒฐ์ •ํ•œ๋‹ค. ๋งŒ์•ฝ ํ•œ ๋ฒˆ๋„ ์กฐ๊ฑด์‹์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด, repeat ๋ธ”๋ก์ด ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋˜์—ˆ์„ ๊ฒƒ์ด๋‹ค.

 

 

 

 

 

 

 

 

 


4. ๊ตฌ๋ฌธ ์ด๋ฆ„ํ‘œ

๋ฐ˜๋ณต๋ฌธ ์•ž์— ์ด๋ฆ„๊ณผ ํ•จ๊ป˜ ์ฝœ๋ก ์„ ๋ถ™์—ฌ ๊ตฌ๋ฌธ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์„ ๊ตฌ๋ฌธ ์ด๋ฆ„ํ‘œ๋ผ๊ณ  ํ•œ๋‹ค. ์ค‘์ฒฉํ•˜์—ฌ ๋ฐ˜๋ณต๋ฌธ์„ ์ž‘์„ฑํ•  ๋•Œ ์ œ์–ด ํ‚ค์›Œ๋“œ์˜ ์‚ฌ์šฉ ๋ฒ”์œ„๊ฐ€ ์• ๋งคํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋Ÿด ๋•Œ ๊ตฌ๋ฌธ ์ด๋ฆ„ํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ •ํ™•ํ•˜๊ฒŒ ์›ํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฆ„์ด ์ง€์ •๋œ ๊ตฌ๋ฌธ์„ ์ œ์–ดํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” ์ œ์–ด ํ‚ค์›Œ๋“œ์™€ ๊ตฌ๋ฌธ ์ด๋ฆ„์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

let employees: [String:Int] = ["๊น€ํ•˜๋‚˜": 1, "๊ฐ•๋‘˜": 2, "์†ก์…‹": 3, "์ง„๋„ท": 4]
let retirees = "๊ฐ•๋‘˜"


employeesLoop: for employeeInfo in employees {
    var payGrade = 0
    var salary = 0
    
    print("\(employeeInfo.key)", terminator: " ")
    
    salaryCalLoop: while employeeInfo.value >= payGrade {
        salary+=100
        payGrade+=1
        
        if employeeInfo.key == retirees {
            print("ํ‡ด์ง์ž์ž…๋‹ˆ๋‹ค.")
            continue employeesLoop
        }
    }
    print("์˜ ์›”๊ธ‰์€ \(salary)์ด๋‹ค.")
}
/*
 ์†ก์…‹ ์˜ ์›”๊ธ‰์€ 400์ด๋‹ค.
 ๊ฐ•๋‘˜ ํ‡ด์ง์ž์ž…๋‹ˆ๋‹ค.
 ๊น€ํ•˜๋‚˜ ์˜ ์›”๊ธ‰์€ 200์ด๋‹ค.
 ์ง„๋„ท ์˜ ์›”๊ธ‰์€ 500์ด๋‹ค.
 */

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

๊ณต๋ถ€ ์ž๋ฃŒ

์•ผ๊ณฐ - ์Šค์œ„ํ”„ํŠธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ 5ํŒ (SWIFT5)
Do It! ์Šค์œ„ํ”„ํŠธ๋กœ ์•„์ดํฐ ์•ฑ ๋งŒ๋“ค๊ธฐ ๊ฐœ์ • 5ํŒ
728x90