Skip to content
Prepare to Grow

Swift Fundamentals

Conditions and switch

Direct program behavior with if, else, guard, and exhaustive switch statements.

Beginner4 min read2 guided activities

Before you begin

What you will understand

  • Write Boolean conditions with if and else.
  • Use guard for early exits.
  • Build exhaustive switch statements.
  • Choose a control-flow structure that communicates intent.
01

Learning stage

Concept Explanation

Conditional statements choose which code should run. Use if when behavior depends on a Boolean expression, and add else or else if branches when alternatives matter.

Use guard to require a condition and leave the current scope when it fails. Use switch when one value should be matched against several exhaustive patterns or cases.

  • if handles general Boolean conditions.
  • guard keeps the successful path less nested.
  • switch must cover every possible value.
  • default covers remaining cases when listing all of them is not useful.
02

Learning stage

Syntax & Examples

The function validates progress with guard and then uses switch to create a readable category.

ProgressMessage.swiftswift
func progressMessage(percent: Int) -> String {
  guard (0...100).contains(percent) else {
      return "Invalid progress"
  }

  switch percent {
  case 100:
      return "Complete"
  case 75..<100:
      return "Almost there"
  case 1..<75:
      return "Keep going"
  default:
      return "Ready to begin"
  }
}

print(progressMessage(percent: 82))
Expected output: “Almost there”.
03

Learning stage

Real-World Usage

These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.

  • Validate input before continuing a workflow.
  • Select interface states such as loading, empty, content, or failure.
  • Match enums and associated values without fragile chains of comparisons.
04

Learning stage

Common Mistakes

Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.

  • Writing conditions that do not evaluate to Bool.
  • Creating deeply nested if statements when guard can reject invalid input early.
  • Forgetting that switch cases do not fall through automatically.
  • Using a broad default that hides a newly added enum case.
05

Learning stage

Practice Exercises

Work through these activities in a small Xcode project. No embedded playground is required on this site.

Activity 1

Describe a password state

Use if/else if/else to return Weak, Acceptable, or Strong based on a password length you define.

Hint: Arrange branches from the most specific threshold to the fallback.

Activity 2

Model interface states

Sketch an enum with loading, empty, content, and failure cases, then write a switch that returns a message for each.

Hint: Avoid default so the compiler checks exhaustiveness.

06

Learning stage

Knowledge Check

Answer each question before revealing the explanation.

QUESTION 1

What important requirement does Swift place on switch statements?

  1. A.They must be exhaustive
  2. B.They must include default
  3. C.They can match only Int
  4. D.They always fall through
Reveal answer

They must be exhaustive

Every possible input must be handled, either through explicit cases or a suitable default.

QUESTION 2

What must a guard else branch do?

Reveal answer

It must transfer control out of the guard statement's enclosing scope, such as with return, throw, break, or continue.

07

Learning stage

Interview Preparation

Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.

When would you choose switch instead of if?

Short answer: Use switch when matching one value against several exhaustive patterns or enum cases.

Explore the detailed answer

if is flexible for unrelated Boolean conditions. switch is clearer for a single subject with multiple cases and lets the compiler verify exhaustiveness, especially for enums. It also supports ranges, tuples, value binding, and where clauses.

Possible follow-ups

  • • Why is exhaustive matching useful?
  • • When is guard preferable to if?
08

Learning stage

Key Takeaways

Control flow should make the successful path and alternatives obvious. Use guard for requirements, if for general conditions, and switch for exhaustive value matching.

  • Swift conditions must be Boolean.
  • guard exits when a requirement is not met.
  • switch is exhaustive and does not fall through by default.
  • Prefer structures that minimize unnecessary nesting.