Learn
Logical Operators
Logical OR Operator

The logical, ||, operator evaluates two operands and returns a Boolean result. It returns false only when both operands are false and returns true when at least one operand is true.

|| is denoted by two pipe symbols and translates to OR.

The following truth table demonstrates all of the possible outcomes of an expression using ||.

Logical Expression Result
true || true true
true || false true
false || true true
false || false false

Take a look at the following example:

var snowing = false var raining = true if snowing || raining { print("Wear waterproof shoes!") }

If it is snowing OR raining, we recommend wearing waterproof shoes because in either scenario, your feet can get wet! Since raining is true, the condition will result in true, and the message Wear waterproof shoes! will get printed.

Instructions

1.

In SafetyFirst.swift, declare the following variables:

  • carInMotion and set its value to be false.

  • insideACar and set its value to be true.

2.

Below the variable declarations, create an if statement that determines the Boolean result of carInMotion OR insideACar in its condition.

Within the code block of the if statement, add a print statement that outputs Safety first! Buckle up.

Folder Icon

Take this course for free

Already have an account?