Bird
Raised Fist0
Swiftprogramming~10 mins

If and if-else statements in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - If and if-else statements
Start
↓
Evaluate condition
↓
Execute if-block
↓
Continue
↓
Check if else-block exists?
↓
Execute else-block
↓
Continue
↓
Continue
The program checks a condition. If true, it runs the if-block. If false and else-block exists, it runs else-block. Then it continues.
Execution Sample
Swift
let number = 7
if number % 2 == 0 {
    print("Even")
} else {
    print("Odd")
}
Checks if number is even or odd and prints the result.
Execution Table
StepConditionResultBranch TakenOutput
1number % 2 == 07 % 2 == 0 → 1 == 0 → falseelse-blockOdd
2End of if-elseNo more conditionsExit
💡 Condition is false, else-block runs, then execution ends.
Variable Tracker
VariableStartAfter Step 1Final
number777
Key Moments - 2 Insights
Why does the else-block run when the condition is false?
Because the condition in the if statement is false (see execution_table step 1), the program skips the if-block and runs the else-block instead.
What happens if there is no else-block and the condition is false?
The program skips the if-block and continues without running any code inside the if or else blocks (not shown in this example).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the condition result at step 1?
Atrue
Bundefined
Cfalse
Derror
💡 Hint
Check the 'Result' column in execution_table row 1.
At which step does the else-block run?
AStep 1
BStep 2
CNo else-block runs
DBefore step 1
💡 Hint
Look at the 'Branch Taken' and 'Output' columns in execution_table row 1.
If number was 8, how would the output change?
A"Odd"
B"Even"
CNo output
DError
💡 Hint
Think about the condition number % 2 == 0 with number = 8.
Concept Snapshot
if condition {
  // code runs if condition is true
} else {
  // code runs if condition is false
}

- Condition is checked once.
- If true, if-block runs.
- Else-block runs only if condition is false.
- Else-block is optional.
Full Transcript
This visual shows how if and if-else statements work in Swift. The program starts by checking a condition. If the condition is true, it runs the code inside the if-block. If the condition is false and there is an else-block, it runs the else-block code instead. Then the program continues after the if-else. For example, checking if a number is even or odd uses this logic. The execution table shows the condition check, the branch taken, and the output printed. The variable tracker shows the value of the number throughout. Key moments clarify why the else-block runs when the condition is false and what happens if there is no else-block. The quiz asks about condition results, when the else-block runs, and how changing the number affects output. The snapshot summarizes the syntax and behavior simply.

Practice

(1/5)
1. What does an if statement do in Swift?
easy
A. It runs code only if a condition is true.
B. It repeats code multiple times.
C. It stores data in a variable.
D. It defines a new function.

Solution

  1. Step 1: Understand the purpose of if statement

    An if statement checks a condition and runs code only when that condition is true.
  2. Step 2: Compare with other options

    Repeating code is done by loops, storing data is done by variables, and defining functions uses func.
  3. Final Answer:

    It runs code only if a condition is true. -> Option A
  4. Quick Check:

    If statement = runs code if true [OK]
Hint: If checks true condition to run code, else skips it [OK]
Common Mistakes:
  • Confusing if with loops
  • Thinking if stores data
  • Mixing if with function definitions
2. Which of the following is the correct syntax for an if-else statement in Swift?
easy
A. if condition { } else { }
B. if (condition) then { } else { }
C. if condition then { } else { }
D. if condition: { } else: { }

Solution

  1. Step 1: Recall Swift if-else syntax

    Swift uses if condition { } and else { } blocks without extra keywords like 'then' or colons.
  2. Step 2: Check each option

    if condition { } else { } matches Swift syntax exactly. Options A, B, and C use invalid keywords or punctuation.
  3. Final Answer:

    if condition { } else { } -> Option A
  4. Quick Check:

    Swift if-else = if condition { } else { } [OK]
Hint: Swift uses braces {} without 'then' or colons in if-else [OK]
Common Mistakes:
  • Adding 'then' keyword
  • Using colons after if or else
  • Putting condition in parentheses unnecessarily
3. What will be printed by this Swift code?
let number = 10
if number > 5 {
    print("Big")
} else {
    print("Small")
}
medium
A. Small
B. No output
C. 10
D. Big

Solution

  1. Step 1: Evaluate the condition

    The variable number is 10. The condition number > 5 is true because 10 is greater than 5.
  2. Step 2: Determine which block runs

    Since the condition is true, the code inside the if block runs, printing "Big".
  3. Final Answer:

    Big -> Option D
  4. Quick Check:

    10 > 5 is true, so prints "Big" [OK]
Hint: Check condition true? Run if block, else run else block [OK]
Common Mistakes:
  • Confusing which block runs
  • Printing variable instead of string
  • Ignoring condition result
4. Find the error in this Swift code:
let age = 18
if age >= 18
    print("Adult")
else
    print("Minor")
medium
A. Variable age should be a string
B. Wrong comparison operator
C. Missing braces { } around if and else blocks
D. No error, code is correct

Solution

  1. Step 1: Check Swift if-else syntax

    Swift requires braces { } around the code blocks after if and else.
  2. Step 2: Identify missing braces

    The code lacks braces after if age >= 18 and else, causing a syntax error.
  3. Final Answer:

    Missing braces { } around if and else blocks -> Option C
  4. Quick Check:

    Swift needs braces for if-else blocks [OK]
Hint: Always use { } after if and else in Swift [OK]
Common Mistakes:
  • Omitting braces in if-else
  • Using wrong operators
  • Wrong variable types
5. You want to write Swift code that prints "Positive", "Negative", or "Zero" depending on the value of an integer variable num. Which code correctly uses if-else statements to do this?
hard
A. if num >= 0 { print("Positive") } else if num <= 0 { print("Negative") } else { print("Zero") }
B. if num > 0 { print("Positive") } else if num < 0 { print("Negative") } else { print("Zero") }
C. if num > 0 { print("Positive") } else { print("Negative") } print("Zero")
D. if num == 0 { print("Zero") } else if num > 0 { print("Negative") } else if num < 0 { print("Positive") }

Solution

  1. Step 1: Understand the conditions needed

    We need to check if num is greater than 0, less than 0, or exactly 0.
  2. Step 2: Analyze each option

    if num > 0 { print("Positive") } else if num < 0 { print("Negative") } else { print("Zero") } correctly checks >0, then <0, else zero. if num >= 0 { print("Positive") } else if num <= 0 { print("Negative") } else { print("Zero") } wrongly overlaps conditions. if num > 0 { print("Positive") } else { print("Negative") } print("Zero") prints "Zero" always. if num == 0 { print("Zero") } else if num > 0 { print("Negative") } else if num < 0 { print("Positive") } incorrectly prints "Negative" for positive numbers and "Positive" for negative numbers.
  3. Final Answer:

    if num > 0 { print("Positive") } else if num < 0 { print("Negative") } else { print("Zero") } -> Option B
  4. Quick Check:

    Check >0, else if <0, else zero [OK]
Hint: Check positive first, then negative, else zero last [OK]
Common Mistakes:
  • Overlapping conditions causing wrong output
  • Printing zero outside else block
  • Wrong order of conditions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard