449,411 Members | 1,030 Online
Need help? Post your question and get tips & solutions from a community of 449,411 IT Pros & Developers. It's quick & easy.

# I have a doubt

 P: n/a hi all, if I have: if(A && B || C) which operation gets executed first? If I remeber well should be &&, am I correct? thanks Ivan Dec 7 '06 #1
122 Replies

 P: n/a ivan said: hi all, if I have: if(A && B || C) which operation gets executed first? See pages 21 and 53 of K&R2. Page 21 for a detailed answer to your question, and page 53 for a more generic answer. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Dec 7 '06 #2

 P: n/a ivan wrote: hi all, if I have: if(A && B || C) which operation gets executed first? A is evaluated first. If I remeber well should be &&, am I correct? Not until A is evaluated. Dec 7 '06 #3

 P: n/a "Martin Ambuhl"

 P: n/a On Thu, 7 Dec 2006 10:36:51 +0100, "ivan" wrote: >hi all,if I have:if(A && B || C)which operation gets executed first?If I remeber well should be &&, am I correct?thanksIvan Assuming you meant by "operation" the "&&" and "||" operators, the C answer is the "&&" operator gets evaluated first. But that's not the right answer, because I don't know that you meant that, and others reading your code might not know that you meant that, or even know that's that the C answer. The correct answer, in order to appease us all sitting here at the round table, is that you must use parenthesis around the appropriate pair of binary operands. Pick either (A && B) or (B || C). During the code review we'll tell you if you're right or wrong. And most likely, even before that, you'll know whether you're right or wrong. C lets you get away with a lot of stuff. That doesn't mean you should necessarily take the freedom to get away with said stuff. Some organizations, such as MISRA (which deals in part with safety-critical software), take such "stuff" very seriously, and impose rules about what you can and cannot do in C. Many of the rules are, IMHO, seriously worth considering in any type of software. -- jay Dec 7 '06 #5

 P: n/a ivan wrote: > "Martin Ambuhl" ivan wrote: hi all, if I have: if(A && B || C) which operation gets executed first? A is evaluated first. If I remeber well should be &&, am I correct? Not until A is evaluated. sorry but i do not have a C book at hand. what I am saing is: A && B || C is the same as: (A&&B)||C or the same as: A&&(B||C) You're confusing order of evaluation with operand grouping. In both `(A && B) || C` and `A && (B || C)` the first of the two operators to be evaluated [1] is the `&&` and the first operand to be evaluated is `A`. This is because && and || are Special and have defined evaluation ordering. Most [2] of the other C operators don't. What's more, for both && and || the right-hand operand is evaluated only if the left-hand operand hasn't already determined the value of the expression. [1] Well, if by "operators" we mean "the expressions with those operators". [2] The comma /operator/ [3] and the ?: conditional operator being the other two. [3] Not the comma that separates function argument expressions. -- Chris "Perikles triumphant" Dollin Meaning precedes definition. Dec 7 '06 #6

 P: n/a ivan wrote: > .... snip ... > what I am saing is: A && B || C is the same as: (A&&B)||C or the same as: A&&(B||C) Why bother? Simply use the explicit parenthesis. -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. Dec 7 '06 #7

 P: n/a You're confusing order of evaluation with operand grouping. In both `(A && B) || C` and `A && (B || C)` the first of the two operators to be evaluated [1] is the `&&` and the first operand to be evaluated is `A`. This is because && and || are Special and have defined evaluation ordering. Most [2] of the other C operators don't. What's more, for both && and || the right-hand operand is evaluated only if the left-hand operand hasn't already determined the value of the expression. So if I got it right it should be: 1. in the case of (A && B) || C: if A=0 then the result is C, 2. in the case of A && (B || C): if A=0 then the result is 0. 3. in the case of A && B || C it evaluates from left to right, hence it is the same as 1. 4. if I have A || B && C it is the same as (A || B) && C am I correct? thanks ivan Dec 7 '06 #8

 P: n/a "CBFalconer"

 P: n/a ivan wrote: >You're confusing order of evaluation with operandgrouping.In both `(A && B) || C` and `A && (B || C)` the firstof the two operators to be evaluated [1] is the `&&` and thefirst operand to be evaluated is `A`.This is because && and || are Special and have definedevaluation ordering. Most [2] of the other C operatorsdon't. What's more, for both && and || the right-handoperand is evaluated only if the left-hand operand hasn'talready determined the value of the expression. So if I got it right it should be: 1. in the case of (A && B) || C: if A=0 then the result is C, 2. in the case of A && (B || C): if A=0 then the result is 0. Yes. 3. in the case of A && B || C it evaluates from left to right, In the case of `A && B || C`, the grammar forces the grouping `(A && B) || C`. (Note that this is /not/ "evaluates from left to right"; in `A || B && C` the grouping is `A || (B && C)`.) hence it is the same as 1. Not "hence", but yes, the answer is the same as (1). 4. if I have A || B && C it is the same as (A || B) && C am I correct? No, as per my comments on (3). && is more binding than ||; ie, ||-expressions can have &&-expressions as operands, but not vice-versa unless bracketed. I'm going to say this again: there are rules for grouping, and there are rules for evaluation, and /they are not the same thing/, although the evaluation rules must respect the grouping. -- Chris "Perikles triumphant" Dollin "Who do you serve, and who do you trust?" /Crusade/ Dec 7 '06 #10

 P: n/a ivan wrote: "CBFalconer"

 P: n/a Chris Dollin wrote: ivan wrote: >>You're confusing order of evaluation with operandgrouping.In both `(A && B) || C` and `A && (B || C)` the firstof the two operators to be evaluated [1] is the `&&` and thefirst operand to be evaluated is `A`.This is because && and || are Special and have definedevaluation ordering. Most [2] of the other C operatorsdon't. What's more, for both && and || the right-handoperand is evaluated only if the left-hand operand hasn'talready determined the value of the expression. So if I got it right it should be:1. in the case of (A && B) || C: if A=0 then the result is C,2. in the case of A && (B || C): if A=0 then the result is 0. Yes. Nit-picking, but the result is 0 or 1. In 1. the result depends on evaluating C, but C=-1 will still result in the expression evaluating to one. -- imalone Dec 7 '06 #12

 P: n/a ma**********@pobox.com wrote: (WRT "A && B || C"): Before putting the file back in to your repository, why not add the parentheses so that the next time you or someone else reads the file, they can see what that line of code means straight away? Am I the only poster who abhors explicit parentheses in such simple cases? If we are to assume that the code maintainer cannot be trusted to know or figure out the correct order of evaluation, what argues against such abominations as int foo = bar + (baz*quux); ? Or, horrors, void strcpy( char *dst, char *src ) { while ( *(dst++) = *(src++) ); } At some point the maintainer has to be expected to have a reasonable knowledge of operator precedence so such ugliness can be avoided. -- C. Benson Manica | I *should* know what I'm talking about - if I cbmanica(at)gmail.com | don't, I need to know. Flames welcome. Dec 7 '06 #13

 P: n/a Christopher Benson-Manica wrote: ma**********@pobox.com wrote: (WRT "A && B || C"): >Before putting the file back in to your repository, why not add theparentheses so that the next time you or someone else reads the file,they can see what that line of code means straight away? Am I the only poster who abhors explicit parentheses in such simple cases? No. (It depends how complex the A/B/C expressions are, though.) At some point the maintainer has to be expected to have a reasonable knowledge of operator precedence so such ugliness can be avoided. Indeed. -- Chris "Perikles triumphant" Dollin Meaning precedes definition. Dec 7 '06 #14

 P: n/a Christopher Benson-Manica wrote: Am I the only poster who abhors explicit parentheses in such simple cases? You may want to choose your fights more wisely. There is nothing wrong with parens or braces, and there is nothing wrong with reliance on eval rules. What I'm saying is, YOU are right, and whoever you are arguing with is ALSO right. What that means is you can't win, unless you are literally the policy maker. If it's YOUR project and YOU make the rules, then by all means, lay down the law. Dec 7 '06 #15

 P: n/a james of tucson wrote: Christopher Benson-Manica wrote: >Am I the only poster who abhors explicit parentheses in such simplecases? You may want to choose your fights more wisely. He wasn't fighting: he was expressing an opinion. There is nothing wrong with parens or braces, In appropriate quantities. Clutter is clutter. When the helpful-to-the-reader brackets become troublesome, they are no longer helpful. (Opinions about how much it takes to be clutter may reasonably vary.) and there is nothing wrong with reliance on eval rules. I should jolly well hope not: if one can't rely on the evaluation rules, one is highly rotated. What I'm saying is, YOU are right, and whoever you are arguing with is ALSO right. What that means is you can't win, unless you are literally the policy maker. Or if you're not fighting. If it's YOUR project and YOU make the rules, then by all means, lay down the law. -- Chris "Perikles triumphant" Dollin Scoring, bah. If I want scoring I'll go play /Age of Steam/. Dec 7 '06 #16

 P: n/a Chris Dollin wrote: Christopher Benson-Manica wrote: >>ma**********@pobox.com wrote:(WRT "A && B || C"): >>>Before putting the file back in to your repository, why not add theparentheses so that the next time you or someone else reads the file,they can see what that line of code means straight away? Am I the only poster who abhors explicit parentheses in such simplecases? No. (It depends how complex the A/B/C expressions are, though.) Which leads to another guideline; if the expression is complex enough to require parentheses, it is too complex. Break it down into parts that don't. -- Ian Collins. Dec 7 '06 #17

 P: n/a ivan wrote: > 1. in the case of (A && B) || C: if A=0 then the result is C. Just to make explicit what others have said, in this case, B is not evaluated. eg: void eval(int A) { int B, C; int val; B = 0; C = 2; val = A && (B=1) || C; printf(" A = %d\n", A); printf(" A && (B=1) || C evaluates to: %d\n", val); printf(" B = %d\n", B); } When A is non-zero, the expression B=1 is evaluated and the side effect (assignment to B) takes place. When A is 0, the overall expression is evaluated and has the value 1 (not C), but B remains 0. -- Bill Pursell Dec 7 '06 #18

 P: n/a Christopher Benson-Manica wrote: ma**********@pobox.com wrote: (WRT "A && B || C"): >Before putting the file back in to your repository, why not add theparentheses so that the next time you or someone else reads the file,they can see what that line of code means straight away? Am I the only poster who abhors explicit parentheses in such simple cases? No, and I'd take it a step further. In most cases, if the expression is complex enough to require explicit parentheses, then the expression is too complex and should be broken down into it's constituent parts. Every time I see parentheses in such a simple expression, my first thought is: "They must be forcing some non-default grouping", when this isn't the case then the original programmer just wasted some of my time and energy. If we are to assume that the code maintainer cannot be trusted to know or figure out the correct order of evaluation, what argues against such abominations as int foo = bar + (baz*quux); ? Or, horrors, void strcpy( char *dst, char *src ) { while ( *(dst++) = *(src++) ); } At some point the maintainer has to be expected to have a reasonable knowledge of operator precedence so such ugliness can be avoided. 100% agreed. -- Clark S. Cox III cl*******@gmail.com Dec 7 '06 #19

 P: n/a ivan wrote: hi all, if I have: if(A && B || C) which operation gets executed first? If I remeber well should be &&, am I correct? I have two doubts myself: Why do you post under the russian looking name "ivan", when it is clear to everybody that you are from india, and could you please please please tell us why people from India write "I have a doubt" instead of "I have a question"? Dec 7 '06 #20

 P: n/a "christian.bau" ivan wrote: >hi all,if I have:if(A && B || C)which operation gets executed first?If I remeber well should be &&, am I correct? >I have two doubts myself: Why do you post under the russian lookingname "ivan", when it is clear to everybody that you are from india, andcould you please please please tell us why people from India write "Ihave a doubt" instead of "I have a question"? Oh come on, grow up! Why do you post using the name 'Christian', when you exhibit no ability to be accepting and tolerant? -- Chris. Dec 7 '06 #21

 P: n/a christian.bau

 P: n/a christian.bau wrote: ivan wrote: hi all, if I have: if(A && B || C) which operation gets executed first? If I remeber well should be &&, am I correct? I have two doubts myself: Why do you post under the russian looking name "ivan", when it is clear to everybody that you are from india, The OP will have to supply the definitive answer but I can hazard a guess. His first name's probably 'Navi' or beginning with the same. He probably thought it was cool to reverse it and get a Russian sounding name. and could you please please please tell us why people from India write "I have a doubt" instead of "I have a question"? Because, in English as it's taught here the word doubt is synonymous with the word question. Dec 8 '06 #23

 P: n/a Chris McDonald wrote: Why do you post using the name 'Christian', when you exhibit no ability to be accepting and tolerant? Modelling himself on Richard I ? Dec 8 '06 #24

 P: n/a Christopher Benson-Manica wrote: ma**********@pobox.com wrote: (WRT "A && B || C"): >Before putting the file back in to your repository, why not addthe parentheses so that the next time you or someone else readsthe file, they can see what that line of code means straight away? Am I the only poster who abhors explicit parentheses in such simple cases? If we are to assume that the code maintainer cannot be trusted to know or figure out the correct order of evaluation, what argues against such abominations as Because the C rules are weird and wonderful, and totally confusing to anyone used to a rational set of rules. If you need to rely on "precedence" beyond multiplicative, additive, logical (highest to lowest) use explicit parentheses to ensure readability. You will avoid much future confusion, and reduce the number of threads similar to this one. Even algebraic precedence is apparently unknown to the manufacturers of most 4 function calculators. -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. Dec 8 '06 #25

 P: n/a Old Wolf wrote: Chris McDonald wrote: >Why do you post using the name 'Christian', when you exhibit noability to be accepting and tolerant? Modelling himself on Richard I ? The Lionheart? -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. Dec 8 '06 #27

 P: n/a santosh wrote: christian.bau wrote: .... snip ... >>I have two doubts myself: Why do you post under the russian lookingname "ivan", when it is clear to everybody that you are from india, The OP will have to supply the definitive answer but I can hazard a guess. His first name's probably 'Navi' or beginning with the same. He probably thought it was cool to reverse it and get a Russian sounding name. Maybe Ivan is an expatriate Russian, fleeing from Putins poisoners. >and could you please please please tell us why people from Indiawrite "I have a doubt" instead of "I have a question"? Because, in English as it's taught here the word doubt is synonymous with the word question. The usage seems odd to us, but the meaning is clear. More to the point, why do many Indians persist in using silly abbreviations such as u, ur, etc. Did they pick it up from the kewl script kiddies or other low lifes. -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. Dec 8 '06 #28

 P: n/a CBFalconer wrote: Christopher Benson-Manica wrote: Am I the only poster who abhors explicit parentheses in such simple cases? No. Because the C rules are weird and wonderful, and totally confusing to anyone used to a rational set of rules. Many of the unusual precedences are easy to remember when you notice a pattern: "&" is spelled similarly to "&&", so like the latter "&" has a low precedence. Similarly ">" and "<" have low precedence and so do ">>" and "<<". (Obviously the pattern doesn't follow a simple rule -- & is higher than && but << is higher than < -- but that isn't a source of confusion.) I also object to the idea that complex expressions should be avoided. I frequently write complicated variants of if (0 || foo1 && foo2 && foo3 || foo4 && foo5 && foo6 || foo7 && foo8 && foo9) launch(); Often this is the simplest most readable form of the logic. (The leading "0 ||" may seem silly, but gives the expression an easily-read, easily-edited pattern.) James Dow Allen Dec 8 '06 #31

 P: n/a CBFalconer wrote: santosh wrote: christian.bau wrote: and could you please please please tell us why people from India write "I have a doubt" instead of "I have a question"? Because, in English as it's taught here the word doubt is synonymous with the word question. The usage seems odd to us, but the meaning is clear. More to the point, why do many Indians persist in using silly abbreviations such as u, ur, etc. Did they pick it up from the kewl script kiddies or other low lifes. It's the result of the rampant use of cell phones with SMS facility. Since each character costs a certain amount, ridiculous abbreviations become the norm, gradually invading into email, forums and probably even snail mail, God forbid if I know. Cost is also, partly, the reason why the very vast majority use Google Groups to access Usenet. Subscriptions with news providers are not cheap, (especially if you consider that almost no Indian ISP provides Usenet access as a part of their package), and hunting around for public/free news servers is too much of a technical hassle. Dec 8 '06 #32

 P: n/a James Dow Allen wrote: > I also object to the idea that complex expressions should be avoided. I frequently write complicated variants of if (0 || foo1 && foo2 && foo3 || foo4 && foo5 && foo6 || foo7 && foo8 && foo9) launch(); Often this is the simplest most readable form of the logic. (The leading "0 ||" may seem silly, but gives the expression an easily-read, easily-edited pattern.) I'd prefer something like: if( whateverTheConditionIsForLaunch() ) { launch(); } -- Ian Collins. Dec 8 '06 #33

 P: n/a Ian Collins

 P: n/a "santosh"

 P: n/a Richard Bos wrote: Ian Collins >James Dow Allen wrote: >>>I also object to the idea that complex expressions shouldbe avoided. I frequently write complicated variants of if (0 || foo1 && foo2 && foo3 || foo4 && foo5 && foo6 || foo7 && foo8 && foo9) launch();Often this is the simplest most readable form of the logic.(The leading "0 ||" may seem silly, but gives the expressionan easily-read, easily-edited pattern.) I'd prefer something like:if( whateverTheConditionIsForLaunch() ){ launch();} And what would you put in the horribly sTuDlYcApPeD and far too verbosely named whateverTheConditionIsForLaunch()? Possibly something The name would be something that explained the condition. like this: int whateverTheConditionIsForLaunch(void) { return 0 || foo1 && foo2 && foo3 || foo4 && foo5 && foo6 || foo7 && foo8 && foo9; } Not an improvement, IMO. Maybe not in this case, but if the condition was complex enough to justify a comment, then it would. -- Ian Collins. Dec 8 '06 #36

 P: n/a Richard Bos wrote: "santosh"

 P: n/a Richard Bos

 P: n/a santosh wrote: Richard Bos wrote: >>"santosh" >>christian.bau wrote:could you please please please tell us why people from India write "Ihave a doubt" instead of "I have a question"?Because, in English as it's taught here the word doubt is synonymouswith the word question. Then, beg pardon, what's taught there isn't English. I didn't realise that English had been standardised by the ISO. Can you point us to a dictionary that lists doubt as a synonym of question? -- Ian Collins. Dec 8 '06 #39

 P: n/a Ian Collins wrote: santosh wrote: Richard Bos wrote: >"santosh"

 P: n/a santosh wrote: Ian Collins wrote: >>santosh wrote: >>>Richard Bos wrote:"santosh" christian.bau wrote:>>>could you please please please tell us why people from India write "I>>have a doubt" instead of "I have a question"?>>Because, in English as it's taught here the word doubt is synonymous>with the word question.Then, beg pardon, what's taught there isn't English.I didn't realise that English had been standardised by the ISO. Can you point us to a dictionary that lists doubt as a synonym of question? And can you point us to a country that enforces the English vocabulary of it's people with a dictionary? I'm just curious where the usage originated, no offense intended. -- Ian Collins. Dec 8 '06 #41

 P: n/a James Dow Allen wrote: CBFalconer wrote: >Christopher Benson-Manica wrote: >>Am I the only poster who abhors explicit parentheses in such simplecases? No. >Because the C rules are weird and wonderful, and totally confusingto anyone used to a rational set of rules. Many of the unusual precedences are easy to remember when you notice a pattern: "&" is spelled similarly to "&&", so like the latter "&" has a low precedence. Similarly ">" and "<" have low precedence and so do ">>" and "<<". (Obviously the pattern doesn't follow a simple rule -- & is higher than && but << is higher than < -- but that isn't a source of confusion.) I also object to the idea that complex expressions should be avoided. I frequently write complicated variants of if (0 || foo1 && foo2 && foo3 || foo4 && foo5 && foo6 || foo7 && foo8 && foo9) launch(); Often this is the simplest most readable form of the logic. (The leading "0 ||" may seem silly, but gives the expression an easily-read, easily-edited pattern.) I would write that as: if ((foo1 && foo2 && foo3) || (foo4 && foo5 && foo6) || (foo7 && foo8 && foo9)) launch(); leaving very little doubt, and consuming less vertical space. -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. Dec 8 '06 #43

 P: n/a Ian Collins wrote: santosh wrote: >Richard Bos wrote: >>>"santosh" -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. Dec 8 '06 #44

 P: n/a CBFalconer

 P: n/a "santosh" >and could you please please please tell us why people from India >write "I have a doubt" instead of "I have a question"? > Because, in English as it's taught here the word doubt is synonymous with the word question. The usage seems odd to us, but the meaning is clear. More to the point, why do many Indians persist in using silly abbreviations such as u, ur, etc. Did they pick it up from the kewl script kiddies or other low lifes. It's the result of the rampant use of cell phones with SMS facility. That's no excuse. SMS is ridiculously popular here, as well, and SMS-sp3k is used here, too, including in IM and email from school children, but you rarely see anyone use it after they grow up, and certainly not in a serious context. Since each character costs a certain amount, What a weird arrangement. Over here, each SMS costs something, but that amount is nothing to do with the length of the message, and everything with whom you are sending it to. ridiculous abbreviations become the norm, gradually invading into email, forums and probably even snail mail, God forbid if I know. God forbid _that_ you know. (But I suspect most native speakers would get that wrong, as well.) Cost is also, partly, the reason why the very vast majority use Google Groups to access Usenet. Subscriptions with news providers are not cheap, (especially if you consider that almost no Indian ISP provides Usenet access as a part of their package), and hunting around for public/free news servers is too much of a technical hassle. news.individual.net. Spread the word. Richard Dec 8 '06 #46

 P: n/a CBFalconer wrote: Ian Collins wrote: >>"santosh" I question your sanity. The verb 'question' and the verb 'doubt' can in some contexts be synonymous. Unfortunately the nouns never are. Dec 8 '06 #47

 P: n/a "santosh" >>"santosh" > >>>christian.bau wrote: >>> >>>>could you please please please tell us why people from India write "I >>>>have a doubt" instead of "I have a question"? >>> >>>Because, in English as it's taught here the word doubt is synonymous >>>with the word question. >> >>Then, beg pardon, what's taught there isn't English. > I didn't realise that English had been standardised by the ISO. > Can you point us to a dictionary that lists doubt as a synonym of question? And can you point us to a country that enforces the English vocabulary of it's people with a dictionary? No. Basically, by using "doubt" as if it meant "question", you're not breaking any constraints, but you _are_ invoking undefined linguistic behaviour. Don't complain about any resulting conversational bus error. Richard Dec 8 '06 #48

 P: n/a Richard Bos wrote: > "santosh"

 P: n/a In article <45****************@news.xs4all.nl>, Richard Bos Because, in English as it's taught here the word doubt is synonymouswith the word question. >Then, beg pardon, what's taught there isn't English. It's not British English, and I doubt that it's American English, but it may well be Indian English. All part of the Imperial legacy.. -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963. Dec 8 '06 #50

122 Replies

### This discussion thread is closed

Replies have been disabled for this discussion.