๐Ÿฆ… Swift

Control Flow

Kiwi๐Ÿ’ป 2022. 6. 1. 12:17

Swift๋Š” ๋‹ค์–‘ํ•œ ์ œ์–ด ํ๋ฆ„๋ฌธ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค. ๊ทธ ์ข…๋ฅ˜๋กœ๋Š” while, if, guard, switch ๊ตฌ๋ฌธ๊ณผ ๋‹ค๋ฅธ ์ฝ”๋“œ์˜ ์‹คํ–‰์œผ๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ํ•ด์ฃผ๋Š” break์™€ continue๋ฌธ๋„ ์กด์žฌํ•œ๋‹ค.

Swift๋Š” ๋˜ํ•œ ๋ฐฐ์—ด, ๋”•์…”๋„ˆ๋ฆฌ, ๋ฒ”์œ„, ๋ฌธ์ž์—ด, ๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ์—ฐ์†์ ์ธ ๊ฒƒ์— ๋Œ€ํ•œ ๋ฐ˜๋ณต์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค์–ด ์ฃผ๋Š” for-in ๋ฃจํ”„๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

For-In ๋ฃจํ”„(For-IN Loops)

๋ฐฐ์—ด์˜ ์š”์†Œ, ํŠน์ • ๋ฒ”์œ„์˜ ์ˆซ์ž, ๋ฌธ์ž์—ด ๊ฐ™์€ ์—ฐ์†์ ์ธ ๊ฒƒ๋“ค์„ for-in ๋ฃจํ”„๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ˜๋ณต ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
    print("Hello, \(name)!")
}
// Hello, Anna!
// Hello, Alex!
// Hello, Brian!
// Hello, Jack!

๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค ๋ฐ ๋ฐ์ดํ„ฐ ๊ฐ’์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”•์…”๋„ˆ๋ฆฌ์˜ ๊ฐ ์š”์†Œ๋“ค์€ ๋ฐ˜๋ณต๋ฌธ์„ ๊ฑฐ์ณ ํŠœํ”Œ๋กœ ๋ฐ˜ํ™˜๋  ์ˆ˜ ์žˆ๋‹ค.

let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {
    print("\(animalName)s have \(legCount) legs")
}
// cats have 4 legs
// ants have 6 legs
// spiders have 8 legs

์ˆซ์ž ๋ฒ”์œ„์— ๋Œ€ํ•œ ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25

๋ฒ”์œ„๋ฅผ ๊ฑด๋„ˆ ๋›ฐ๋Š” stride ๋ฐ˜๋ณต๋ฌธ๋„ ์กด์žฌ ํ•œ๋‹ค.

let minuteInterval = 5
for tickMark in stride(from: 0, to: minutes, by: minuteInterval) {
    // render the tick mark every 5 minutes (0, 5, 10, 15 ... 45, 50, 55)
}

let hours = 12
let hourInterval = 3
for tickMark in stride(from: 3, through: hours, by: hourInterval) {
    // render the tick mark every 3 hours (3, 6, 9, 12)
}

While ๋ฃจํ”„ (While Loops)

while ๋ฃจํ”„๋Š” ์–ด๋– ํ•œ ์กฐ๊ฑด์ด false๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฌธ์žฅ์ด๋‹ค. ๋˜ํ•œ 2๊ฐ€์ง€ while ๋ฃจํ”„๊ฐ€ ์กด์žฌ ํ•œ๋‹ค.

  • while ์€ ๋ฃจํ”„๊ฐ€ ์‹œ์ž‘ํ•  ๋•Œ๋งˆ๋‹ค ์กฐ๊ฑด์„ ๋น„๊ตํ•œ๋‹ค.
  • repeat-while ์€ ๋ฃจํ”„๊ฐ€ ๋๋‚  ๋•Œ ๋งˆ๋‹ค ์กฐ๊ฑด์„ ๋น„๊ตํ•œ๋‹ค.

while

while ๋ฃจํ”„๋Š” ๋‹จ์ผ ์กฐ๊ฑด์„ ํ‰๊ฐ€ํ•จ์œผ๋กœ ์‹œ์ž‘ํ•œ๋‹ค. ์กฐ๊ฑด์ด false ๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ์ค‘๊ด„ํ˜ธ ์•ˆ์˜ ๊ตฌ๋ฌธ์€ ๋ฐ˜๋ณต ๋œ๋‹ค.

Repeat-while

๋ฃจํ”„์˜ ์กฐ๊ฑด์„ ํŒ๋‹จํ•˜๊ธฐ ์ „์— ๋ฃจํ”„ ๋ธ”๋Ÿญ์„ ์ฒ˜์Œ์— ํ•œ๋ฒˆ ๋จผ์ € ํ†ต๊ณผํ•œ ํ›„ ์กฐ๊ฑด์„ ํŒ๋‹จํ•˜๋Š” repeat-while ๋ฃจํ”„๊ฐ€ ์žˆ๋‹ค. ์กฐ๊ฑด์ด false ๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๋ฃจํ”„๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.

์กฐ๊ฑด ๊ตฌ๋ฌธ (Conditional Statements)

Swift๋Š” ์ฝ”๋“œ์— ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ if ๊ตฌ๋ฌธ๊ณผ switch ๊ตฌ๋ฌธ์ด ์žˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ if ๊ตฌ๋ฌธ์€ ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์ ์€ ๊ฐ„๋‹จํ•œ ์กฐ๊ฑด์— ์ ํ•ฉํ•˜๋‹ค. switch ๊ตฌ๋ฌธ์€ ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ด๋ฉฐ ๋” ๋ณต์žกํ•œ ์กฐ๊ฑด์— ์ ํ•ฉํ•˜๊ณ  ์‹คํ–‰ํ•ด์•ผ ํ•  ์ ์ ˆํ•œ ์ฝ”๋“œ ๋ถ„๊ธฐํ•ด์•ผ ํ•˜๋Š” ํŒจํ„ด ๋งค์นญ ์ƒํƒœ์— ์œ ์šฉํ•˜๋‹ค.

If

๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ํ˜•์‹์œผ๋กœ if ๊ตฌ๋ฌธ์€ ๋‹จ์ผ ์กฐ๊ฑด์„ ๊ฐ–๋Š”๋‹ค. ์กฐ๊ฑด์ด true ์ผ ๊ฒฝ์šฐ์—๋งŒ ๊ตฌ๋ฌธ์„ ์‹คํ–‰ํ•œ๋‹ค.

 temperatureInFahrenheit = 40
if temperatureInFahrenheit <= 32 {
    print("It's very cold. Consider wearing a scarf.")
} else {
    print("It's not that cold. Wear a t-shirt.")
}
// Prints "It's not that cold. Wear a t-shirt."

2๊ฐœ์˜ ์ค‘๊ด„ํ˜ธ ์ค‘ ํ•˜๋‚˜๋Š” ํ•ญ์ƒ ์‹คํ–‰๋œ๋‹ค. ๊ธฐ์˜จ์ด ์ฆ๊ฐ€ํ•˜์—ฌ ํ™”์”จ 40๋„๊ฐ€ ๋˜๋ฉด ์Šค์นดํ”„๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ๋งํ•˜์ง€ ์•Š์œผ๋ฉฐ, else ๊ตฌ๋ฌธ์ด ์‹คํ–‰ ๋ฉ๋‹ˆ๋‹ค.

Switch

switch ๊ตฌ๋ฌธ์€ ์—ฌ๋Ÿฌ ๊ฐ€๋Šฅํ•œ ์ƒํƒœ์— ์‘๋‹ตํ•˜๊ธฐ ์œ„ํ•ด if ๊ตฌ๋ฌธ์˜ ๋Œ€์ฒด ๊ตฌ๋ฌธ์œผ๋กœ ์ œ๊ณต๋œ๋‹ค.

switch๋Š” ๋ชจ๋“  ์ผ€์ด์Šค๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•˜๋ฉฐ, ๋˜ํ•œ ๊ธฐ๋ณธ ์ผ€์ด์Šค๋Š” default ํ‚ค์›Œ๋“œ๋กœ ๋‚˜ํƒ€๋‚ด๊ณ  ํ•ญ์ƒ ๋งˆ์ง€๋ง‰์— ์œ„์น˜ํ•ด์•ผ ํ•œ๋‹ค.

์ œ์–ด ์ „์†ก๋ฌธ(Control Transfer Statements)

Continue

๋ฐ˜๋ณต๋ฌธ์—์„œ ๋‹ค์Œ ์ฃผ๊ธฐ๋กœ ๋„˜์–ด๊ฐ€์„œ ๊ณ„์†ํ•จ(continue)์„ ์˜๋ฏธ

continue๋ฅผ ๋งŒ๋‚˜๋ฉด ๋‹ค์Œ ๋ฌธ์žฅ๋“ค์„ ๋ฌด์‹œํ•˜๊ณ  ์‹ธ์ดํด๋กœ ๋„˜์–ด๊ฐ

let puzzleInput = "great minds think alike"
var puzzleOutput = ""
let charactersToRemove: [Character] = ["a", "e", "i", "o", "u", " "]
for character in puzzleInput {
    if charactersToRemove.contains(character) {
        continue
    }
    puzzleOutput.append(character)
}
print(puzzleOutput)
// Prints "grtmndsthnklk"

Break

๋ฐ˜๋ณต๋ฌธ์„ ์•„์˜ˆ ์ค‘์ง€
breake๋ฅผ ๋งŒ๋‚˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์˜ ์‹ธ์ดํด์„ ์ค‘์ง€ํ•˜๊ณ  ๋ฐ˜๋ณต๋ฌธ์„ ๋ฒ—์–ด๋‚œ ๋‹ค์Œ ๋ฌธ์žฅ์œผ๋กœ ๋„˜์–ด๊ฐ

Fallthrough

๋งค์นญ๋œ ๊ฐ’์— ๋Œ€ํ•œ ๊ณ ๋ ค์—†์ด, ๋ฌด์กฐ๊ฑด ๋‹ค์Œ๋ฌธ์žฅ๋„ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ์„ ๋•, fallthrough ํ‚ค์›Œ๋“œ ์‚ฌ์šฉํ•œ๋‹ค.

let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
    description += " a prime number, and also"
    fallthrough
default:
    description += " an integer."
}
print(description)
// Prints "The number 5 is a prime number, and also an integer."

๋ผ๋ฒจ์ด ์žˆ๋Š” ๊ตฌ๋ฌธ (Labeled Statements)

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

gameLoop: while square != finalSquare {
    diceRoll += 1
    if diceRoll == 7 { diceRoll = 1 }
    switch square + diceRoll {
    case finalSquare:
        // diceRoll will move us to the final square, so the game is over
        break gameLoop
    case let newSquare where newSquare > finalSquare:
        // diceRoll will move us beyond the final square, so roll again
        continue gameLoop
    default:
        // this is a valid move, so find out its effect
        square += diceRoll
        square += board[square]
    }
}
print("Game over!")

'๐Ÿฆ… Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Closures  (0) 2022.06.15
Functions  (0) 2022.06.01
Collection Types  (0) 2022.05.22
Strings and Characters  (0) 2022.05.22
Basicย Operators  (0) 2022.05.15