Short circuiting

An important fact about if statements is what happens when there are multiple Boolean conditions separated by ANDs (&&) or ORs (||).

Consider the following code:

if 1 > 2 && name == "Matt Galloway" {

// ...

}

The first condition of the if-statement, 1 > 2 is false. Therefore the whole expression cannot ever be true. So Swift will not even bother to check the second

part of the expression, namely the check of name. Similarly, consider the following code:

if 1 < 2 || name == "Matt Galloway" {

// ...

}

Since 1 < 2 is true, the whole expression must be true as well. Therefore once again, the check of name is not executed.

This will come in handy later on when you start dealing with more complex data types.

results matching ""

    No results matching ""