• 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 Operator

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Please check the following code :

class EBH204 {
static boolean m1(String s, boolean b) {
System.out.print(s + (b ? "T" : "F"));
return b;
}
public static void main(String[] args) {
m1("A",m1("B",false) || m1("C",true) || m1("D",false));
}}


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

a. Prints: ATBFCT
b. Prints: ATBFCTDF
c. Prints: BFCTAT
d. Prints: BTCTDFAT
e. Run-time error
f. Compile-time error
g. None of the above

The correct answer is : BFCTAT

doubt : the conditional operator works like
condition ? value1 : value2
if the condition is true then it returns value 1 else value 2
Now, when i pass a boolean as a condition clause then how to go further.

Sorry , it is a very basic question but i am little confused about it.

Thanks
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
don't forget you're using the shortcut operater...

what happens is this...

the first thing that happens is we call m1 with "B" and false. so we print
BF

we return false.

we then call m1 with "C" and true. so we print
CT
we return true.

we now KNOW your or condition is true... we have
false || true || (it really doesn't matter what's here)

so the m1 call with "D" IS NEVER MADE.

we now call m1 with "A" and true, printing AT

hence,
BFCTAT

did that answer your question?
 
Anjali Bahl
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Fred

It's very much clear now.

But another small doubt is :

If the conditional operator was :

(b ? "F" : "T"));

Then how we would have evaluated
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you'd get the same flow, but your output would be different...

the conditional operator (sometimes called the ternary op. since it has 3 arguments) evaluates a boolean expression, then returns one of two choices. This:

b ? "T" : "F"

is equivilent to this:


Note that it doesn't CHANGE any of these three values... it just looks and one and spits out one of the other two...

so if you changed your m1 function to


the first thing that happens is we call m1 with "B" and false.
in m1, we evaluate b (false) so we spit out the second string. this gets used in the print statement, printing BT.

but since m1 returns b, we still return false.

we now call m1 with "C" and true. b is now true, so the ternary returns the first string, or "F". we print CF.

we return b, which is true. again, the shortcut operator kicks in, so we KNOW our || condition is true, so we never call m1 with "D" and false.

our or condition is true, so we can now call m1 with "A" and true, which will print AF.

so, your change will give

BTCFAF

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anjali, please mention the source of your code example.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic