Results 1 to 4 of 4
Thread: Shorthand ternary operator ?
- 10-02-2011, 09:46 PM #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.
-
Re: Shorthand ternary operator ?
You'd have to nest ternary operations and it would be one ugly mess.
- 10-02-2011, 09:53 PM #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.
-
Re: Shorthand ternary operator ?
In my opinion, yes. Sure, you could always do
But try to debug it 1 month or even 1 week later, and what then?Java Code:a = x == 1 ? 10 : x == 2 ? 2 * a : 3333;
Similar Threads
-
Java OR operator || help!
By BeardedCamel in forum New To JavaReplies: 2Last Post: 09-19-2011, 08:55 PM -
doubt regarding ternary operator (?)
By subith86 in forum New To JavaReplies: 2Last Post: 03-10-2011, 08:15 AM -
Use of ternary
By wulfgarpro in forum New To JavaReplies: 1Last Post: 09-21-2010, 06:31 AM -
Confused between the usage of printf and println... and ternary operator
By blessed07 in forum New To JavaReplies: 2Last Post: 02-27-2010, 07:10 AM -
Shorthand ?
By gatzke in forum New To JavaReplies: 5Last Post: 07-29-2009, 02:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks