0
0
Swiftprogramming~20 mins

If and if-else statements in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If-Else Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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")
}
AFizzBuzz
BFizz
CBuzz
DNo match
Attempts:
2 left
💡 Hint
Check if the number is divisible by 3 and then by 5 inside the nested if.
Predict Output
intermediate
2: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")
}
AGrade A
BGrade C
CGrade B
DGrade F
Attempts:
2 left
💡 Hint
Check the conditions from top to bottom and see where 75 fits.
🔧 Debug
advanced
2: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")
ATypeError: Cannot compare Int with Int
BRuntimeError: Unexpected token
CNo error, prints "Hot day"
DSyntaxError: Missing braces for if and else blocks
Attempts:
2 left
💡 Hint
Swift requires braces {} for if and else blocks.
🧠 Conceptual
advanced
2: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?
AB
BC
CA
DNo output
Attempts:
2 left
💡 Hint
The first true condition stops the rest from running.
Predict Output
expert
2: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")
}
AY
BNo output
CZ
DX
Attempts:
2 left
💡 Hint
Evaluate each condition carefully with logical AND and OR.