Can anyone help me with this "if" condition please?

K

Keith Thompson

Don Bruder said:
Don Bruder said:
if ( (SomeFunc(Item, Col) <= Limit) == (V == 1))

Would appreciate any comments/suggestions/code-simplifications that
you may be able to offer.

You have a compound boolean expression working here. And you seem to be
confused about order of evaluation.


Order of evaluation has very little to do with this.


Based on the specific way the query was phrased I'd say that order of
evaluation has practically *EVERYTHING* to do with his question. (an
impression which is further reinforced by seeing his responses in the
rest of the thread)


I disagree. In fact, I honestly don't understand how you've reached
that conclusion.

The full expression is:

(SomeFunc(Item, Col) <= Limit) == (V == 1)

which can be broken down to two subexpressions, both operands of "==":

SomeFunc(Item, Col) <= Limit)
(V == 1)

Breaking each of those down, we get 4 subexpressions:

SomeFunc(Item, Col)
Limit
V
1

(I could break it down further, but this suffices.)

There are 12 possible orders in which those 4 subexpressions can be
evaluated. The order *doesn't matter*. The result will be exactly
the same regardless of the order of evaluation, and assuming that they
must be evaluated left-to-right is not helpful in understanding what
it means. (Unless SomeFunc has side effects that can change the value
of Limit or V, but if it does then the fact that order of evaluation
is unspecified becomes very significant.)

[...]
True enough - at the compiler level. At the "I'm trying to understand
what's going on here" level, it seems to me like something pretty
crucial.

It's true at *every* level. I wasn't talking about compilers, I was
talking about what the expression actually means based on the language
definition.

Sure, left-to-right is one of the legal evaluation orders, and if
mentally processing it that way is helpful, that's fine. It's your
assertion that the expression is necessarily evaluated that way that I
dispute.
In this case, it appears that the order is critical, at least to the
poster's understanding of what the code is trying to do. It may or may
not be of importance to the compiler. But since it isn't the compiler
asking the question...

If the OP's understanding depends on assuming a left-to-right order of
evaluation, then the OP doesn't really understand it.
Consider trying to tailor your response to the question being asked - If
someone asks "what's 2+2?" you wouldn't launch into some obscure chunk
of number theory that proves that the answer is 5 in certain dimensions,
would you?

Nope.

(Well, I'd take into account the fact that it's probably a trick
question, since hardly anybody would ask such a question seriously,
and I might go off a tangent about modular arithmetic or base 3 -- or,
more likely, I might make a joke like "What kind of answer did you
have in mind?" But I digress.)
By flinging around "chapter and verse according to things deep in the
guts of the compiler", that's pretty much what you're doing.

Not at all. Again, I didn't mention compilers at all.

The fact that evaluation order is, in many cases, unspecified is a
vital fact about the C language (though some might wish it weren't).
 
N

Nate Eldredge

Lew Pitcher said:
Hi,

Hope you can help me with this one. I find the following line of
code (specifically the contents of the if statement) I quite
confusing. What does it mean and can it be simplified?

SomeFunc() takes 2 parameters and returns a type float which is
compared to Limit (which is also type float). that's fin I understand
that, but its the next part that I find confusing the "== (V == 1)"
part...


if ( (SomeFunc(Item, Col) <= Limit) == (V == 1))


Gaak! What some programmers won't do to avoid coding boolean operators.
OTOH, this condition /is/ shorter than the corresponding code w booleans.


Yep, sadly C doesn't provide an XNOR operator.

Actually, the absence of logical XOR is something that's always
aesthetically bothered me. Did they just think that `a ^^ b' looked too
strange?
 

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
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top