Key Concepts

Review core concepts you need to learn to master this subject

if Statement

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

An if statement executes a code block when its condition evaluates to true. If the condition is false, the code block does not execute.

else Statement

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

An else statement is a partner to an if statement. When the condition for the if statement evaluates to false, the code within the body of the else will execute.

else if Statement

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

An else if statement provides additional conditions to check for within a standard if/else statement. else if statements can be chained and exist only after an if statement and before an else.

Comparison Operators

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

Comparison operators compare the values of two operands and return a Boolean result:

  • < less than
  • > greater than
  • <= less than or equal to
  • >= greater than or equal to
  • == equal to
  • != not equal to

Ternary Conditional Operator

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

The ternary conditional operator, denoted by a ?, creates a shorter alternative to a standard if/else statement. It evaluates a single condition and if true, executes the code before the :. If the condition is false, the code following the : is executed.

switch Statement

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

The switch statement is a type of conditional used to check the value of an expression against multiple cases. A case executes when it matches the value of the expression. When there are no matches between the case statements and the expression, the default statement executes.

switch Statement: Interval Matching

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

Intervals within a switch statement’s case provide a range of values that are checked against an expression.

switch Statement: Compound Cases

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

A compound case within a switch statement is a single case that contains multiple values. These values are all checked against the switch statement’s expression and are separated by commas.

switch Statement: where Clause

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

Within a switch statement, a where clause is used to test additional conditions against an expression.

Logical Operator !

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

The logical NOT operator, denoted by a !, is a prefix operator that negates the value on which it is prepended. It returns false when the original value is true and returns true when the original value is false.

Logical Operator &&

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

The logical AND operator, denoted by an &&, evaluates two operands and returns a Boolean result. It returns true when both operands are true and returns false when at least one operand is false.

Logical Operator ||

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

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

Combining Logical Operators

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

Logical operators can be chained in order to create more complex logical expressions. When logical operators are chained, it’s important to note that the && operator has a higher precedence over the || operator and will get evaluated first.

Controlling Order of Execution

var halloween = true if halloween { print("Trick or treat!") } // Prints: Trick or treat!

Within a Swift logical expression, parentheses, (), can be used to organize and control the flow of operations. The usage of parentheses within a logical expression overrides operator precedence rules and improves code readability.

  1. 1
    On a daily basis, we’re faced with making decisions based on certain conditions; if the weather is beautiful, we’ll go for a walk, or if it’s rainy, we’ll stay in and code! In Swift, the ability …
  2. 2
    The if statement is the most basic and fundamental type of conditional in Swift. It is used to execute some code when a given condition is true. The structure of an if statement is: if condition…
  3. 3
    A tool most commonly used together with an if statement is the else statement. An else statement is used to execute a block of code when the condition of an if statement is false. Think of the el…
  4. 4
    So far, our conditions have consisted of a single variable whose value is a Boolean, true or false. With the help of operators, we can expand on this and create conditions that utilize multiple val…
  5. 5
    Until now, we’ve been working with conditionals that can handle only one condition. If that condition is met, our program follows one course of action, otherwise it follows another. Swift provide…
  6. 6
    A standard if/else statement can get pretty lengthy - at least 5 lines of code. Many developers prefer to keep their code as concise as possible and favor a shorter syntax. Swift allows us to minim…
  7. 7
    Another type of conditional statement that exists in Swift is the switch statement. The switch statement is a popular programming tool used to check the value of a given expression against multip…
  8. 8
    One super power that the switch statement possesses, is its ability to match values to an expression that exist within intervals. An interval denotes a range used for checking whether a given val…
  9. 9
    Another noteworthy ability of the switch statement is its use of multiple values in a single case. These are known as compound cases. The switch statement will match each value within a compound …
  10. 10
    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 lo…
  11. 11
    Excellent work! In this lesson, we’ve learned the following concepts: - An if statement consists of a condition and code block that executes when the condition is true. - An else statement is im…
  1. 1
    Operators are special symbols used to assign, compare, combine, or simply, check values. Up until this point, we’ve worked with various operators supported by Swift including arithmetic operators…
  2. 2
    The logical, &&, operator evaluates two operands and returns a Boolean result. It returns true only when both operands are true and returns false when at least one operand is false. && is denoted …
  3. 3
    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 …
  4. 4
    The logical, !, operator is a prefix operator that negates, or reverses, the Boolean value of its operand. It returns false when the operand is true, and returns true, when the operand is false. …
  5. 5
    Swift allows us to combine or chain logical operators, &&, ||, and ! in order to create longer, more complex compound expressions. Take a look at the following if statement that utilizes the && a…
  6. 6
    Instead of relying on the compiler to set operator precedence, we can do so ourselves by wrapping a logical expression in parentheses, (). The use of parentheses also makes our code easier to read …
  7. 7
    Excellent work! 👏 In this lesson, we’ve learned the following concepts: - The logical && operator translates to AND and returns true only when both operands are true. It returns false in all other…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo