Ad space (banner)
๐Ÿ“ŠR Lessons
Lesson 18 / 68

Logical operators (&&, ||, !)

This lesson covers R'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 && has_ticket, and "under 18, or not holding a ticket" with age < 18 || !has_ticket.

A common early stumble is the difference between & and &&. In R, & compares element by element across a vector while && compares single values — a distinction particular to R.

In professional work, the split matters: && for the condition of an if statement, & for applying a condition across a whole vector.

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

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