• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alternative to if else block

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Please advise me how to write the below piece of code ...in form of ternary conditional operator..!!



 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added the code tags to make your post more readable. (You know how to do that by now, don't you?)

Anyway I don't think it can be done -- although the code you posted won't even compile, I don't think.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:Hi Guys,

Please advise me how to write the below piece of code ...in form of ternary conditional operator..!!


I would suggest you to read about the Ternary operator here to find out what expressions can be used in the Ternary operator.

Is there a requirement to use ternary condition or you are looking to use that so as to make the code concise? If its the latter then the code would not be obvious when Ternary operator is used with complex conditions/expressions.
 
Zeeshan Sheikh
Ranch Hand
Posts: 144
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you posted similar question in another post. Atleast try it yourself !!!
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zeeshan Sheikh wrote:I think you posted similar question in another post. Atleast try it yourself !!!

Yes, similar question but different code.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
introduce one more return statement in end of the if block, so that you can remove else {}

something like this:
 
Gijs van Wieringen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edit] Oops, didn't read well. Removed wrong code


 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gijs van Wieringen wrote:


Thanks a lot... I have one query..in my piece of code....



the initial value of allow was false ..and if condition was not satisfied it remains as false ..and is returned ...would that logic is still maintained in this piece of code..please explain..!!
 
Gijs van Wieringen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, my example was wrong
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gijs van Wieringen wrote:Sorry, my example was wrong


Hi Gijs,

Just chill..it happens..!! could you please correct it..so that I can grasp ...the right one..!!
 
Gijs van Wieringen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm can't think of a useful way to implement the ternary conditional operator in this case atm. The code I posted earlier is nonsense. That's my punishment for writing comments with a hangover. Please remove my code from your reply haha
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gijs van Wieringen wrote:Hmm can't think of a useful way to implement the ternary conditional operator in this case atm. The code I posted earlier is nonsense. That's my punishment for writing comments with a hangover. Please remove my code from your reply haha



Yeah but I am still looking the solution to this...!!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49812
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote: . . . Thanks a lot... I have one query..in my piece of code.... . . . the initial value of allow was false ..and if condition was not satisfied it remains as false ..and is returned ...would that logic is still maintained in this piece of code..please explain..!!
Don’t know. It depends whether appData.remove(IAppDataWag.sf_REWARDS_ENABLED returns a boolean. If it does, then it might return true. If it doesn’t, then chances are the code won’t compile.
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Saral Saxena wrote: . . . Thanks a lot... I have one query..in my piece of code.... . . . the initial value of allow was false ..and if condition was not satisfied it remains as false ..and is returned ...would that logic is still maintained in this piece of code..please explain..!!
Don’t know. It depends whether appData.remove(IAppDataWag.sf_REWARDS_ENABLED returns a boolean. If it does, then it might return true. If it doesn’t, then chances are the code won’t compile.


HI,

IT has nothing to do with... appData.remove..that is simply we are setting the value...I am just searching the alternative in terms of ternary condition...

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:I am just searching the alternative in terms of ternary condition...


It. Can't. Be. Done.

Contrary to what your subject line suggests you believe, the ternary operator is not a simple one-for-one substitute for an if/else statement. There are times when we can replace if/else with the ternary operator, but this is not one of them. And even if it was, it would not be a good idea. The ternary operator causes enough confusion as it is. Using in complex cases where it's not immediately obvious what it's doing is just asking for debugging and maintenance headaches.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please BeForthrightWhenCrossPostingToOtherSites(⇐click) so people don't waste their time duplicating each others' answers.

https://forums.oracle.com/forums/thread.jspa?threadID=2364445&tstart=0
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:It. Can't. Be. Done.

Actually, it can; but only if Sarel wants to create two methods that contain precisely the same code he has already written, AND return the correct value.

@Sarel: a pretty pointless exercise.
My question is: Why do you want to know?

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Jeff Verdegan wrote:It. Can't. Be. Done.

Actually, it can; but only if Sarel wants to create two methods that contain precisely the same code he has already written, AND return the correct value.


Yeah, yeah. I stand corrected.

So let me rephrase:

It. Shouldn't. Be. Done. And I'm certainly not going to help someone to perpetuate such an atrocity.
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Winston Gutkowski wrote:
Jeff Verdegan wrote:It. Can't. Be. Done.

Actually, it can; but only if Sarel wants to create two methods that contain precisely the same code he has already written, AND return the correct value.


Yeah, yeah. I stand corrected.

So let me rephrase:

It. Shouldn't. Be. Done. And I'm certainly not going to help someone to perpetuate such an atrocity.


Hi Folks,

So you mean to say ..that below code can never be expressed through ternary operator condition..

 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:So you mean to say ..that below code can never be expressed through ternary operator condition..

No, Jeff said exactly what he meant: it should never be expressed with a ternary operator.

The whole point of the ternary is operator is that it's concise and involves two (usually value-based) expressions. Your code doesn't qualify on either count.

Winston
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason why you cannot replace your if / else construction with the ternary operator has to do with the difference between statements and expressions.

A statement is just a command to do something; for example, a method call to a void method. It doesn't have a result value.
An expression is something that can be evaluated and that results in a value.

In Java, if / else is a statement and the ternary operator is an expression. That means when you use the ternary operator, it must always result in a value. In fact, the ternary operator is built up out of three expressions:

result = boolean-expression ? expression-if-true : expression-if-false;

The boolean-expression must be an expression that results in a boolean value (true or false). The two expressions expression-if-true and expression-if-false must have the same type. That means, for example, if expression-if-true results in an int, then expression-if-false must also result in an int.

If we look at your if / else statement:

we see that what is in the block for the if (lines 4 and 5) and also for the else (line 7) are not expressions, but statements. Statements are not expressions, so you cannot put these statements in a ternary operator expression.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic