Skip to content
Learn Netverks

Lesson

Step 10/36 28% through track

control-flow-swift

Control flow

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Control flow: the syntax, patterns, and safety habits you need before advancing in Swift.

Coroutines replace callback hell on Android and in Ktor—structured concurrency is interview-critical.

You will apply Control flow in contexts like: iPhone/iPad/Mac apps, server-side Swift (niche), and Apple toolchain projects.

Write Swift in main.swift with print(), click Run on server—the dev runner swiftc compiles and runs the binary (requires Swift toolchain, typically macOS; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Swift control flow resembles Java and C# but adds powerful switch pattern matching and guard statements for early exits.

if, guard, and switch

let score = 85
if score >= 90 { print("A") }
else if score >= 80 { print("B") }
else { print("Review") }

switch score {
case 90...100: print("Excellent")
case 80..<90: print("Good")
default: print("Keep practicing")
}

Loops

for item in collection, while, and repeat-while cover iteration. Ranges: 1...10 (closed), 1..<10 (half-open).

Important interview questions and answers

  1. Q: guard vs if?
    A: guard exits early when a condition fails—keeps the happy path unindented; common for optional unwrapping.
  2. Q: Does switch fall through?
    A: No implicit fall-through—each case must exit or use fallthrough explicitly.

Self-check

  1. What range operator includes 10 as the upper bound?
  2. Does Swift switch require break by default?

Tip: guard keeps happy-path code unindented—like early returns in Kotlin and C#.

Interview prep

guard vs if?

guard exits early when condition fails—keeps happy path flat.

Switch fall-through?

No implicit fall-through—must use fallthrough explicitly.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • switch must be exhaustive?
  • for-in ranges?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump