๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŽ iOS/Swift

[Swift] ์Šค์œ„ํ”„ํŠธ ์กฐ๊ฑด๋ฌธ(Conditional Statements)

์Šค์œ„ํ”„ํŠธ ์กฐ๊ฑด๋ฌธ(Conditional Statements)

  • if๋ฌธ
  • switch๋ฌธ

๐ŸŽ if๋ฌธ

  • if ๋‹จ๋…์ ์œผ๋กœ ์‚ฌ์šฉํ•ด๋„ ๋˜๊ณ , else if, else์™€ ์กฐํ•ฉํ•ด์„œ ์‚ฌ์šฉ.
  • if ๋’ค์˜ ์กฐ๊ฑด ๊ฐ’์—๋Š” Bool ํƒ€์ž…์˜ ๊ฐ’๋งŒ ์œ„์น˜ํ•ด์•ผ ํ•˜๋ฉฐ, ์กฐ๊ฑด ๊ฐ’์„ ๊ฐ์‹ธ๋Š” ์†Œ๊ด„ํ˜ธ๋Š” ์„ ํƒ์‚ฌํ•ญ.

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

if ์กฐ๊ฑด {
    /* ์‹คํ–‰ ๊ตฌ๋ฌธ */
} else if ์กฐ๊ฑด {
    /* ์‹คํ–‰ ๊ตฌ๋ฌธ */
} else {
    /* ์‹คํ–‰ ๊ตฌ๋ฌธ */
}
let myAge = 20

if myAge < 50 {	//์‹คํ–‰
    print("50 ๋ฏธ๋งŒ")
} else if myAge > 50 {
    print("50 ์ดˆ๊ณผ")
} else {	// ๊ฐœ๋ฐœ์ž์˜ ์˜๋„์— ๋”ฐ๋ผ else๋ฌธ๋„ ์ƒ๋žต ๊ฐ€๋Šฅ
    print("50")
}

์Šค์œ„ํ”„ํŠธ์˜ ์กฐ๊ฑด์—๋Š” ํ•ญ์ƒ Bool ํƒ€์ž…์ด ๋“ค์–ด์™€์•ผ ํ•œ๋‹ค. ์Šค์œ„ํ”„ํŠธ๋Š” ํƒ€์ž…๊ฒ€์‚ฌ๋ฅผ ์—„๊ฒฉํ•˜๊ธฐ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋Š” ์—๋Ÿฌ ๋ฐœ์ƒ.

number๋Š” Bool ํƒ€์ž…์ด ์•„๋‹Œ Int ํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ

var number = 0
if !number { // ์ปดํŒŒ์ผ ์—๋Ÿฌ!
  // ...
}

if number == 0 { // ๋งž๋Š” ์ฝ”๋“œ!
  // ...
}

๐Ÿ–ค if๋ฌธ์—์„œ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž - AND

//if๋ฌธ์—์„œ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž - AND
//1. && ์—ฐ์‚ฐ์ž - if (A) && (B) ์‚ฌ์šฉ
var tmp = 30
if (tmp > 0) && (tmp % 2 == 0) {
    print("10 ์ด์ƒ์˜ ์ง์ˆ˜")
}

//2. ์ฝค๋งˆ(,) - if a, b ์‚ฌ์šฉ
if tmp > 0, tmp % 2 == 0 {
    print("10 ์ด์ƒ์˜ ์ง์ˆ˜")
}

๐ŸŽ switch๋ฌธ

  • ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋˜ ์ •์ˆ˜ํƒ€์ž…์˜ ๊ฐ’๋งŒ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋Œ€๋ถ€๋ถ„์˜ ์Šค์œ„ํ”„ํŠธ ๊ธฐ๋ณธ ํƒ€์ž…์„ ์ง€์›ํ•˜๋ฉฐ, ๋‹ค์–‘ํ•œ ํŒจํ„ด๊ณผ๋„ ์‘์šฉ์ด ๊ฐ€๋Šฅ.
  • switch๋ฌธ ํŠน์ง•
    • ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์‚ฌ๋ก€๋ฅผ ๋ฐ˜๋“œ์‹œ ๋‹ค๋ฃจ์–ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, default ๊ตฌ๋ฌธ์€ ๋ฐ˜๋“œ์‹œ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. 
    • >>(์˜ˆ์™ธ) ํ•œ์ •์ ์ธ ๊ฐ’(ex. enum์˜ case ๋“ฑ)์ด ์•„๋‹Œ ๊ฒฝ์šฐ, case๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ๊ฐ€ ๋‹ค ๊ตฌํ˜„์ด ๋œ ๊ฒฝ์šฐ
    • ๋ช…์‹œ์  break๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ์ž๋™์œผ๋กœ case๋งˆ๋‹ค break ๋จ.
    • fallthrough ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ break๋ฅผ ๋ฌด์‹œ ๊ฐ€๋Šฅ.
    • ์‰ผํ‘œ(,)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜๋‚˜์˜ case์— ์—ฌ๋Ÿฌ ํŒจํ„ด ๊ฐ€๋Šฅ.

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

switch ๋น„๊ต๊ฐ’ {	//์ž…๋ ฅ๊ฐ’! ๋น„๊ต๋Œ€์ƒ
case ํŒจํ„ด:
    /* ์‹คํ–‰ ๊ตฌ๋ฌธ */
default:
    /* ์‹คํ–‰ ๊ตฌ๋ฌธ */
}
let a : Character = "a"

switch a {
case "a":
//๋น„๊ต๋ฅผ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™์€ ํƒ€์ž…์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
    print("์•ŒํŒŒ๋ฒณ์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž")
// ์‚ฌ์‹ค ์ผ€์ด์Šค๊ฐ€ ์ผ์น˜ํ•˜๋ฉด ์Šค์œ„์น˜ ๊ตฌ๋ฌธ์€ ์ข…๋ฃŒ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ Case ๋งˆ๋‹ค default์— ์žˆ๋Š” break ๊ฐ€ ์ƒ๋žต๋˜์–ด ์žˆ๋‹ค๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.
case "z":
    print("์•ŒํŒŒ๋ฒณ์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž")
default:
//    ์ผ€์ด์Šค๊ฐ€ ๋ชจ๋‘ ๋งž์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋‚˜์˜ค๋Š” ๋ถ€๋ถ„.
    break
//    break ์—†์œผ๋ฉด ์˜ค๋ฅ˜ ๋ฐœ์ƒ. ์™œ๋ƒ, ๋ฌด์กฐ๊ฑด ์ ์–ด๋„ ํ•˜๋‚˜์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™€์•ผ ํ•œ๋‹ค.
}

๐Ÿ–ค default๊ฐ€ ํ•„์š” ์—†๋Š” ๊ฒฝ์šฐ! - ๋ชจ๋“  ์ผ€์ด์Šค๋ฅผ ๋‹ค ๊ตฌํ˜„ํ•œ ๊ฒฝ์šฐ

let isTrue: Bool = true

switch isTrue{
case true:
    print("true")
case false:
    print("false")
default:	//์—๋Ÿฌ๋Š” ์•„๋‹ˆ๊ณ  warning ๋ฐœ์ƒ
    print("ERROR")
}
enum School{
    case primary, elementary, middle, high
}

let ํ•™๋ ฅ: School = School.middle

switch ํ•™๋ ฅ {
case .primary:
    print("์œ ์น˜์›")
case .elementary:
    print("์ดˆ๋”ฉ")
case .middle:
    print("์ค‘๋”ฉ")
case .high:
    print("๊ณ ๋”ฉ")
}

๐Ÿ–ค switch๋ฌธ์—์„œ ๋ฒ”์œ„ ์—ฐ์‚ฐ์ž ํ™œ์šฉํ•˜๊ธฐ

let Number = 100

switch Number {
case 0:
    print("zero")
case 1..<100:		//100๋ฏธ๋งŒ
    print("1~99")
case 100:
    print("100")
case 101...Int.max:
    print("over 100")
default:		//default๊ตฌ๋ฌธ ์—†์œผ๋ฉด ์˜ค๋ฅ˜๋ฐœ์ƒ!!
    print("unknown")
} 
// 100

๐Ÿ–ค where ํ‚ค์›Œ๋“œ ์‚ฌ์šฉํ•˜์—ฌ case ์กฐ๊ฑด ํ™•์žฅํ•˜๊ธฐ

let ์ง๊ธ‰: String = "์‚ฌ์›"
let ์—ฐ์ฐจ: Int = 1
let ์ธํ„ด์ธ๊ฐ€: Bool = false

switch ์ง๊ธ‰ {
case "์‚ฌ์›" where ์ธํ„ด์ธ๊ฐ€==true:
    print("์ธํ„ด์ž…๋‹ˆ๋‹ค")
case "์‚ฌ์›" where ์—ฐ์ฐจ<2 && ์ธํ„ด์ธ๊ฐ€==false:
    print("์‹ ์ž…์‚ฌ์›์ž…๋‹ˆ๋‹ค")
default:
    print("์‚ฌ์žฅ")
}

๐Ÿ–ค ํŠœํ”Œ switch case ๊ตฌ์„ฑ

typealias tupleSwitch = (name:String, age:Int)
let tupleValue : tupleSwitch=("yeon",20)

switch tupleValue{
case("yeon",20):
    print("๋‚˜์•ผ๋‚˜")
default:
    print("์•„๋‹ˆ์•ผ")
}

 

728x90
๋ฐ˜์‘ํ˜•