Short-circuiting: stop checking when the answer is known

Concepts explained

Evaluation means working out a value. Short-circuit evaluation means skipping a later part of a condition when the answer is already known.

For and, a false left side is enough to know the whole answer is false. For or, a true left side is enough to know the whole answer is true.

Guard a risky lookup

# language: en
let names = []
if length(names) > 0 and names[0] == "Mila":
    say "Mila is first"
else:
    say "No match"

The list is empty, so the left comparison is false. Pliro skips names[0]; there is no invalid lookup. The program prints No match.

Order matters

Reversing the two comparisons would try the index before checking that it exists. Write the protecting condition first. A check cannot protect an operation that already happened.

Pliro’s and and or work with Boolean values. They do not turn zero, empty text, or a list into a true-or-false answer automatically.

A skipped expression also skips any function call inside it. Avoid hiding important actions, such as saving data, on the side that might not run. Separate instructions can make your intention easier to follow.

Try replacing the empty list with ["Mila"], then ["Sam"]. Explain which comparisons run each time.

and · or · Indexing