Lesson 3 โ€ข Beginner

    Control Flow ๐Ÿ”€

    Make decisions with if/else, master Swift's powerful switch statement with pattern matching, and repeat actions with for-in, while, and repeat-while loops.

    What You'll Learn in This Lesson

    • โ€ข Conditionals: if, else if, else, and the ternary operator
    • โ€ข Swift's switch with ranges, tuples, and pattern matching
    • โ€ข Loops: for-in, while, repeat-while
    • โ€ข Loop control with break and continue
    • โ€ข Ranges: closed (1...5) and half-open (1..<5)

    1๏ธโƒฃ Conditionals

    Conditionals let your program make decisions. Swift's if statements don't require parentheses around the condition (unlike C/Java), but the braces are always required โ€” even for single-line bodies.

    Try It: Conditionals

    Use if/else, comparison operators, logical operators, and ternary

    Try it Yourself ยป
    JavaScript
    // Swift Conditionals โ€” if, else if, else, guard
    console.log("=== if / else if / else ===");
    let temperature = 28;
    console.log("let temperature = " + temperature);
    console.log();
    
    if (temperature > 35) {
      console.log("๐Ÿ”ฅ It's extremely hot! Stay hydrated.");
    } else if (temperature > 25) {
      console.log("โ˜€๏ธ It's a warm day. Perfect for the beach!");
    } else if (temperature > 15) {
      console.log("๐ŸŒค๏ธ It's mild. Grab a light jacket.");
    } else if (temperature > 5) {
      console.log("๐Ÿงฅ It's cold. Wear
    ...

    2๏ธโƒฃ Switch Statements

    Swift's switch is far more powerful than in most languages. It supports ranges (0...100), tuples, pattern matching, and where clauses. Unlike C, there's no implicit fallthrough โ€” each case automatically breaks.

    Try It: Switch Statements

    Pattern matching with ranges, tuples, and multiple values

    Try it Yourself ยป
    JavaScript
    // Swift's Powerful Switch Statement
    console.log("=== Switch Statement ===");
    console.log("Swift's switch is MUCH more powerful than other languages!");
    console.log();
    
    // Basic switch
    let day = "Wednesday";
    console.log("let day = \"" + day + "\"");
    switch (day) {
      case "Monday": case "Tuesday": case "Wednesday": case "Thursday": case "Friday":
        console.log("๐Ÿ“… It's a weekday โ€” time to work!");
        break;
      case "Saturday": case "Sunday":
        console.log("๐ŸŽ‰ It's the weekend โ€” time to relax!
    ...

    3๏ธโƒฃ Loops

    Swift provides for-in for iterating over sequences, while for condition-based loops, and repeat-while (similar to do-while) which always executes at least once. Use stride(from:to:by:) for custom step values.

    Try It: Loops

    for-in, while, repeat-while, stride, break, and continue

    Try it Yourself ยป
    JavaScript
    // Swift Loops โ€” for-in, while, repeat-while
    console.log("=== for-in Loop ===");
    
    // Looping over a range
    console.log("for i in 1...5 {");
    for (let i = 1; i <= 5; i++) {
      console.log("  " + i + " ร— " + i + " = " + (i * i));
    }
    console.log();
    
    // Looping over an array
    console.log("for fruit in fruits {");
    let fruits = ["Apple", "Banana", "Cherry"];
    for (let fruit of fruits) {
      console.log("  ๐ŸŽ " + fruit);
    }
    console.log();
    
    // Enumerated (index + value)
    console.log("for (index, fruit) in fruits.
    ...

    โš ๏ธ Common Mistakes

    โš ๏ธ
    Non-exhaustive switch โ€” Swift requires switch statements to be exhaustive. Always include a default case or cover all enum values.
    โš ๏ธ
    Using == with optionals โ€” Comparing an optional directly works in Swift, but be careful: nil == nil is true.
    ๐Ÿ’ก
    Pro Tip: Use where clauses in switch/for to add conditions: for i in 1...100 where i % 2 == 0.

    ๐Ÿ“‹ Quick Reference โ€” Control Flow

    ConstructSwift Syntax
    if/elseif x > 5 { ... } else { ... }
    switchswitch val { case 1: ... }
    for-in rangefor i in 1...10 { ... }
    for-in arrayfor item in array { ... }
    whilewhile x > 0 { ... }
    repeat-whilerepeat { ... } while x > 0
    stridestride(from: 0, to: 10, by: 2)

    ๐ŸŽ‰ Lesson Complete!

    You've mastered conditionals, switch statements, and loops! Next, learn about functions and closures.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service