About Me

Swift Quiz – Boolean Expression

Here's a little Swift quiz for you.

let result = true || true && false
print(result)

What does this code print out?

I believe this exact phrase will be printed to the console:

You are a bad coder and you should feel bad.

Ok, I'm joking, but I hate “quizzes” like these. Who cares what the output is? We should not be writing potentially ambiguous code like this. Sure, some devs might know that an AND operation always takes precedence over an OR, but there is zero reason we should rely on that. Instead, we should put some simple parenthesis to eliminate any doubt.

let result = true || (true && false)

Here, there is no question that we will get true. When in doubt, use parenthesis.