• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

if-else

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not able to understand the output of b3?Can anyone plz expain this?

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;
boolean b2 = false?false true?false true?false:true));
boolean b3 = ((false?false:true)?false:true)?false:true;
System.out.println(m1(b1) + m1(b2) + m1(b3));
}}

What is the result of attempting to compile and run the program?

a. Prints: FFF
b. Prints: FFT
c. Prints: FTF
d. Prints: FTT
e. Prints: TFF
f. Prints: TFT
g. Prints: TTF
h. Prints: TTT
i. Run-time error
j. Compile-time error
k. None of the above


answer is b (FFT)
 
kashish verma
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOps!!!

b2 is
boolean b2 = false?false:(true?false:(true?false:true));

I forgot to disable smilies.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


b3 is easy. b1 is the trickiest. Aparently, it is evaluated "from behind", like b2.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
good question
 
kashish verma
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jan,
Can u explain the last line of your reply.How would we calculate b1 from behind?
I understood for b3, but now output of b1 is confusing me.
 
Jan Valenta
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kashish verma:
I understood for b3, but now output of b1 is confusing me.



In b1, the last (right-most) ?: operator is evaluated first, then the middle one and finally the left-most. Therefore the evaluation order and the result is the same as for b2, where this evaluation order is highlighted by brackets.

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

Originally posted by kashish verma:
Hi Jan,
Can u explain the last line of your reply.How would we calculate b1 from behind?
I understood for b3, but now output of b1 is confusing me.



Hi Kashish,
This is how it goes...
evaluation of b1 is straight and simple...moving right-to-left...since conditional operators have right-associativity i.e.. they are evaluated from right to left
b1 = false?false:true?false:true?false:true;
b1 = false?false:true?false:false;
b1 = false?false:false;
b1 = false;

Now coming to b2,

b2 = false?false true?false true?false:true));
again moving from right-to-left,
b2 = false?false true?false:false);
b2 = false?false:false;
b2 = false;

and finally, b3

b3 = ((false?false:true)?false:true)?false:true);
This is a bracketed equation, so brackets take precedence over the precedence order.
so evaluating from innermost bracket,
b3 = ((true?false:true)?false:true)
= (false?false:true)
= true

So, b1 = false, b2 = false and b3 = true

Hence the function m1(false,false,true)...so the answer FFT

Hope this helps
[ December 12, 2005: Message edited by: Sherry Jacob ]
 
kashish verma
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now i got it.
Thanks Jan and Sherry.
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic