Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ternary operator

 
rachel biji
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code that I wrote

i%2==0?(System.out.println("even"):System.out.println("odd");

The compilation error being "Not a statement". Cant understand whats wrong.I also tried

(i%2==0)?(System.out.println("even");) System.out.println("odd");) ;
even though it didnt look right

Can You please give me an explanation?



[edit]Disable smilies CR[/edit]
[ August 21, 2008: Message edited by: Campbell Ritchie ]
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

take this example
---------------------------------
String value(){
return 5<3?"true":"false";
}
------------------------------------
meaning of ternary operator:

(condition)?if success:if failure

if you have any confusion work on this in some IDE.

Hope This Helps
 
Katrina Owen
Sheriff
Pie
Posts: 1367
18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have seen the ternary operator used in two ways. In both, it is evaluated to a value (not to an executable statement).

1) Assignment



2) Returning a value (as in the previous post)



I have never seen it used to execute a statement, like you are attempting.

So for your example:

 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More details in this other thread.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic