ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋“ค์ธ ์ œ์–ด๋ฌธ, ๋ฌธ์ž์—ด, ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์ ์–ด๋ณด๋ คํ•œ๋‹ค. ๋‹ค๋ฅธ ์–ธ์–ด์™€ ๋งŽ์€ ์ ์ด ๋‹ค๋ฅด์ง€๋Š” ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ•œ ํฌ์ŠคํŒ…์—์„œ ๋ชจ๋‘ ์•Œ์•„๋ณด๋ ค ํ•œ๋‹ค. ๋‹ค๋งŒ Switch, String(unicode, index ๋ฌธ์ œ ๋•Œ๋ฌธ)์˜ ๊ฒฝ์šฐ๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ˆ ์ถ”๊ฐ€๋กœ ์•Œ์•„๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœ๋“œ๋ฆฐ๋‹ค. ํ•จ์ˆ˜๋Š” ์‚ฌ์‹ค closure์˜ ์ผ์ข…์ธ๋ฐ, ์ด๋Š” ์ด ๋‹ค์Œ ๊ธ€์—์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ณ , ์ด๋ฒˆ์—๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ์•„๋Š” ํ•จ์ˆ˜์˜ ๊ฐœ๋…์œผ๋กœ ์ดํ•ดํ•˜๊ณ  ๊ธ€์„ ์ฝ์–ด๋ณด์ž.

๋ฐ˜๋ณต

  • for-in
    • Sequence๋ฅผ iterate
    • Sequence Protocol์„ ๋งŒ์กฑํ•˜๋Š” ๋ชจ๋“  ๊ฐ์ฒด์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅ
      • Sequence ์ •์˜
        • for-in loop๋กœ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…
      • Sequence ์ข…๋ฅ˜
  • while
    • ์กฐ๊ฑด ๋ฌธ์ด ์•ž์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์†Œ 0ํšŒ ์‹คํ–‰
  • repeat-while
    • do-while๊ณผ ์œ ์‚ฌ
    • ์กฐ๊ฑด๋ฌธ ๋’ค์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์†Œ 1ํšŒ ์‹คํ–‰

๋ถ„๊ธฐ

  • if
    • if, else if, else
  • switch
    • Pattern Matching

    • ๋ณต์žกํ•œ ์กฐ๊ฑด์— ๋”ฐ๋ฅธ ๋ถ„๊ธฐ ๊ฐ€๋Šฅ

    • break๋ฌธ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๊ฐ case๋งŒ ์‹คํ–‰ํ•˜๊ณ  ์ข…๋ฃŒ๋จ

      • ๋งŒ์•ฝ ์•„๋ž˜๊นŒ์ง€ ๋™์ž‘ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด fallthrough ํ‚ค์›Œ๋“œ ํ™œ์šฉ
    • ๋ชจ๋“  case๋ฌธ์€ ๋ฐ˜๋“œ์‹œ ์‹คํ–‰๋ฌธ์ด ํ•œ์ค„์ด์ƒ ์žˆ์–ด์•ผ ํ•จ

    • ๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•ด case๋ฌธ์ด ์—†๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ default ํ•„์š”

    • Interval Matching

      switch count {
      case 0:
          result = "no"
      case 1..<5:
          result = "few"
      default:
          result = "many"
      }
    • Value Binding

      switch point {
      case (let x, 0):
          print("x์ถ• ์œ„์— ์žˆ์Œ")
      case (0, let y):
          print("y์ถ• ์œ„์— ์žˆ์Œ")
      case (let x, let y) where x == y:
          print("y = x ์œ„์— ์žˆ์Œ")
      case (let x, let y) where x == -y:
          print("y = -x ์œ„์— ์žˆ์Œ")
      case (let x, let y):
          print("์ขŒํ‘œํ‰๋ฉด ์œ„์— ์žˆ์Œ")
      }
      • where ์ ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ถ”๊ฐ€ ์กฐ๊ฑด ํ™•์ธ์ด ๊ฐ€๋Šฅํ•จ