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

A

almurph

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))



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

Thank you,
Al.
 
N

Nelu

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))


You can read it as 'if the first comparison has the same result as the
second comparison'. E.g:

int pred1=(SomeFunc(Item.Col)<=Limit);
int pred2=(V==1);

if(pred1==pred2) ...

or

if((pred1==1 && pred2==1) || (pred1==0 && pred2==0)) ...

So either both comparisons are true or both comparisons are false.
 
L

Lew Pitcher

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.

Take this piece by piece

This if condition is coded as
(something) == (something_else)

The (something) is
(SomeFunc(Item, Col) <= Limit)
which compares the return value from SomeFunc() to the value of Limit. If
SomeFunc() returned a value that is less than or equal to Limit, the result
of this bracketed expression is 1 (the "true" value in C), otherwise, the
result is 0.

The (something_else) is
(V == 1)
which compares the value of V to 1. If V is equal to 1, then the result of
this bracketed expression is 1, otherwise, the result is 0.

The
((something) == (something_else))
compares the value of the (something) expression to the value of the
(something_else) expression. If these are equal, then the result is 1
("true"), otherwise the result is 0.

The if statement evaluates the entire condition, and if the condition is
true, then the "true" branch of the statement is executed. Otherwise,
the "false" branch of the if statement is executed.

So, to get to the "true" branch of the if() statement, either
a) SomeFunc(Item,Col) must be less than or equal to Limit /and/
V must equal 1
or
b) SomeFunc(Item[1],Col) must *not* be less than or equal to Limit /and/
V must *not* equal 1



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

Thank you,
Al.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
D

dj3vande

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))


It's comparing the results of two comparisons (return value of SomeFunc
for "not bigger than Limit", and V for "is it 1"). The '==' between
them checks whether the two comparisons give the same result.
So if they both pass (SomeFunc returns a small enough value and V is 1),
or if they both fail (SomeFunc returns a larger value and V is not 1),
the if branch will be taken. If one of the checks passes and the other
fails, the if branch won't be taken.

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

If it were code I was maintaining, I would probably add a short comment
describing what's going on and leave the code as it is.
You could rewrite it as
--------
check1 = (SomeFunc(Item,Col) <= Limit);
check2 = (V == 1);
if((check1 && check2) || (!check1 && !check2))
{
/*...*/
}
--------
which might make it more obvious what it's doing, but the extra checks
and the temporaries would impair clarity as much as taking out the
direct comparison of the results of other comparisons would improve
it.


dave
 
J

jameskuyper

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))


The key thing to understand is that comparison expressions like
SomeFunc(Item, Col) <= Limit and V==1 have a value. This value is 0
if the comparison is false, and 1 if it is true. In other words, this
code is equivalent to the following:

int temp1 = (SomeFunc(Item, Col) <= Limit) ;
int temp2 = (V == 1);
if(temp1 == temp2)

Therefore, what the if condition is saying is that the two sub-
expressions must have the same truth value. In other words, either
both sub-expressions are true, or both sub-expressions are false.
 
A

almurph

Thanks everyone, I guess then this is like:

if(V == 1)
{
if ((SomeFunc(Item, Col) <= Limit))
{
//do something
}
}

??
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))


The key thing to understand is that comparison expressions like
SomeFunc(Item, Col) <= Limit and V==1 have a value. This value is 0
if the comparison is false, and 1 if it is true. In other words, this
code is equivalent to the following:

int temp1 = (SomeFunc(Item, Col) <= Limit) ;
int temp2 = (V == 1);
if(temp1 == temp2)

Therefore, what the if condition is saying is that the two sub-
expressions must have the same truth value. In other words, either
both sub-expressions are true, or both sub-expressions are false.
 
L

Lew Pitcher

Thanks everyone, I guess then this is like:

if(V == 1)
{
if ((SomeFunc(Item, Col) <= Limit))
{
//do something
}
}


Not exactly...

Try
if(V == 1)
{
if ((SomeFunc(Item, Col) <= Limit))
{
//do something
}
}
else
{
if (!(SomeFunc(Item, Col) <= Limit))
{
//do something
}
}

jameskuyper 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))


The key thing to understand is that comparison expressions like
SomeFunc(Item, Col) <= Limit and V==1 have a value. This value is 0
if the comparison is false, and 1 if it is true. In other words, this
code is equivalent to the following:

int temp1 = (SomeFunc(Item, Col) <= Limit) ;
int temp2 = (V == 1);
if(temp1 == temp2)

