Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

short circuit operators

 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which operator's precedence is more between && and ||.
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
neithers precedence is more i think.
I believe the expression is read from left to right and parantheses are taken into consideration.
 
Mathangi Shankar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
&& has higher precedence than || operator.

In the bitwise operators folloing is the sequence of precedence of operators in descending order

&,^,|

Thanks,

Mathangi
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Details can be refered to JLS Chapter 15.7 Evaluation Order .

Nick
 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok Mathangi,
I was also thinking the same but after solving this question I got a bit confused

class EBH202 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}}

ans is true,false,false
 
udhaya kanagaraj
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai Soni Prasad,
Before answering ur question I will give the order of precedence.
|,&,||,&&.This will be the order of precedence for the operators.
Answering for the code given by u,
the shortcircuit_OR operator won't check for the second value if the first value itself is true.ie,if the first value is false then only it will
check for the second value.so in ur code since a is assinged to true,it won't check b and c.while trying to print b and c it will print the default
value faulse.If ur code is like this
x=((a=true)||(b=true))&&(c=true)
then the output will be
a=true
b=false
c=true
o.k.
regards,
udhaya
 
Mathangi Shankar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prasad,

In evaluation of boolean expressions involving conditional AND and OR, the left-hand operand is evaluated before the right one, and the evaluation is short-circuited.

I hope now it is clear.

Thanks,

Mathangi.
 
udhaya kanagaraj
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai mathangi,
sorry.I think u r wrong .b'cos shortcircut OR will be given more precedence than shortcircut AND.
regards
udhaya
 
challa
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

In Khalid Mughal book it is given that && has more precedence than || operator. However when iam executing ur code iam getting true,false,false
even if iam changing the code as

boolean x = (a = true) || ((b = true) && (c = true)); iam getting the same result. In this case right hand operator should be evaluated first,then b becomes true and then || is evaluated. How come iam getting the same result.can any one please explain.

regards
purnima.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i think the code executes from Left to right as written in K&B

the answer you is getting is just because || is first written on lefthand side ....and the rest is not evaluating...

if && is written first then 2nd oprand will be evaluated....

check examples in K&B book

u all will be clear

:-)
 
M Rama
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so what have we concluded.

|| and && would be left to right , whichever comes first.

while bitwise & has a higher priority over |.


How about some more examples:



results would be:

true
1. i1=2 j1= 2
true
2. i1=1 j1= 1
---------
true
3. i1=3 j1= 3
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume a,b and c are boolean expressions.
&& has higher precedence than || so (a || b && c) becomes ( a || (b && c)).

This has nothing to do with the order in which the expression is evaluated, which is left to right. So a is evaluated first and if it evaluates to false, b && c is evaluated.

If && had lower precednce the expression would become ((a || b) && c).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic