• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Conditional if

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

class EBH023 {
static String m1(boolean b){
return b?"T":"F";
}
public static void main(String [] args) {
boolean b1 = false?false:true?false:true?false:true;
System.out.prinln(b1);
}
}
[/CODE]


When I work on that b1

B1 = false?false:true?false:true?false:true;

b1 = true ?false:true?false:true;
b1 = false ?false:true;
b1 = true;

but this b1 output is false ?How?

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


I would think you go from right to left:

true?false:true = true
true?false:true = true
false?false:true = false

Maybe you can see it this way:

(false?false true?false true?false:true)));


m1 returns "F"

Not sure...I am still learning.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henrik,thanks.You are right.I also figured it.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shiva Mohan:

class EBH023 {
static String m1(boolean b){
return b?"T":"F";
}
public static void main(String [] args) {
boolean b1 = false?false:true?false:true?false:true;
System.out.prinln(b1);
}
}
[/CODE]


When I work on that b1

B1 = false?false:true?false:true?false:true;

b1 = true ?false:true?false:true; //THIS STATEMENT WILL MAKE B1
FALSE
BECAUSE CONDITION IS TRUE

b1 = false ?false:true;
b1 = true;

but this b1 output is false ?How?

Please help me.

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The terinary operator behaves similar to && and ||. These operators does not evaluate the second expression if they are satisfied with the first expression itself. For example if the first expression is false in an && operator, the second expression is not evaluated, because it expects both to be true. In your expression for the second time when it sees a true?false:........., it takes only false and not evaluate the second part.

Thanks
Chandu
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does the associativity of the ?: operator factor in to the evaluation of the expression?
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic