Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Conditional Operators (try this)

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What is the output?
Cheers
-Suresh
 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi suresh,
i think the ans is true, true, true.
Vineela
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Step by step
boolean x = a = false || (b = true) && (c = true);
1. b and c becomes true
boolean x = a = false || (true) && (true);
2. (true) && (true) is true
boolean x = a = false || true;

3. false || true is true
value of a is set true
value of x is set to true
So the answer is indeed true, true, true
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas explanation is clear, however I want to add one more thing...
if boolean x = a=true || (b = true) && (c = true);
then "(b = true) && (c = true)" is never evaluated thus outputting.
true false false.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vicken
Your point is correct but when u actually excute the code the ans is true,true,true!
And now i am confused!
Shilpi
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Evaluating
1) boolean x = a=true || (b = true) && (c = true);
will result: true, false, false
2) boolean x = a=false || (b = true) && (c = true);
will result true, true, true

I think you simply misread the question, am I right?
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The concept is:
The && operator has precedence over the || operator.
Lets apply this concept to your expression:
boolean x =a=false||(b = true) && (c = true)-------[1]
Step 1
&& is evaluated before || giving,
(b=true)&&(c=true)
The Result being true
Both sides of the above expression will be evaluted giving,
b=true and c=true
Step 2
next, || is evaluated giving,
a=false||true
The result being true
Now try interchanging the || and && in the expression [1] above,
The output will be,
true false true
The concept being the same.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vicken:
"
if boolean x = a=true || (b = true) && (c = true);
then "(b = true) && (c = true)" is never evaluated thus outputting.
true false false."

this is wrong... && has higher precedence over ||, so the assg to b and c
are made because of this and the answer is true true true
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Why the output of the below one is - true, false, false.
-----
class ConditionalCheck {
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);}}
---------
If && has higher precedence than ||, why (b=true) && (c=true) is not evaluated.
I am bit confused, please explain.

Narasimha.
 
Dan Andrei
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first I want to apologize to Vicken for my prevoius post,
HE WAS RIGHT AND i WAS WRONG
I think the result is true false false, because of the rules of evaluation of && and || ops.
Now IF the left hand side of the || op is true then the rest is not evaluated regardless id we have && (higher order evaluation op)
In the example x=a=false||(b=true)&&(c=true)
BECAUSE the left hand side was FALSE we have to evaluate the rigth hand side
and here because && had higher precedence it was evaluated first
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dan Andrei:
first I want to apologize to Vicken for my prevoius post,
HE WAS RIGHT AND i WAS WRONG
I think the result is true false false, because of the rules of evaluation of && and || ops.
Now IF the left hand side of the || op is true then the rest is not evaluated regardless id we have && (higher order evaluation op)
In the example x=a=false||(b=true)&&(c=true)
BECAUSE the left hand side was FALSE we have to evaluate the rigth hand side
and here because && had higher precedence it was evaluated first


I couldn�t explained it better... Way to go.
 
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
boolean x = a=true || (b = true) && (c = true);
System.out.println(a + "," + b + "," + c);
boolean x = a=true && (b = true) || (c = true);
System.out.println(a + "," + b + "," + c);
Hello all,
I am preparing for the SCJP exam. I am confused with the concept discussed here. Looks like according to the question posted, precedence doesn't make any difference. It is true, if the two operators have the same level of precedence, it is evaluated from left to right. But here the && operator has higher precedence over the || operator, so the expression should be evaluated in a hierarchical order.Which means the && operator should be evaluated first and then the || operator. But it seems like in both the situations it is evaluated from right to left. So could you please explain what is happening here.
Thank you very much.....
 
reply
    Bookmark Topic Watch Topic
  • New Topic