What is Short-circuit evaluation?

A way of evaluating the logical operators and and or that skips the right side when the left side alone decides the result. With and, the right side is not evaluated if the left side is false; with or, if the left side is true. It is often used to prevent out-of-range accesses by putting the array range check on the left.

Japanese name: 短絡評価

Short-circuit evaluation appears in 4 practice questions across 3 topics, including Programming Basics (Pseudocode Tracing), C Programming Basics, Software Development and Project Management.

Field: Programming Basics (Pseudocode Tracing)

When it changes the result

When the right side contains a function call or an array access, the result can depend on whether evaluation is short-circuited. In "i ≦ n and a[i] ≠ x", once i exceeds n the left side is false, so a[i] is not read outside the array. Swapping the two sides would read outside the array first. If a function called on the right side changes variables, the number of times it is called also changes.

In programming languages

In many languages, such as C, Java and Python, the logical operators && and || (and and or in Python) use short-circuit evaluation. In C and Java, & and | always evaluate both sides. When short-circuit evaluation affects the result in a pseudocode question, follow the explanation in the question.

Writing conditions with it

Short-circuit evaluation lets you write in one expression a condition that only makes sense when an earlier condition holds, such as "within the array range, and that element satisfies the condition". Putting an expensive test on the right side skips it when the left side already decides the result. On the other hand, work that must always run should not be placed in a function on the right side, because it may not be called.

Try it: questions about Short-circuit evaluation

For the decision 'A and B' (A and B are conditions whose truth values can be set independently), two test cases were run: (A = true, B = false) and (A = false, B = true). Which statement about coverage criteria is correct?

  1. ABranch coverage is achieved, but condition coverage is not
  2. BBoth branch coverage and condition coverage are achieved
  3. CCondition coverage is achieved, but branch coverage is not
Show answer and explanation

Answer: C. Condition coverage is achieved, but branch coverage is not

Condition coverage requires each individual condition in a decision (A and B) to take both true and false at least once. In the two test cases, A and B each take true and false once, so condition coverage is achieved. However, 'A and B' is false in both test cases, and the branch for a true decision is never taken, so branch coverage is not achieved. Because condition coverage alone does not guarantee branch coverage, criteria that satisfy both, such as decision/condition coverage, are used. Note that this reasoning uses the values given to each condition; with short-circuit evaluation, as with && in C, where the right side is not evaluated when the left side is false, one may instead count only the conditions actually evaluated.

FAQ

Is short-circuit evaluation an optimization?

In C, Java, Python and similar languages, short-circuit evaluation is not a compiler optimization but part of the language definition. Programs that rely on the right side not being evaluated are therefore guaranteed to work correctly.

Are not (A or B) and not A and not B the same?

By De Morgan's laws they have the same truth value. Both not (i > n or a[i] = x) and i ≦ n and a[i] ≠ x skip evaluating a[i] when the condition on i alone decides the result.

Short-circuit evaluation in the explanations

Questions that mention Short-circuit evaluation (4)

  1. In the following program, and does not evaluate its right side when the left side is false (short-circuit evaluation). cnt is a global variable. What is the value of cnt after the program runs?
  2. The following program searches the n-element array a for the value x and stores the index where it is found in i. Array indices start at 1. and and or do not evaluate their right side when the left side alone decides the result. Which condition in 【a】 reads a[n + 1], outside the array, when x is not in a?
  3. Which of the following is the output when the code above, with b++ on the right side of && and ||, is executed?
  4. For the decision 'A and B' (A and B are conditions whose truth values can be set independently), two test cases were run: (A = true, B = false) and (A = false, B = true). Which statement about coverage criteria is correct?

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Variable Array Branch coverage Argument Bitwise operation Control structure Integer division Modulo operation Object-oriented programming Off-by-one error

Back to the glossary