Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ternary statement

 
mark crannage
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can ternary operators be used instead of simple if else statements ?

eg. (i>5)?doSomething() : DoSomethingElse();

Ive tried coding similar to this but with no success. Can terenary operators only be used to assign values ?

eg. booleanVar = (j>5)?true:false;

is there any other way to do the same logic as a simple if else statement ?

thanks,

mc

(disabled smily)
[ July 26, 2006: Message edited by: Barry Gaunt ]
 
mark crannage
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ha, i dont think any compiler will compile the smiley face, replace that with :

thanks,

Mark
 
Rishi Kanth
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey You can use the teneray operators instead of a If-else statement

Condition?true:false;

Rishi
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't use the ternary operator like that.

This is from the Java Language Specification.

Note that it is a compile-time error for either the second or the third operand expression to be an invocation of a void method. In fact, it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear (�14.8).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic