Question

As precedence of && is more (is this the suitable adj.?) than precedence of ||, I wrote something like this:

if (a || b && c ){ ....

I guess the program will first find b && c, let's call it d, and then find a || d, right?

I want the the program to check "a" statement first, and then if it is wrong; check b and c. Is something like this possible?

Was it helpful?

Solution

Logical expressions in C are subject to short-circuit evaluation. Now, as you noted,

a || b && c

is the same as

a || (b && c)

because of precedence.

But a is evaluated first, because evaluation order is left-to-right. Because of short-circuit evaluation, only if a is false does the program then go on to evaluate b && c. So, your code already does what you want it to.

OTHER TIPS

I want the the program to check "a" statement first, and then if it is wrong; check b and c. Is something like this possible?

Yes. You are going right.

a || b && c will be parsed as a || (b && c) because && has higher precedence than that of || operator. b and c binds to &&.
As logical operators evaluates from left to right, first a is evaluated, if it is true then b && c would not be evaluated because || operator performs short circuit evaluation of its operand.

If a evaluated as false then b && c will be evaluated, in which b is evaluated first and on being true c is evaluated, otherwise c would not be evaluated because of the same reason that&& performs short circuit evaluation of its operand.

I guess the program will first find b && c, let's call it d, and then find a || d, right?

No.

First of all, operator precedence only affects grouping of operators and operands; it does not affect the order in which expressions are evaluated. In general, evaluation order is left unspecified; for example, in an expression like a + b * c, each of a, b, and c may be evaluated in any order. Similarly, b * c must be evaluated before a + b * c, but that doesn't mean that b * c must be evaluated before a.

The || and && operators are special in that they force left-to-right evaluation; the left-hand side will be fully evaluated (and all side effects applied) first. Also, both operators use "short-circuit" evaluation; if the value of the expression can be determined from the left-hand operand, then the right-hand operand won't be evaluated at all.

Since && has higher precedence than ||, the expression will be parsed as a || (b && c), meaning the result of a will be OR'd with the result of b && c. However, since || and && force left to right evaluation, a will be evaluated first. If the result of a is 0, then b will be evaluated. If the result of b is non-zero, then c will be evaluated. If b is 0, then c won't be evaluated. If a is non-zero, then neither b nor c will be evaluated.

Here's a table showing the various possibilities; NZ means "non-zero", and NE means "not evaluated":


a    b    c      b && c      a || b && c
-    -    -      ------      -----------
0    NZ   NZ     true  (1)   true  (1)
0    NZ   0      false (0)   false (0)
0    0    NE     false (0)   false (0)
NZ   NE   NE     NE          true  (1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top