Forums Register Login

Alternative to if else block

+Pie Number of slices to send: Send
Hi Guys,

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



+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
I think you posted similar question in another post. Atleast try it yourself !!!
+Pie Number of slices to send: Send
 

Zeeshan Sheikh wrote:I think you posted similar question in another post. Atleast try it yourself !!!


Yes, similar question but different code.
+Pie Number of slices to send: Send
introduce one more return statement in end of the if block, so that you can remove else {}

something like this:
+Pie Number of slices to send: Send
[edit] Oops, didn't read well. Removed wrong code


+Pie Number of slices to send: Send
 

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..!!
+Pie Number of slices to send: Send
Sorry, my example was wrong
+Pie Number of slices to send: Send
 

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..!!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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...!!
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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...

+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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..

+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
Then YOU must do the pig's work! Read this tiny ad. READ IT!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 5345 times.
Similar Threads
doubt in " =="
Expected output is incorrect!
CheckBoxRenderer
Question regarding "if"
a doubt....
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:11:35.