Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 18 / 68

Logical operators (&&, ||, !)

This lesson covers Objective-C's logical operators, with the aim of combining several conditions in a judgement.

When you want to combine conditions you use a logical operator. && means "both are true", || means "either one is true", and ! means "the opposite".

The sample code checks "18 or over and holding a ticket" with age >= 18 && hasTicket, and "under 18, or not holding a ticket" with age < 18 || !hasTicket. The BOOL type with YES and NO is characteristic of Objective-C.

A common early stumble is short-circuit evaluation. Not knowing that the right-hand side of && goes unevaluated when the left is false leads to surprises if that side has a side effect.

In professional work, these are indispensable in if statements for judging several conditions together.

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

๐Ÿงช This site can't compile or run Objective-C 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)