# Thread: Ternary Operator (?:) Questions

1. Member
Join Date
Dec 2013
Posts
4
Rep Power
0

## Ternary Operator (?:) Questions

I had quite a hard time figuring this out my self (it came from the book and I am trying to analyze it) but... pff <_< no hope. Been trying to figure it out for an hour and yet I still couldn't get it.

So here is the question:

- How to convert this if/else statement using the ternary (?:) operator?
Java Code:
double value = 8.9;
if (value > 10.6)
System.out.println("x is greater than 10. \n");
else
System.out.println("x is less than or equal to 10. \n");

If you do know it, please tell me what you did and how did you done it (line by line) if you may so that I can understand it properly.

Last edited by Mind Blown; 12-11-2013 at 04:53 PM.

2. ## Re: Ternary Operator (?:) Questions

As it is only one line it's hard to tell line by line:
Java Code:
System.out.println(value > 10.6?"x is greater than 10. \n":"x is less than or equal to 10. \n");

3. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
10

## Re: Ternary Operator (?:) Questions

I think the intended result is that those four lines of the if/else statements turn into 1 line that does a single System.out, so there is no "line by line" to speak of.

Okay lets say I give you a simple example and not solve the riddle for you directly.

Java Code:
boolean truthOrDare = true;

String text = truthOrDare ? "truth" : "dare";
Can you then apply this to your problem?

EDIT: ninja'd by the spoonfeeding PhHein! Tsk tsk.

4. Member
Join Date
Dec 2013
Posts
4
Rep Power
0

## Re: Ternary Operator (?:) Questions

Originally Posted by PhHein
As it is only one line it's hard to tell line by line:
Java Code:
System.out.println(value > 10.6?"x is greater than 10. \n":"x is less than or equal to 10. \n");
Heh, my bad I've no idea actually.

Now I finally understand it.
Thanks man.

5. ## Re: Ternary Operator (?:) Questions

Originally Posted by gimbal2
EDIT: ninja'd by the spoonfeeding PhHein! Tsk tsk.
Yeah, sorry, mate. I couldn't think of a non spoonfeeding way. I'm not as creative as you :)

Phil

6. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
10

## Re: Ternary Operator (?:) Questions

Not sure if serious or poking fun at me in return... I hope the latter.

7. ## Re: Ternary Operator (?:) Questions

Originally Posted by gimbal2
Not sure if serious or poking fun at me in return... I hope the latter.
You could do this:

Java Code:
System.out.println("x is "+(value > 10.6?"great than":"less than or equal to")+" "+(42-32));
kindest regards,

Jos

8. ## Re: Ternary Operator (?:) Questions

Originally Posted by gimbal2
Not sure if serious or poking fun at me in return... I hope the latter.
The latter, of course.

9. ## Re: Ternary Operator (?:) Questions

Originally Posted by PhHein
The latter, of course.
<high squeaky voice>
You're lying old woman! Now confess! It was the first! Confess!
</high squeaky voice>

kindest regards,

The Spanish Inquisition

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•