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.
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..!!
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
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...!!
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 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...
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.
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?
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.
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..
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.
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 by:autobot
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