Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. SwiftBeginner - About Logical Operators

About Logical Operators - Images: Japanese

Hey there, everyone!

Next up, let's dig into 'logical operators'. These are operators that are often used together with the 'if statement' we covered last time.

The idea is something like "if this AND that" or "if this OR that." They come up all the time, so let's get a solid handle on them.

There are three logical operators: '&&', '||', and '!'. Let's start with '&&'.

'&&' is called the 'logical AND operator'. In practice, you won't hear people say "logical AND operator" very often — it's more commonly called "double ampersand" or just "and-and".

Here's how '&&' works when combined with an if statement:

if true && true { // This will execute.
    print("Executed.")
}

Take a look at the condition in the if statement — it says 'true && true'. The '&&' operator evaluates the whole expression as 'true' only when both operands (both sides) are 'true'. In this case, both sides are 'true', so the if statement runs.

On the other hand, if one side is 'false' like this:

if true && false { // This will NOT execute.
    print("Executed.")
}

it won't run.

Same goes for false && false.

if false && false { // This will NOT execute.
    print("Executed.")
}

'&&' is often described in textbooks as meaning "this AND that." If you ever forget, searching "if statement AND" should point you in the right direction.

Next up is '||'. It's called the 'logical OR operator'. Again, in real-world usage you'll mostly hear it called "double pipe" or "double bar" rather than its formal name.

'||' evaluates the whole expression as 'true' when at least one of the two operands (either side) is 'true'.

if true || false { // This will execute.
    print("Executed.")
}

Here we have 'true || false'. Since one of the operands is 'true', the if statement executes. Now take a look at this one:

if false || false { // This will NOT execute.
    print("Executed.")
}

In this case it's 'false || false' — both sides are 'false', so it won't run.

By the way, it doesn't matter which side is 'true' — either will do. So 'false || true' and 'true || true' both execute. Easy to mix up, so watch out for that.

if false || true { // This will execute.
    print("Executed.")
}

if true || true { // This will execute.
    print("Executed.")
}

Now let's look at '!'. It's called the 'logical NOT operator'.

Just to be clear — this is talking about '!' specifically in the context of logical operators. As a plain symbol, '!' is just an exclamation mark, and you can call it that without any problem.

Here's how it's used:

if !false {
    print("Executed.")
}

Notice '!false' — there's a '!' stuck right in front of 'false'.

! is a bit unique: it only takes one operand. Where && and || evaluate operands on both the left and right sides, ! only works with a single operand.

※ If you need a refresher on operands, check out this article.

What '!' does is flip the boolean value of its operand. Let's try it out with 'print()' to see it in action:

print(!true) // Outputs 'false'.
print(!false) // Outputs 'true'.

That's how '!' flips boolean values. It's a construct that shows up frequently, so it's worth committing to memory.

One important thing to note: in Swift, logical operators can only take boolean values as operands — that means only 'true' or 'false'.

For example, the following will all produce an error:

// All of the following are errors.
print(1 && 2)
print(1 || 2)
print(!1)

In other languages, there are often designated "truthy" and "falsy" values, so things like '1 || 0', '!1', or '!"hello"' can be totally valid — but in Swift, all of those are errors.

If you're coming from another language, this is an easy gotcha to fall into. Keep it in mind!

And that's all for this article!

In the next one, we'll be covering the 'switch statement'. Until then — see you next time!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .