Another neat feature available for the cases of a switch
statement is the where
clause.
The where
clause allows for additional pattern matching for a given expression. It can also be used with loops and other conditionals such as if
statements.
Assume we’re creating a program that determines if a random integer between 0 and 10 is even or odd. We can write the following program:
var randomNumber = Int.random(in: 0...10) switch randomNumber { case let x where x % 2 == 0: print("\(randomNumber) is even") case let x where x % 2 == 1: print("\(randomNumber) is odd") default: print("Invalid") }
Let’s dive into what’s happening on the first line:
var randomNumber = Int.random(in: 0...10)
We’re generating a random integer,
Int
, using the built-in Swift method,.random()
which returns an arbitrary value from a range of numbers. Notice how we’re using the closed range operator,...
, to denote a numerical range.We then assign the randomly generated value to the variable,
randomNumber
. We’ll be working more with.random()
in the following lessons.
Following the variable declaration is a standard switch
statement that checks the value of randomNumber
:
switch randomNumber { case let x where x % 2 == 0: print("\(randomNumber) is even") case let x where x % 2 == 1: print("\(randomNumber) is odd") default: print("Invalid") } // Prints: 7 is odd
Each
case
contains a variable declaration followed by awhere
clause and a condition. When a condition istrue
, the code for thatcase
will execute.The
let
keyword followed by thex
creates a temporary binding to therandomNumber
value. This means that the value ofx
temporarily becomes the value ofrandomNumber
. IfrandomNumber
is 5, thenx
is 5!The
let
keyword is specifically used here instead ofvar
since the value ofx
will not be reassigned at any point throughout theswitch
statement, thus its value will always be constant. Ifvar
is used, Swift will display a compiler warning recommending us to uselet
instead:
Numbers.swift:6:12: warning: variable 'x' was never mutated; consider changing to 'let' constant
Note: a compiler warning is not an error. Your program should still run even with a warning.
- Lastly, the
where
condition checks ifx
is divisible by 2 with or without a remainder and determines if therandomNumber
is even or odd.
If you run this code, chances are your output will be different from ours since the number generated each time is random!
Instructions
In Numbers.swift, we’ll set up a program that determines if a number between 10 and 20 is prime or composite.
Below the wholeNumber
variable, set up a switch
statement that accepts wholeNumber
as its expression. Keep the body of the switch
statement empty for now.
Note: You will see an error in the terminal on the right, but it will go away in the next step when we add case
statements!
Within the code block of the switch
statement, set up the following cases:
Case 1: Declare the temporary variable
x
and use thewhere
clause to check if the value is divisible by 2. Print,"Composite"
.Case 2: Declare the temporary variable
x
and use thewhere
clause to check if the value is divisible by 3. Print,"Composite"
.
The default
statement should print, "Prime"
.
Awesome work! This step is optional so feel free to hit Run if you’d like to skip it and move on to the next the exercise.
Challenge:
Instead of manually assigning and testing different values for wholeNumber
, we can use .random()
to generate one for us.
Set up the
.random()
method to accept a range ofInteger
s between 10 and 20. Assign this calculation towholeNumber
.Use a
print()
statement below the variable to output the value ofwholeNumber
.