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); }}
What will be the output of the program: the answer is given as true,false, false.
but before applying ||,&& the assignment expressions in () shud be executed. what do ya say? then the output should be true, true, true. But, i executed the program it displays true, false, false.
can someone give me the logical explanation for this?
Sriram.
Darya Akbari
Ranch Hand
Joined: Aug 21, 2004
Posts: 1855
posted
0
Hi Sriram,
your name 'IGNORAMUS' does not confirm with the name policy here at JavaRanch.
Your boolean a, b and c are all false by default. Since you use the short circuit logical operator || only the left side is evaluated. So a = true, b and c remain false. Whenever the left side is true the right will not be evaluated. That is the speciality of the short circuit logical operator ||.
Follow the link give below, the same question has been dicsussed before here
sriRam gaddaM
Greenhorn
Joined: Dec 17, 2004
Posts: 7
posted
0
Hi,
i have got a small explanation for it. can u guys comment on it. &&, ||, ?: are always evaluated from left to right irrespective of parentheses. but if you take the case of (a=2) * (b=3) * (c=4) a,b,c will have 2,3,4. ain't it?
But, i got one more doutb here. && has more precedence than || so, it shud be evaluated first before ||. But, it is not happening here?
P.S: Darya, Didn't get when u said my Uname doesn't conform with the naming policy. can u throw some light on that? (haven't read the policy in detail... ))
Sriram.
Animesh Shrivastava
Ranch Hand
Joined: Jul 19, 2004
Posts: 298
posted
0
&& and || are Short Circuit operators. Did u go thru the link i mentioned in my previous reply. Its clearly mentioned abt the usage of these Short Circuit operators.
Darya Akbari
Ranch Hand
Joined: Aug 21, 2004
Posts: 1855
posted
0
Hi Sriram,
there is somewhere a link on JavaRanch about name policy let me look for it...
i think my name is fictious but doesn't fall into any of the categories mentioned in the policy nor is offending. moreover, i specify my name whenever i make a post. wud request the forum mods to let me use it.
i think my name is fictious but doesn't fall into any of the categories mentioned in the policy nor is offending. moreover, i specify my name whenever i make a post. wud request the forum mods to let me use it.
Sriram.
Guess again. We require display names to be two words: your first name, a space, then your last name. Fictitious names are not allowed. You can edit your display name here. Don't take too long - accounts with invalid display names get deleted.
thanks, Dave. Moderator.
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
wud request the forum mods to let me use it.
I don't know who wud is, but nope you cannot use that displayed name.