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.

  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