Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Alternative to if else block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Alternative to if else block" Watch "Alternative to if else block" New topic
Author

Alternative to if else block

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Hi Guys,

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



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Sep 08, 2007
Posts: 3064
    
  33

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.


Mohamed Sanaulla | My Blog
Zeeshan Sheikh
Ranch Hand

Joined: Nov 20, 2011
Posts: 144

I think you posted similar question in another post. Atleast try it yourself !!!


MySQL Blog
http://mysqlearner.blogspot.com/
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

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

Joined: Jan 28, 2008
Posts: 5575

introduce one more return statement in end of the if block, so that you can remove else {}

something like this:
Gijs van Wieringen
Greenhorn

Joined: May 22, 2005
Posts: 10
[edit] Oops, didn't read well. Removed wrong code


Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

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

Joined: May 22, 2005
Posts: 10
Sorry, my example was wrong
Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

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

Joined: May 22, 2005
Posts: 10
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

Joined: Apr 22, 2011
Posts: 202

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

Joined: Oct 13, 2005
Posts: 37953
    
  22
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

Joined: Apr 22, 2011
Posts: 202

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 17, 2011
Posts: 7526
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Apr 22, 2011
Posts: 202

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

Joined: Mar 17, 2011
Posts: 7526
    
  18

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Alternative to if else block
 
Similar Threads
Expected output is incorrect!
a doubt....
doubt in " =="
Question regarding "if"
CheckBoxRenderer