Ad space (banner)
๐ŸฆSwift Lessons
Lesson 18 / 68

Logical Operators (&&, ||, !)

In this lesson you will learn the logical operators in Swift so you can combine several conditions in one decision. This is for anyone searching for "Swift logical operators how to use".

When you want to combine conditions you use logical operators. && means "both are true", || means "at least one is true", and ! means "the opposite".

The sample code checks "18 or older and holding a ticket" with age >= 18 && hasTicket, and "under 18, or not holding a ticket" with age < 18 || !hasTicket.

A common stumbling block for beginners is short-circuit evaluation. If you do not know that the right-hand side of && is skipped when the left is false, code with side effects on the right can behave unexpectedly.

In real-world development these operators are indispensable in if statements whenever you need to test several conditions at once.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Swift directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)