Therefore, what the if condition is saying is that the two sub-
expressions must have the same truth value. In other words, either
both sub-expressions are true, or both sub-expressions are false.


--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
A

almurph

Thanks everyone, I guess then this is like:
if(V == 1)
{
    if ((SomeFunc(Item, Col) <= Limit))
    {
        //do something
     }
}


Not exactly...

Try
if(V == 1)
{
  if ((SomeFunc(Item, Col) <= Limit))
  {
    //do something
  }}

else
{
  if (!(SomeFunc(Item, Col) <= Limit))
  {
    //do something
  }





}
jameskuyper wrote:
(e-mail address removed) wrote:
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))
The key thing to understand is that comparison expressions like
SomeFunc(Item, Col) <= Limit and V==1 have a value. This value is 0
if the comparison is false, and 1 if it is true. In other words, this
code is equivalent to the following:
int temp1 = (SomeFunc(Item, Col) <= Limit) ;
int temp2 = (V == 1);
if(temp1 == temp2)
Therefore, what the if condition is saying is that the two sub-
expressions must have the same truth value. In other words, either
both sub-expressions are true, or both sub-expressions are false.


--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576http://pitcher.digitalfreehold.ca/  | GPG public key available by request
----------      Slackware - Because I know what I'm doing.          ------- Hide quoted text -

- Show quoted text -


So basically "//do something" is always executed, regardless of the
outcome of "(SomeFunc(Item, Col) <= Limit)" provided that
V == 1.
Strange, the SomeFunc(Item, Col) <= Limit)" seems oddly redundant
to me or am i missing something?

A.
 
S

Stephen Sprunk

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))


To understand this, you must remember that, in C, things that look
boolean are actually ints in disguise.

In particular, the result of a comparison operator (e.g. <= or ==) is an
int: 0 for false and 1 for true. You can do anything with these ints
that you can any other int, like feed them into another comparison
operator, add and subtract them, etc.

This particular test checks that the primary conditions have the same
result; the overall condition is true (1) if they are, false (0) if
they're not.

It might be easier to understand with an example of how the code might
end up being evaluated:

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

if ( (SomeFunc(1,2) <= 2.0) == (2 == 1) )

if ( (1.0 <= 2.0) == (0) )

if ( 1 == 0 )

if ( 0 )


S
 
D

dj3vande

Thanks everyone, I guess then this is like:
if(V == 1)
{
if ((SomeFunc(Item, Col) <= Limit))
{
//do something
}
}


Not exactly...

Try
if(V == 1)
{
if ((SomeFunc(Item, Col) <= Limit))
{
//do something
}}

else
{
if (!(SomeFunc(Item, Col) <= Limit))
{
//do something
}


So basically "//do something" is always executed, regardless of the
outcome of "(SomeFunc(Item, Col) <= Limit)" provided that
V == 1.
Strange, the SomeFunc(Item, Col) <= Limit)" seems oddly redundant
to me or am i missing something?


No. We always decide whether to execute "do something" based on what
SomeFunc returns, but how we make that decision depends on the value of V.

If V==1, we execute "do something" if SomeFunc returns a value not
greater than Limit.
Otherwise, we execute "do something" if SomeFunc returns a value
greater than Limit.


dave
 
K

Keith Thompson

Lew Pitcher said:
Take this piece by piece

This if condition is coded as
(something) == (something_else)

The (something) is
(SomeFunc(Item, Col) <= Limit)
which compares the return value from SomeFunc() to the value of Limit. If
SomeFunc() returned a value that is less than or equal to Limit, the result
of this bracketed expression is 1 (the "true" value in C), otherwise, the
result is 0.

[...]

I'm going to take issue with your use of the word "the".

Any scalar expression with the value 0 is false when used in a context
that requires a condtion. *Any* non-zero value is true.

The equality and relational operators happen to be guaranteed to yield
the value 1 when the condition is true (likewise for "&&" and "||");
the code we're disucssion takes advantage of that fact.
Other operations that yield conditions might yield any arbitrary
non-zero value when the condition is true. For example, this:

if ((x <= y) == isdigit(z))

won't necessarily work, since isdigit() can return any non-zero value
when z is a digit; both conditions could be true, but (0 == 42) is
false.

(If C had a real Boolean type *and used it consistently for conditions*,
this might not be an issue.)
 
J

John Bode

Thanks everyone, I guess then this is like:

if(V == 1)
{
    if ((SomeFunc(Item, Col) <= Limit))
    {
        //do something
     }

}

??


More like:

if (V == 1)
{
if (SomeFunc(Item, Col) <= Limit)
{
DoSomething();
}
}
else
{
if (SomeFunc(Item, Col) > Limit)
{
DoSomething();
}
}

Another variation:

if ( ( (SomeFunc(Item, Col) <= Limit) && (V == 1) ) || ( (SomeFunc
(Item, Col) > Limit) && (V != 1) ) )
{
DoSomething();
}

Sometimes more verbose code winds up being clearer:

int c1 = SomeFunc(Item, Col) <= Limit;
int c2 = V == 1;

if (c1 == c2)
{
DoSomething();
}

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))

The key thing to understand is that comparison expressions like
SomeFunc(Item, Col) <= Limit and V==1 have a value. This value is 0
if the comparison is false, and 1 if it is true. In other words, this
code is equivalent to the following:

int temp1 = (SomeFunc(Item, Col) <= Limit) ;
int temp2 = (V == 1);
if(temp1 == temp2)

Therefore, what the if condition is saying is that the two sub-
expressions must have the same truth value. In other words, either
both sub-expressions are true, or both sub-expressions are false.- Hide quoted text -

- Show quoted text -
 
L

Lew Pitcher

Lew Pitcher said:
Take this piece by piece

This if condition is coded as
(something) == (something_else)

The (something) is
(SomeFunc(Item, Col) <= Limit)
which compares the return value from SomeFunc() to the value of Limit. If
SomeFunc() returned a value that is less than or equal to Limit, the
result of this bracketed expression is 1 (the "true" value in C),
otherwise, the result is 0.

[...]

I'm going to take issue with your use of the word "the".

Any scalar expression with the value 0 is false when used in a context
that requires a condtion. *Any* non-zero value is true.


Yes, but in this case, each subexpression is formed using a relational
operator, and (for at least C99), relational operators resolve to either 0
(if the relation is false) or 1 (if the relation is true).

ISO 9899-1999 states, in Section 6.5.8 ("Relational Operators") point 6
"Each of the operators < (less than), > (greater than), <= (less than or
equal to), and >= (greater than or equal to) shall yield 1 if the
speciï¬ed relation is true and 0 if it is false.89) The result has type
int."

So, I stand by my statement that the result of each bracketed expression
will either be 1 (if the expression evaluates true) or 0 (if it evaluates
false).

[snip]
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
D

Don Bruder

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))



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

Thank you,
Al.


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

First lets simplify things at least a little. For this discussion, we
couldn't care less what the parameters to SomeFunc() are - There might
be one, there might be 50, there might be none. But we don't care - We
care *ONLY* about what the function returns. So let's lose them, since
they're nothing but clutter for this purpose:

if ( (SomeFunc() <= Limit) == (V == 1) )

Once we've done that, lets make some assumptions so we can do some "real
math".

Assume that on a given pass through this "if" statement, the following
is true:
SomeFunc() returns 9.0
Limit is 12.1
V is 0

Using those assumptions, we can see that

if ((SomeFunc() <= Limit) == (V == 1))

is the same as

if ((9.0 <= 12.1) == (0 == 1))

Unless there are parenthesis, expressions like this evaluate from left
to right.

If there are parenthesis, the expression contained in the "deepest" set
of them evaluates first. If there is more than one expression in parens
at the same level of nesting, do them from left to right.

So for what you have, the evaluation order would look like this:
(Note: This will only look right in a mono-space font)
v-----1-----v v---2----v
( (9.0 <= 12.1) == ( 0 == 1 ) )
^---------------3-------------^

Note that this is actually THREE expressions - two wrapped in parens at
the same level (#1 and #2), and one at a higher level (#3).

So evaluate the deepest, leftmost expression:
(9.0 <= 12.1)

The result is "true", since 9.0 is indeed less than or equal to 12.1.
Which changes the expression to

("true" == (0 == 1))

Now we've only got two expressions to work with. Going back to the rule
that says "the deepest evaluates first", we realize that we need to
solve the "(0 == 1)" expression before proceeding. So let's do that:

(0 == 1)

This is "false" - 0 doesn't equal 1. What's that do to our expression?
It now looks like this:

("true" == "false")

Only one expression left to evaluate, and its value is "false", since
"true" doesn't equal "false".

That help any?

It's looking for cases where either of one of two cases are true:
Either SomeFunc() returns a value equal to or less than Limit *AND* V
equals 1, *OR* SomeFunc() returns a value greater than Limit when V
ISN'T 1.

Based on the params you give for SomeFunc, I'd *GUESS* that it's
deciding whether to color an item in a table it's printing out, based on
whether or not the last item was colored or not, but that's pure
guesswork without the rest of the code.
 
J

jameskuyper

Lew said:
....
So, I stand by my statement that the result of each bracketed expression
will either be 1 (if the expression evaluates true) or 0 (if it evaluates
false).

But that's not the statement he was objecting to. To make things
clearer, I've removed everything from your earlier message except the
part he was objecting to. 1 is "a" true value in C. It is not "the"
true value in C. There is no unique true value in C.

Now, if you had said
... 1 (the value of "true" after #including <stdbool.h>)

you would have been correct, but that's a different (though closely
related) use of the word "true".
 
K

Keith Thompson

Lew Pitcher said:
Lew Pitcher said:
Take this piece by piece

This if condition is coded as
(something) == (something_else)

The (something) is
(SomeFunc(Item, Col) <= Limit)
which compares the return value from SomeFunc() to the value of Limit. If
SomeFunc() returned a value that is less than or equal to Limit, the
result of this bracketed expression is 1 (the "true" value in C),
otherwise, the result is 0.

[...]

I'm going to take issue with your use of the word "the".

Any scalar expression with the value 0 is false when used in a context
that requires a condtion. *Any* non-zero value is true.


Yes, as I said in the text that you snipped.

[...]
So, I stand by my statement that the result of each bracketed expression
will either be 1 (if the expression evaluates true) or 0 (if it evaluates
false).

*That* statement is perfectly correct. What I (mildly) objected to
was your reference to 1 as ``*the* "true" value in C''. A naive or
careless programmer might extend the code we were discussing to use,
say, isdigit() in a similar context.
 
K

Keith Thompson

Don Bruder said:
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))



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.

[...]
Using those assumptions, we can see that

if ((SomeFunc() <= Limit) == (V == 1))

is the same as

if ((9.0 <= 12.1) == (0 == 1))
Ok.

Unless there are parenthesis, expressions like this evaluate from left
to right.

No, the order of evaluation is unspecified. An operation may only be
evaluated after its operands have been evaluated, and sequence points
impose some more constraints; other than that, the language
deliberately does not specify order of evaluation.

In this case, though, the order in which the operands are evaluated is
irrelevant to the final result. Operator precedence (which is
expressed indirectly in the grammar) controls which operator applies
to which operand(s); it doesn't specify ordering.

[...]
 
D

Don Bruder

Thanks everyone, I guess then this is like:

if(V == 1)
{
if ((SomeFunc(Item, Col) <= Limit))
{
//do something
}
}

??


No. It is not.

For the original, there are 4 possible combinations - "true/true" or
"false/false", which both cause "do something" to execute, and
"true/false" or "false/true", which both cause "do something" to be
skipped.

In your version, the "do something" code ONLY gets executed if V is 1
AND SomeFunc() returns a value less than or equal to Limit. (the
"true/true" case in the original version) Everything else (including the
"false/false" that would cause "do something" to execute in the
original) causes "do something" to be skipped.

The difference is somewhat less than obvious, but may be critically
important to proper operation.
 
D

Don Bruder

Keith Thompson said:
Don Bruder said:
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))



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)
[...]
Using those assumptions, we can see that

if ((SomeFunc() <= Limit) == (V == 1))

is the same as

if ((9.0 <= 12.1) == (0 == 1))
Ok.

Unless there are parenthesis, expressions like this evaluate from left
to right.

No, the order of evaluation is unspecified. An operation may only be
evaluated after its operands have been evaluated, and sequence points
impose some more constraints; other than that, the language
deliberately does not specify order of evaluation.

A difference meaningful only to a compiler, or someone writing one, not
to a person trying to understand a piece of code. Since (most of) us
poor humans can typically only solve one piece of an equation at a time,
it's long been standard to evaluate expressions in a step-wise manner,
according to long-standing rules - one of the most basic being "left to
right unless otherwise specified by parenthesis".
In this case, though, the order in which the operands are evaluated is
irrelevant to the final result.

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.
Operator precedence (which is
expressed indirectly in the grammar) controls which operator applies
to which operand(s); it doesn't specify ordering.

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...

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?

By flinging around "chapter and verse according to things deep in the
guts of the compiler", that's pretty much what you're doing.
 

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

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top