0
0
Swiftprogramming~10 mins

Switch statement power 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 print the correct message for the number using a switch statement.

Swift
let number = 3
switch number {
case 1:
    print("One")
case 2:
    print("Two")
case [1]:
    print("Three")
default:
    print("Other")
}
Drag options to blanks, or click blank then click option'
A4
B3
C5
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using a case value that does not match the variable, so the default case runs.
2fill in blank
medium

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

Swift
let day = "Sunday"
switch day {
case "Saturday", [1]:
    print("Weekend")
default:
    print("Weekday")
}
Drag options to blanks, or click blank then click option'
A"Friday"
B"Monday"
C"Sunday"
D"Thursday"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a weekday instead of Sunday in the case.
3fill in blank
hard

Fix the error in the switch statement to correctly handle the character input.

Swift
let letter: Character = 'a'
switch letter {
case [1]:
    print("Vowel")
default:
    print("Consonant")
}
Drag options to blanks, or click blank then click option'
A'a'
B"a", "e", "i", "o", "u"
C"aeiou"
D"a"
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to match multiple characters in one case using a string or list.
4fill in blank
hard

Fill both blanks to create a switch that prints the season based on the month number.

Swift
let month = 4
switch month {
case 12, 1, 2:
    print("Winter")
case 3, 4, [1]:
    print("Spring")
case [2]:
    print("Summer")
default:
    print("Fall")
}
Drag options to blanks, or click blank then click option'
A5
B6
C7
D8
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up month numbers for seasons.
5fill in blank
hard

Fill all three blanks to create a switch that categorizes a score into grades.

Swift
let score = 85
switch score {
case [1]:
    print("Fail")
case [2]:
    print("Pass")
case [3]:
    print("Excellent")
default:
    print("Invalid score")
}
Drag options to blanks, or click blank then click option'
A0...49
B50...79
C80...100
D101...200
Attempts:
3 left
💡 Hint
Common Mistakes
Using overlapping or incorrect ranges causing wrong category.