This week's book giveaway is in the Other Open Source APIs forum. We're giving away four copies of Storm Applied and have Sean Allen, Peter Pathirana & Matthew Jankowski on-line! See this thread for details.
Hi, Please go through the below piece of code, 1. boolean a,b,c; 2. a=b=c=false; 3. boolean x = (a=true) || (b=true) && (c=true); After this statement values of the different boolean variables are, a - true b - false c - false x - true Now upto this fine. My doubt is, && having higher precedence than ||, hence first && should be evaluated before ||. That means, (b=true) && (c=true) should have been evaluated and values of b and c should have been true. Even if i use () for (b=true) && (c=true), still i am getting the same output. That is, boolean x = (a=true) || ( (b=true) && (c=true) ); This statement is also giving the same output. IS it something like this, if in the equation it finds || operator and the left hand side is true, then it will ignore all the right hand sides of || operator.
Hi, When the boolean || operator is encountered the control doesn't deal with the rest of the statement if its TRUE. In this case as the first part of the statement is TRUE so the control will not go beyond the || operator.Hence you see those results. Now please try this: boolean x = (b=true) && (c=true) || (a=true); and you will get a result as, a - false b - true c - true x - true Hope that helps, Preethi
Courage does not always roar. Sometimes, it is the quiet voice at the end of the day saying, "I will try again tomorrow".
Narasimha Rao B.
Joined: Aug 26, 2002
Preethi, Do you mean here precedence rules will not be applicable. If you take the below example, int x = 2 + 3 * 4 - In this equation always 3*4 will be evaluated first and then on that result + 2 will be applied, becuase * is having higher precedence than +. But if take && , || --- precedence rules will not be applicable (and also even if you have more braces) - Always evaluation starts from left side, based on the operators and operands equation will be evaluated partly or fully.. Is my understanding is correct... Thanks
Joined: Feb 20, 2004
Hi, You really made me put on my thinking caps............ After going through the following sets of examlpes I really don't know as to why the precedence rule with the && and || operator is not followed Eg 1) boolean a,b,c; a=b=c=false; boolean x = (a=true) || (b=true) && (c=true); After this statement values of the different boolean variables are, a - true b - false c - false x - true Eg 2 -- boolean a,b,c; a=b=c=false; boolean x = (b=true) && (c=true) || (a=true); After this statement values of the different boolean variables are, a - false b - true c - true x - true && HAS MORE PRECEDENCE THAN THE || OPERATOR THEN WHY IS IT THAT THE PRECEDENCE RULE IS NOT FOLLOWED IN THIS CASE? Perhaphs the compiler has been built that way.I guess someone can help us in answering this question ...Experts do fill us in. Thanks in advance for the help. Preethi
Narasimha Rao B. & Preethi S, I just noticed that your publicly dislpayed names don't follow our general rule for names. The key part is that we do allow first initials, but we ask for full last names. I know it seems like a nitpicky thing, but I would like to ask you to modify your publicly displayed names. You can read the Javaranch Naming Policy here and change your publicly displayed name here. To Narasimha, I realize that you've been around the ranch for some time now and I'm surprised no one has asked you about this before. If there are some other extenuating circumstances that I don't know about, please let me know. To Preethi, I welcome you to the Ranch and I hope you enjoy your stay. Corey
Hi Corey, My full name is Bandlamudi Narasimha Rao. Bandlamudi - Is my family name ( Common to all my family members) Narasimha Rao - Is my name. (It is my actual name) So every one call me as - Narasimha, Narasimha Rao. In all the applications i will write as, B. NARASIMHA RAO or Narasimha Rao B.
I hope it clarifies your doubt.
Joined: Dec 20, 2001
Originally posted by Narasimha Rao B.: I hope it clarifies your doubt.
That's fine - our goal is really to give JavaRanch a "professional" feel. Having people posting with handles such as "javakiller" or "juicyjuice696" takes away from that - I would have to say that your name is close enough (and certainly been in use long enough) to be considered to pass that rule. Thanks.
Joined: Feb 20, 2004
Hi, The question still remains unanswered............help needed.. Thanks, Preethi
Think of it this way: precedence determines how tightly operators bind to their operands, which is not the same as the order in which terms are evaluated. Given the precedence rules, (a=true) || (b=true) && (c=true); is the same as (a=true) || ((b=true) && (c=true)); but once the grouping is determined, the evaluation still proceeds left to right. So the short circuit || prevents further evaluation. --- Jerry [ March 18, 2004: Message edited by: JL Henderson ]