Learn

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 a `where` clause and a condition. When a condition is `true`, the code for that `case` will execute.

• The `let` keyword followed by the `x` creates a temporary binding to the `randomNumber` value. This means that the value of `x` temporarily becomes the value of `randomNumber`. If `randomNumber` is 5, then `x` is 5!

• The `let` keyword is specifically used here instead of `var` since the value of `x` will not be reassigned at any point throughout the `switch` statement, thus its value will always be constant. If `var` is used, Swift will display a compiler warning recommending us to use `let` 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 if `x` is divisible by 2 with or without a remainder and determines if the `randomNumber` 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

1.

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!

2.

Within the code block of the `switch` statement, set up the following cases:

• Case 1: Declare the temporary variable `x` and use the `where` clause to check if the value is divisible by 2. Print, `"Composite"`.

• Case 2: Declare the temporary variable `x` and use the `where` clause to check if the value is divisible by 3. Print, `"Composite"`.

The `default` statement should print, `"Prime"`.

3.

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 of `Integer`s between 10 and 20. Assign this calculation to `wholeNumber`.

• Use a `print()` statement below the variable to output the value of `wholeNumber`.