Deep_Dev
article thumbnail

 

๐Ÿ“– ํ๋ฆ„์ œ์–ด 

ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋‹ค๋ณด๋ฉด ํŠน์ • ์กฐ๊ฑด์—์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ด์•ผ ํ•˜๊ฑฐ๋‚˜ ์‹คํ–‰ํ•˜์ง€ ๋ง์•„์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ์ƒ๊ธฐ๊ธฐ ๋งˆ๋ จ์ด๋‹ค. 

๋˜, ํŠน์ • ๋ช…๋ น์–ด๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์‹คํ–‰ํ•ด์•ผ ํ•˜๋Š” ์ผ๋„ ์ข…์ข… ๋ฐœ์ƒํ•œ๋‹ค. ์ด๋Ÿด๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ˜๋ณต๋ฌธ์ด๋‹ค.

 

์Šค์œ„ํ”„ํŠธ์˜ ํ๋ฆ„์ œ์–ด ๊ตฌ๋ฌธ์—์„œ๋Š” ์†Œ๊ด„ํ˜ธ()๋ฅผ ๋Œ€๋ถ€๋ถ„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌผ๋ก  ์‚ฌ์šฉํ•ด๋„ ๋ฌด๊ด€ํ•˜์ง€๋งŒ ์ค‘๊ด„ํ˜ธ{} ๋Š” ์ƒ๋žตํ•  ์ˆ˜ ์—†๋‹ค.

 

 

๐Ÿ“– ์กฐ๊ฑด๋ฌธ

๐Ÿ“Œ if ๊ตฌ๋ฌธ

์Šค์œ„ํ”„ํŠธ์˜ if ๊ตฌ๋ฌธ์€ ์กฐ๊ฑด์˜ ๊ฐ’์ด ๊ผญ Bool ํƒ€์ž…์ด์–ด์•ผ ํ•œ๋‹ค. 

let first: Int = 5
let second: Int = 7

if first > second{
	print("first > second")
} else if first < second {
	print("first < second")
} else {
	print("first == second")
}
  • else if๋Š” ๋ช‡๊ฐœ๊ฐ€ ์ด์–ด์ ธ๋„ ์ƒ๊ด€์—†์œผ๋ฉฐ else ๋ธ”๋ก์€ ์—†์–ด๋„ ์ƒ๊ด€์—†๋‹ค.
  • if ์กฐ๊ฑด์„ ์ถฉ์กฑํ•ด ๋ธ”๋ก ๋‚ด๋ถ€์˜ ๋ช…๋ น๋ฌธ์ด ์‹คํ–‰๋˜๋ฉด ๊ทธ ๋‹ค์Œ์— ์ด์–ด์ง„ else if ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜๋”๋ผ๋„ ์‹คํ–‰๋˜์ง€ ์•Š๊ณ  
    ์กฐ๊ฑด๋ฌธ์„ ๋น ์ ธ๋‚˜์˜จ๋‹ค.

 

๐Ÿ“Œ switch ๊ตฌ๋ฌธ

์Šค์œ„ํ”„ํŠธ์˜ switch ๊ตฌ๋ฌธ๋„ ์†Œ๊ด„ํ˜ธ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๊ณ , break ๋Š” ์„ ํƒ์‚ฌํ•ญ์ด๋‹ค.

์ฆ‰, case ๋‚ด๋ถ€์˜ ์ฝ”๋“œ๋ฅผ ๋ชจ๋‘ ์‹คํ–‰ํ•˜๋ฉด break ์—†์–ด๋„ switch ๊ตฌ๋ฌธ์ด ์ข…๋ฃŒ๋œ๋‹ค๋Š” ๋œป์ด๋‹ค. ์Šค์œ„ํ”„ํŠธ์—์„œ switch ๊ตฌ๋ฌธ์˜ case๋ฅผ ์—ฐ์† ์‹คํ–‰ํ•˜๋ ค๋ฉด fallthrough ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

C์–ธ์–ด์—์„œ๋Š” ์ •์ˆ˜ํƒ€์ž…๋งŒ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์—ˆ์œผ๋‚˜ ์Šค์œ„ํ”„ํŠธ์—์„œ๋Š” switch ๊ตฌ๋ฌธ์˜ ์กฐ๊ฑด์— ๋‹ค์–‘ํ•œ ๊ฐ’์ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

๋˜ ๋น„๊ต๋  ๊ฐ’์ด ๋ช…ํ™•ํžˆ ํ•œ์ •์ ์ธ ๊ฐ’์ด ์•„๋‹๋•Œ๋Š” default๋ฅผ ๊ผญ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

 

 

switch ๊ตฌ๋ฌธ ๊ธฐ๋ณธ ๊ตฌํ˜„

let integerValue: Int = 5

switch intergerValue{
	case 0:
    	print("Value==zero")
    case 1...10:
    	print("Value==1~10")
        fallthrough // ๋งŒ์กฑํ•ด๋„ ํƒˆ์ถœํ•˜์ง€ ์•Š๊ณ  ์•„๋ž˜ case๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
    case Int.min..<0, 101..<Int.max:
    	print("Value < 0 or Value > 100")
        break // ์„ ํƒ์‚ฌํ•ญ
    default: // ํ•œ์ •๋œ ๋ฒ”์œ„๊ฐ€ ๋ช…ํ™•ํ•˜์ง€ ์•Š๋‹ค๋ฉด default๋Š” ํ•„์ˆ˜ 
    	print("10 < Value <= 100")
}

 

