Challenge - 5 Problems
If-Else Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested if-else statements
What is the output of this Swift code?
Swift
let number = 15 if number % 3 == 0 { if number % 5 == 0 { print("FizzBuzz") } else { print("Fizz") } } else { print("No match") }
Attempts:
2 left
💡 Hint
Check if the number is divisible by 3 and then by 5 inside the nested if.
✗ Incorrect
The number 15 is divisible by 3 (15 % 3 == 0), so the outer if is true. Inside, 15 % 5 == 0 is also true, so it prints "FizzBuzz".
❓ Predict Output
intermediate2:00remaining
Output of if-else with multiple conditions
What will be printed by this Swift code?
Swift
let score = 75 if score >= 90 { print("Grade A") } else if score >= 80 { print("Grade B") } else if score >= 70 { print("Grade C") } else { print("Grade F") }
Attempts:
2 left
💡 Hint
Check the conditions from top to bottom and see where 75 fits.
✗ Incorrect
The score 75 is less than 90 and 80 but greater or equal to 70, so it prints "Grade C".
🔧 Debug
advanced2:00remaining
Identify the error in if-else syntax
What error does this Swift code produce?
Swift
let temperature = 30 if temperature > 25 print("Hot day") else print("Cool day")
Attempts:
2 left
💡 Hint
Swift requires braces {} for if and else blocks.
✗ Incorrect
In Swift, if and else blocks must be enclosed in braces {}. Missing braces cause a syntax error.
🧠 Conceptual
advanced2:00remaining
Understanding if-else statement flow
Consider this Swift code snippet:
let x = 10
if x > 5 {
print("A")
} else if x > 8 {
print("B")
} else {
print("C")
}
What will be printed?
Attempts:
2 left
💡 Hint
The first true condition stops the rest from running.
✗ Incorrect
The first condition x > 5 is true (10 > 5), so it prints "A" and skips the rest.
❓ Predict Output
expert2:00remaining
Output of complex if-else with logical operators
What is the output of this Swift code?
Swift
let a = 4 let b = 7 if a > 5 && b < 10 { print("X") } else if a <= 5 || b > 10 { print("Y") } else { print("Z") }
Attempts:
2 left
💡 Hint
Evaluate each condition carefully with logical AND and OR.
✗ Incorrect
a > 5 is false (4 > 5 is false), so first if is false. Second condition a <= 5 is true (4 <= 5), so second if is true and prints "Y".