# Shorthand ternary operator ?

Printable View

• 10-02-2011, 09:46 PM
Billywizz
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.
• 10-02-2011, 09:48 PM
Fubarable
Re: Shorthand ternary operator ?
You'd have to nest ternary operations and it would be one ugly mess.
• 10-02-2011, 09:53 PM
Billywizz
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.
• 10-02-2011, 09:57 PM
Fubarable
Re: Shorthand ternary operator ?
In my opinion, yes. Sure, you could always do
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?