Questions tagged [short-circuit-evaluation]

Short-circuit evaluation is an evaluation strategy for boolean operators that consist of evaluation only the minimal terms needed to find out the result. This is an ultra-specialized tag and the more general tag evaluation should be preferred.

What is it?

Short-circuit evaluation is an evaluation strategy for boolean operators that consist of evaluation only the minimal terms needed to find out the result.

In C, C++, and Java for example, && and || have a short-circuit evaluation, unlike & and | which have a systematic evalauation of all terms.

How does it work?

For a succession of AND terms, the evaluation stops as soon as a term is false, since whatever the next terms will be, the overall result will no longer be true.

For a succession of OR terms, the evaluation stops as soon as a term is true, since whatever the next terms will be, the overall result will no longer be false.

This approach allow to combine in a single expression terms that shall only be evaluated in case of some conditions that ensure their feasibility. For example: a!=0 && b/a>10 the second term can only be calucalated if the first term is successful. The short circuit ensures that the second term will not be evaluated if the first term fails.

Related tags

  • This tag is ultra-specialized an the more general tag should be preferred.
  • For more general questions about evaluation strategies, in which short-circuit is only a part, use

See also

30 questions
29
votes
3 answers

How to prevent short-circuit evaluation?

This is a problem that occurred to me while working on a Django project. It's about form validation. In Django, when you have a submitted form, you can call is_valid() on the corresponding form object to trigger the validation and return a Boolean…
3
votes
7 answers

Breaking out of multiple functions (short circuiting) in PHP

I want to return multiple nested functions in PHP. It's possible to break out of multiple loops by adding a number after "break". Eg. while(1) while(1) while(1) break 3; Can I do a circuit break while calling a sequence of functions?
Leo Jiang
  • 24,497
  • 49
  • 154
  • 284
3
votes
1 answer

Can someone explain me why "operator precedence" applies to logical operators like "||", "&&" in javaScript

Can someone explain me why operator precedence applies to logical operators like || and && in JavaScript? What does that mean in an operation like: true || false && false the false && false is evaluated first because the && operator is having a…
2
votes
1 answer

Does PySpark support the short-circuit evaluation of conditional statements?

I want to create a new boolean column in my dataframe that derives its value from the evaluation of two conditional statements on other columns in the same dataframe: columns = ["id", "color_one", "color_two"] data = spark.createDataFrame([(1,…
Adil B
  • 14,635
  • 11
  • 60
  • 78
2
votes
1 answer

In Python 3, is it possible to use short-circuit evaluation in an any call that doesn't have a for .. in expression in it?

In Python 3, is it possible to use short-circuit evaluation in an any call that doesn't have a for .. in expression in it? I know that I can take advantage of short-circuit evaluation in a statement like this: print('Hello' if True or 3/0 else…
user2023861
  • 8,030
  • 9
  • 57
  • 86
2
votes
2 answers

Object destructuring with Short-Circuit Evaluation

Is it possible to do achieve something like this? const obj1 = { name: 'tom' } const obj2 = { age: 20 } let { name, age } = obj1 || obj2 Getting as a result -> name = 'tom' and age=20 The code above doesn't work, as it evaluates the condition one…
1
vote
2 answers

Short Circuit Evaluation with Components in Unity

I have a line of code: Rigidbody rigidbody = go.GetComponent() ? go.GetComponent() : go.AddComponent(typeof(Rigidbody)) as Rigidbody; where go is a GameObject. When I try to simplify this using short circuit evaluation to…
1
vote
3 answers

In JavaScript, how can I conditionally assign value to object with destructuring and short circuit evaluation?

Let's say I have this set up: const objA = { name: "Jacob", email: "jacob@email.com" }; const objB = { lastName: "Smith" }; Why can I do this: const lastName = objA.lastName || objB.lastName; But not this? const { lastName } = objA || objB; Was…
1
vote
3 answers

I’m learning to code in C and cant understand the short circuit behaviour of the logical expressions. Do they not follow the precedence order

I’m learning to code in C. I was going through the logical expressions but then this question came and I couldn’t understand ? Like are logical operators exceptional in case of precedence. Let’s take this one example: i = 1; j = 1; k = 1; printf("%d…
1
vote
1 answer

Shorthand Shortcircut. Is it possible to do some stuff

This question is about a TypeScript code-base, but the general concepts that underpin it are ECMAScript concepts, therefore; I have tagged the question as both JS & TS. If I should tag it differently, you can either let me know, or make the edit…
JΛYDΞV
  • 8,532
  • 3
  • 51
  • 77
0
votes
2 answers

Short Circuit Evaluation of Logicals in Impala?

I am new to Impala having come from an Oracle SQL background. I am tasked with improving an existing SQL script from a performance point of view. The existing script includes the following in the where clause colA > 0 and colB - colA > 10 I am…
0
votes
0 answers

C++ What are the conditions of short circuit evaluation?

if (i - word.size() >= 0 && dp[i - word.size()] && s.substr(i - word.size(), word.size()) == word) dp[i] = true; i - word.size() >= 0 is the condition that I was using to prevent an out-of-bound indexing. The expectation was that if the…
0
votes
2 answers

Why does this short circuit evaluation return undefined if first value is false?

I have an example similar to this where the first expression evaluates to false and the second is undefined but the overall expression returns undefined into valueResult, shouldn't the first false value terminate the check and return…
j obe
  • 1,759
  • 4
  • 15
  • 23
0
votes
1 answer

In Scheme, does `or` and `and` short circuit?

Do and and or short circuit in Scheme? The following are two implementation of lat? (list of atoms). One uses cond … else and the other uses or and and. I was wondering if they are equivalent and the answer to that hinges on whether or and and have…
joseville
  • 685
  • 3
  • 16
0
votes
1 answer

What's the AHK equivalent of one-line logical OR short-circuit evaluation?

For example, in JavaScript, the following code would log foo: false || console.log('foo') How to achieve the same logic in one line in AutoHotkey?
1
2