# Lesson 4 – Taking Decisions Using Conditions, If-Else, and Switch Statements

## Use conditions to validate data

Not all of the data that apps work with is valid. Swift conditions test an app’s data to check whether the app will crash when the data is invalid.

To see this in operation, we can look at the code for the division operator of a calculator app. It looks like this will work when any number is entered.

However, the compiler flags a division by zero error for the code. You can click the playground’s sidebar buttons to view the error’s stack trace. This can help you fix the error. It is handy when debugging complex errors that happen only at runtime, even if the code works well at compile time. You can use conditions to fix the error and prevent the playground from crashing.

Conditions are defined with the if keyword. The != operator is called not equal and checks that the second isn’t 0.

The code that is executed if the condition is true is placed between curly braces {}.

Nothing happens if the condition is false. We can add more logic to fix this.

The == operator is called equal and checks if the second is 0. The if statement prints an error message to the console if the condition is true.

The else branch is executed when the condition is false. The code for the else branch of the statement also needs to be placed between curly braces {}.

This logic can also be rewritten using the != operator.

The else statement prints an error message to the console if the condition is false.

## Compare data in conditions with comparison operators

A calculator app can find the minimum value of two numbers using comparison operators.

The &lt; operator is called less than. It checks if the first number is less than the second. The minimum value is the first number if the condition is true and the second number otherwise.

We can also determine the minimum of two numbers with the &lt;= operator. This is called less than or equal.

If the first number has the same value as the second, the minimum value is the first number since both numbers are equal in this case.

A calculator app can also find the maximum value of two numbers using comparison operators.

The &gt; operator is called greater than, and it checks if the first number is greater than the second. The maximum value is the first number if the condition is true and the second number otherwise.

We can also determine the maximum of two values with the &gt;= operator. This is called greater than or equal.

If the first number has the same value as the second number, the maximum value is the first number since both numbers are equal.

## Initialize constants and variables with boolean values and the ternary operator

The Bool type has only two values: true and false. An example of this is a weather app that works with Celsius degrees. It sets celsius to true.

Booleans are great for setting the value of constants and variables that don’t have an initial value.

The weather app sets its temperature symbol to either °C if celsius is true or °F otherwise.

Type inference works with Boolean values too. We can see this if we look at a scientific app that works with Rankine degrees.

Type inference determines that kelvin is a Bool constant. The app sets kelvin to false.

The scientific app also uses booleans to set the value of its thermodynamic temperature symbol.

The thermodynamic temperature symbol is set to °K if kelvin is true or °R otherwise.

We can simplify the initialization process of constants and variables with the ternary operator.

The ternary operator assigns to the symbol the value after the question mark ? if the condition is true or the value after the colon : if the condition is false.

## Write simple conditions and initialize constants and variables with logical operators

Conditions that contain only Boolean values can be simplified in some instances.

Logical operators can simplify other types of conditions too.

The ! operator is the logical not. The resulting condition is false if the initial condition is true and false otherwise.

We can simplify these conditions even more with the ternary operator.

The ?: and the ! operators make the conditions short and readable at a glance.

Logical operators can also assign Boolean values to constants and variables.

The above code uses the ! operator to set the cache constant value based on the value of the download condition. The cache is true if download is false and false otherwise.

## Write complex conditions with switch statements

The weather app uses conditions to set its temperature name based on the temperature symbol used.

The else if statement declares a condition that is checked if the previous if statement is false. The code that is executed if the condition is true is placed between curly brackets {}.

The weather app works with Celsius degrees if its temperature symbol is °C or Fahrenheit degrees if its temperature symbol is °F. The temperature name is the temperature symbol for any other symbol’s value.

We can simplify this logic with switch statements.

The switch keyword works with all possible values of symbol. Each case statement defines a specific value for symbol, and all cases are declared between curly brackets {}.

The code that is executed if a specific case is true is placed after the colon :. The default keyword handles all symbol values that other cases don’t cover since the switch statement must be exhaustive.

The scientific app also uses conditions to set its temperature name based on its temperature symbol.

The scientific app works with Kelvin degrees if its temperature symbol is °K or Rankine degrees if its temperature symbol is °R. The temperature name is the temperature symbol for any other symbol’s value.

We can simplify this logic with switch statements too.

The code switches on symbol and sets the corresponding temperature name for each symbol’s value. The switch statement must be exhaustive, so it needs a default case.

Both apps can be combined into one temperature app:

There are many else if statements in the code. These can be simplified with a switch statement.

The switch statement handles all possible symbol values previously covered by else if statements. The default case ensures that the switch statement is exhaustive.

## Combine conditions with logical operators

Logical operators create complex conditions from simple conditions.

The &amp;&amp; operator is the logical and. If both conditions are true, the resulting condition is true. Otherwise, the resulting condition is false.

For example, the weather app works with Celsius degrees and °C temperature symbols. This case is covered by the first if statement.

The weather app also works with Fahrenheit degrees and °F temperature symbols. This case is covered by the second if statement.

The scientific app works with logical operators as well.

The scientific app works with Kelvin degrees and °K temperature symbols. This case is covered by the first if statement.

The scientific app also works with Rankine degrees and °R temperature symbols. This case is covered by the second if statement.

The conditions above can be combined further for weather and scientific apps.

The || operator is the logical or. The resulting condition is true when either condition is true. Otherwise, the resulting condition is false.

The round brackets show that the &amp;&amp; operator has higher precedence than the || operator. This means that the compiler first evaluates each &amp;&amp; condition and then every || condition.

The weather app works with Celsius degrees and °C temperature symbols or Fahrenheit degrees and °F temperature symbols. This case is covered by the first if statement.

The scientific app works with Kelvin degrees and °K temperature symbols or Rankine degrees and °R temperature symbols. This case is covered by the second if statement.

Both logical operators use short-circuiting techniques to evaluate conditions.

If the first condition evaluated by the &amp;&amp; operator is false, the resulting condition is always false. So the second condition is never evaluated in this case.

If the first condition evaluated by the || operator is true, the resulting condition is always true. So the second condition is never evaluated in this case.

## Register to get more exclusive free content

• Enroll in the course and track your progress;