1. Member
Join Date
Jan 2011
Posts
57
Rep Power
0

Shorthand ternary operator ?

Hi all,
just wondering if it is possible to write three cases of an if statement as a shorthand ternary operator for example:

if(x==1)
a=10;
else if (x==2)
a=x*2;
else
a=33333;

so far i have:
a = x == 1 ? 10 : 4 ;

this only sets a to either 10 or 4 if x = 1 a = 10 if not a = 4. How do i put the third case in so that a = 4 if and only if x == 2 else a = 3333

All help appreciated.

2. Re: Shorthand ternary operator ?

You'd have to nest ternary operations and it would be one ugly mess.

3. Member
Join Date
Jan 2011
Posts
57
Rep Power
0

Re: Shorthand ternary operator ?

thank you. so it should only really be used for 2 cases if the statement is true do this. else do this.

4. Re: Shorthand ternary operator ?

In my opinion, yes. Sure, you could always do
Java Code:
`a = x == 1 ? 10 : x == 2 ? 2 * a : 3333;`
But try to debug it 1 month or even 1 week later, and what then?

Posting Permissions

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