Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Can anyone help me with this "if" condition please?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Don Bruder, post: 3783581"] [i] 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.[/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Can anyone help me with this "if" condition please?
Top