๋ฌธ์ž์—ด switch case ๊ตฌ์„ฑ

let stringValue: String = "Liam Neeson"

switch stringValue{
	case "yagom" :
    	print("He is yagom")
    case "Jay":
    	print("He is Jay")
    case "Jenny", "Joker", "Nova":
    	print("He or She is \(stringValue)")
    default:
    	print("\(stringValue) said 'I don't know who you are'")
}

// Liam Neeson said 'I don't know who you are'

 

 

๐Ÿ“– ๋ฐ˜๋ณต๋ฌธ

๐Ÿ“Œ for-in ๊ตฌ๋ฌธ

๊ธฐ๋ณธ ํ˜•ํƒœ

for ์ž„์‹œ์ƒ์ˆ˜ in ์‹œํ€€์Šค ์•„์ดํ…œ{
	์‹คํ–‰์ฝ”๋“œ
}

 

for-in ๋ฐ˜๋ณต ๊ตฌ๋ฌธ์˜ ํ™œ์šฉ

for i in 0...2{
	print(i)
}

// 0
// 1
// 2

for i in 0...5{
	if i.isMultiple(of:2){
    	print(i)
        continue // continue๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ”๋กœ ๋‹ค์Œ ์‹œํ€€์Šค๋กœ ๊ฑด๋„ˆ๋›ด๋‹ค.
    }
    print("\(i) == ํ™€์ˆ˜")
}

// 0
// 1 == ํ™€์ˆ˜
// 2
// 3 == ํ™€์ˆ˜
// 4
// 5 == ํ™€์ˆ˜

 

๐Ÿ“Œ while ๊ตฌ๋ฌธ

var names: [String] = ["Joker", "Jenny", "Nova", "yagom"]

while names.isEmpty == false{
	print("Good bye \(names.removeFirst())")
    // removeFirst()๋Š” ์š”์†Œ๋ฅผ ์‚ญ์ œํ•จ๊ณผ ๋™์‹œ์— ์‚ญ์ œํ•œ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
}

// Good bye Joker
// Good bye Jenny
// Good bye Nova
// Good bye yagom

 

๐Ÿ“Œ repeat-while ๊ตฌ๋ฌธ

repeat-while ๋ฐ˜๋ณต ๊ตฌ๋ฌธ์€ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ do-while ๊ตฌ๋ฌธ๊ณผ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š๋‹ค.

repeat ๋ธ”๋ก ์ฝ”๋“œ๋ฅผ ์ตœ์ดˆ 1ํšŒ ์‹คํ–‰ํ•œ ํ›„, while ๋‹ค์Œ์˜ ์กฐ๊ฑด์ด ์„ฑ๋ฆฝํ•˜๋ฉด ๋ธ”๋ก ๋‚ด๋ถ€์˜ ์ฝ”๋“œ๋ฅผ ๋ฐ˜๋ณต ์‹คํ–‰ํ•œ๋‹ค.

var names: [String] = ["A", "B", "C", "D"]

repeat{
	print("Good bye \(names.removeFirst())")
} while names.isEmpty == false

// Good bye A
// Good bye B
// Good bye C
// Good bye D

 

๐Ÿ“Œ ๊ตฌ๋ฌธ ์ด๋ฆ„ํ‘œ

๋ฐ˜๋ณต๋ฌธ์„ ์ž‘์„ฑํ•˜๋‹ค๋ณด๋ฉด ์ข…์ข… ๋ฐ˜๋ณต๋ฌธ์„ ์ค‘์ฒฉ์œผ๋กœ ์ž‘์„ฑํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๋•Œ ๋ฐ˜๋ณต๋ฌธ์„ ์ œ์–ดํ•˜๋Š” ํ‚ค์›Œ๋“œ(break, continue)๊ฐ€ ์–ด๋–ค ๋ฒ”์œ„์— ์ ์šฉ๋˜์–ด์•ผ ํ•˜๋Š”์ง€ ์• ๋งคํ•˜๊ฑฐ๋‚˜ ํฐ ๋ฒ”์œ„์˜ ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•˜๊ณ  ์‹ถ์€๋ฐ ์ž‘์€ ๋ฒ”์œ„์˜ ๋ฐ˜๋ณต๋ฌธ๋งŒ ์ข…๋ฃŒ๋˜๋Š” ๋“ฑ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์‹ค์ˆ˜๋ฅผ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

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

var numbers: [Int] = [3, 2342, 6, 3252]

numbersLoop: for num in numbers{
	if num > 5 || num < 1 {
    	continue numbersLoop
    }
    
    var count: Int = 0
    
    printLoop: while true {
    	print(num)
        count+=1
        
        if count == num {
        	break printLoop
        }
    }
    
    removeLoop : while true{
    	if numbers.first != num {
        	break numbersLoop[
        }
        numbers.removeFirst()
    }
}

// 3
// 3
// 3
// numbers์—๋Š” [2342, 6, 3252]๊ฐ€ ๋‚จ์Šต๋‹ˆ๋‹ค.