operators priority

M

Mark

Am I right that in the following code I don't need to put parentheses in
order to have it evaluated correctly (i.e. first verify that the result of
a() isn't equal to 10, and then check if the result of b() greater than 7):

if (a() != 10 || b() > 7)
return -1;
 
I

Ian Collins

Am I right that in the following code I don't need to put parentheses in
order to have it evaluated correctly (i.e. first verify that the result
of a() isn't equal to 10, and then check if the result of b() greater
than 7):

if (a() != 10 || b() > 7)
return -1;

Yes. But they do no harm if you are unsure.
 
C

Charles Richmond

Mark said:
Am I right that in the following code I don't need to put parentheses in
order to have it evaluated correctly (i.e. first verify that the result
of a() isn't equal to 10, and then check if the result of b() greater
than 7):

if (a() != 10 || b() > 7)
return -1;

Logical "and" and "or" will "short circuit". But you should be
using "and" (&&) and *not* "or" (||). This is *not* because of
operator precedence, but the way that logical "and" and "or" are
defined in C.

With the logical "and", if the left argument is false (0), then
the right argument is *not* evaluated. Whether the right argument
is true or false, the result of the "and" will be false if the
left argument is false.

With the logical "or", if the left argument is false (0), then the
right argument *is* evaluated. If the left argument of a logical
"or" is false, then the value of the operation depends on whether
the right argument is true or false.

--
+----------------------------------------+
| Charles and Francis Richmond |
| |
| plano dot net at aquaporin4 dot com |
+----------------------------------------+
 
P

Phil Carmody

Mark said:
Am I right that in the following code I don't need to put parentheses
in order to have it evaluated correctly (i.e. first verify that the
result of a() isn't equal to 10, and then check if the result of b()
greater than 7):

if (a() != 10 || b() > 7)
return -1;

Parentheses, as long as they didn't completely reshape the
expression's parse tree, would make no difference at all.
The result of a() will be compared against 10, and if it's
equal to 10 (i.e. the first test *fails*, as you used '||'),
only then will b() be called and its result compared against
7.

Phil
 
A

August Karlstrom

Am I right that in the following code I don't need to put parentheses in
order to have it evaluated correctly (i.e. first verify that the result
of a() isn't equal to 10, and then check if the result of b() greater
than 7):

if (a() != 10 || b() > 7)
return -1;

Yes, you are correct. Still, my preference is to use parentheses in this
case as in mathematical notation, e.g.

(a ≠ 10) ∨ (b > 7)


August
 
M

Morris Keesan

Am I right that in the following code I don't need to put parentheses in
order to have it evaluated correctly (i.e. first verify that the result
of a() isn't equal to 10, and then check if the result of b() greater
than 7):

if (a() != 10 || b() > 7)
return -1;

The code is equivalent to
if ( (a() != 10) || (b() > 7) )
return -1;

The choice of which form to use (along with where you put whitespace)
should be based on which form you find more readable, and which form you
think will be more readable to the next person who looks at your code.
 
K

Keith Thompson

Charles Richmond said:
Logical "and" and "or" will "short circuit". But you should be
using "and" (&&) and *not* "or" (||). This is *not* because of
operator precedence, but the way that logical "and" and "or" are
defined in C.

How did you reach that conclusion? Mark's description says that
he checks whether a() is unequal to 10, and then whether b() is
greater than 7. He doesn't say (other than in the code) what he
does with the results of the two checks.

I took the "and then" to refer to the chronological sequence of
the comparisons. You apparently took it to imply a logical "and"
operation, but I don't think that's how Mark meant it.
 
K

Keith Thompson

pete said:
Charles said:
Logical "and" and "or" will "short circuit". But you should be
using "and" (&&) and *not* "or" (||). This is *not* because of
operator precedence, but the way that logical "and" and "or" are
defined in C.
[...]

I want to emphasize that Charles Richmond and Phil Carmody are right.
The shown code is wrong for the description
of what it supposed to do.

I disagree; see my other response. But Phil didn't say that the use of
"||" is wrong, he merely noted it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,093
Messages
2,570,610
Members
47,230
Latest member
RenaldoDut

Latest Threads

Top