0
0
Swiftprogramming~10 mins

Switch with compound cases in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to match multiple cases in one line.

Swift
let fruit = "apple"
switch fruit {
    case [1]:
        print("It's a citrus fruit.")
    default:
        print("It's another fruit.")
}
Drag options to blanks, or click blank then click option'
A"grape", "melon"
B"orange", "lemon"
C"orange", "lemon", "lime"
D"apple", "banana"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to separate values with commas.
Using parentheses instead of commas.
Not enclosing strings in quotes.
2fill in blank
medium

Complete the switch statement to print "Weekend" for Saturday and Sunday.

Swift
let day = "Sunday"
switch day {
    case [1]:
        print("Weekend")
    default:
        print("Weekday")
}
Drag options to blanks, or click blank then click option'
A"Friday", "Saturday"
B"Monday", "Tuesday"
C"Sunday", "Monday"
D"Saturday", "Sunday"
Attempts:
3 left
💡 Hint
Common Mistakes
Including weekdays in the weekend case.
Forgetting to use commas between days.
Not enclosing day names in quotes.
3fill in blank
hard

Fix the error in the switch case to correctly match multiple values.

Swift
let number = 3
switch number {
    case [1]:
        print("Number is 1, 2, or 3")
    default:
        print("Other number")
}
Drag options to blanks, or click blank then click option'
A1, 2, 3
B1 || 2 || 3
C[1, 2, 3]
D1 && 2 && 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using logical OR (||) instead of commas.
Using array brackets [] in case.
Using logical AND (&&) instead of commas.
4fill in blank
hard

Fill both blanks to create a switch that prints "Vowel" for 'a', 'e', 'i', 'o', 'u' and "Consonant" for 'b', 'c', 'd'.

Swift
let letter: Character = "a"
switch letter {
    case [1]:
        print("Vowel")
    case [2]:
        print("Consonant")
    default:
        print("Other letter")
}
Drag options to blanks, or click blank then click option'
A'a', 'e', 'i', 'o', 'u'
B'a', 'e', 'i'
C'b', 'c', 'd'
D'x', 'y', 'z'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing vowels and consonants in the same case.
Using parentheses instead of commas.
Not enclosing characters in single quotes.
5fill in blank
hard

Fill all three blanks to create a switch that prints "Small", "Medium", or "Large" for sizes 1-2, 3-4, and 5-6 respectively.

Swift
let size = 4
switch size {
    case [1]:
        print("Small")
    case [2]:
        print("Medium")
    case [3]:
        print("Large")
    default:
        print("Unknown size")
}
Drag options to blanks, or click blank then click option'
A1, 2
B3, 4
C5, 6
D7, 8
Attempts:
3 left
💡 Hint
Common Mistakes
Using ranges like 1...2 instead of listing values separated by commas.
Mixing sizes between cases.
Not separating values with commas.