Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 18 / 68

Logical operators (&&, ||, !)

This lesson covers Julia'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 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 Julia 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)