Math Fundamentals18 sections · 814 units
Open in Course

Short-Circuit Evaluation

Skipping unnecessary work

Most languages use short-circuit evaluation for AND and OR. If a && b has a = false, the result is false regardless of b. So the language skips evaluating b.

For OR, if a = true in a || b, the result is true no matter what b is. The language skips b again.

Why does this matter? If b is an expensive function call or has side effects, short-circuiting saves time and prevents unintended behavior. Order your conditions with cheaper checks first.