0
0
Swiftprogramming~10 mins

Switch with where clauses 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 the number 10 in the switch statement.

Swift
let number = 10
switch number {
case  [1]:
    print("Number is ten")
default:
    print("Number is something else")
}
Drag options to blanks, or click blank then click option'
A5
B15
C20
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a number different from 10 will not match the case.
2fill in blank
medium

Complete the where clause to check if the number is even.

Swift
let number = 8
switch number {
case let x where x [1] 2 == 0:
    print("Even number")
default:
    print("Odd number")
}
Drag options to blanks, or click blank then click option'
A-
B+
C%
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or - instead of % will not check evenness.
3fill in blank
hard

Fix the error in the where clause to check if the number is positive.

Swift
let number = -3
switch number {
case let x where x [1] 0:
    print("Positive number")
default:
    print("Non-positive number")
}
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < will check for negative numbers instead.
4fill in blank
hard

Fill both blanks to check if the number is between 1 and 10 inclusive.

Swift
let number = 7
switch number {
case let x where x [1] 1 && x [2] 10:
    print("Number is between 1 and 10")
default:
    print("Number is out of range")
}
Drag options to blanks, or click blank then click option'
A>=
B<
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < only excludes the boundary numbers.
5fill in blank
hard

Fill both blanks to check if the number is odd and greater than 5.

Swift
let number = 7
switch number {
case let x where x [1] 2 != 0 && x [2] 5:
    print("Odd number greater than 5")
default:
    print("Other number")
}
Drag options to blanks, or click blank then click option'
A%
B>
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using == 0 checks even numbers, not odd.