conditional statement with expression language that is not ternary - el

I need to make an expression language conditional statement with a few conditions to check. Googling I can only find examples using ternary
#{SomeBean.someProperty ? 'bob' : 'John'}
I need to have more conditions though. I need something like:
If (SomeBean.someProperty == 'a'){
//Ant
}
Else if (SomeBean.someProperty == 'b'){
//Bob
}
Else if (SomeBean.someProperty == 'c'){
//C++
}
Else{
//Back to the drawing board, something went wrong.
}
How can I write this in expression language?

Just the same syntax as in plain Java.
#{bean.property eq 'a' ? 'Ant' : bean.property eq 'b' ? 'Bob' : bean.property eq 'c' ? 'C++' : null}
Do note that property is assumed to be String or enum and not char because a char is interpreted the same way as numbers in EL. See also How to compare a char property in EL.

Related

Swift String format vs Objective-C

I am using swift String(format:...) and need to compute values in the format string itself using ternary operator, something like this but it doesn't compiles.
String(format: "Audio: \(numChannels>1?"Stereo": "Mono")")
In Objective-C, I could do like this:
[NSString stringWithFormat:#"Audio: %#", numChannels > 1 ? #"Stereo" : #"Mono"];
How do I achieve the same elegance in Swift without having an intermediate variable?
Due to the missing spaces around the operators in the conditional expression, the compiler misinterprets 1?"Stereo" as optional chaining. It should be
String(format: "Audio: \(numChannels>1 ? "Stereo" : "Mono")")
instead. However, since the format string has no placeholders at all, this is equivalent to
"Audio: \(numChannels > 1 ? "Stereo" : "Mono")"
One option is to use String(format:) with a placeholder and the conditional expression as the parameter for the placeholder
String(format: "Audio = %#", numChannels > 1 ? "Stereo" : "Mono")

What is Visual Basic's equivalant to Java's "!"?

I would like to know Visual Basic's equivalant to Java's "!"
Example of how it would work in Java:
If !code.DoesExist {
log.info("This code doesn't exist!");
}
Else {
log.info("This code does exist");
}
I hope you understand what I mean.
In my code I need to do the following:
If imgUrl.Contains("imgur") Then
ImagesFound += 1
Select Case ImagesFound
Case 1
imgBox.ImageLocation = imgUrl
End Select
ElseIf !imgUrl.Contains("") Then
'<some code here>
End If
I need it at the 7th line.
Note: I can't just use "Else" I need to specifically point out that if an image with imgur in the HTML source wasn't found, then this and that should happen.
As per suggestion, here is more elaborated answer :
Not Keyword : Performs logical negation on a Boolean expression, or bitwise negation on a numeric expression.
For more info : https://msdn.microsoft.com/en-us/library/2cwcswt4.aspx
<> : Checks if the values of two operands are equal or not; if values are not equal, then condition becomes true.
For more info : http://www.tutorialspoint.com/vb.net/vb.net_operators.htm

Ternary operator error - "Expected expression"

I haven't used the ternary operator much and I'm getting an error on this:
(isInitializing) ? (return YES) : (isInitializing = 1);
Error is: "Expected Expression" and it's pointing to return YES.
Don't use the Ternary Operator to "do stuff" but to return one of two values.
So this is a valid case:
NSString *something = (isInitializing ? #"value a" : #"value b");
In your case, you might want to do this instead:
if (isInitializing) {
return YES;
} else {
isInitializing = 1;
}
The ternary operator is used to return one of two values depending on a condition. It is not so much used to execute statements, hence the return is a bit of a problem. I would rather use an if when you do not want to distinguish values, but rather have two different execution paths.

What is the equivalent of else do nothing using the conditional operator?

I want to know what is the equivalent of this if statement:
if (condition) {
// do something
}else{
// do nothing
}
Using the conditional operator:
(condition) ? // [do nothing] : {do nothing} "
It's not possible to "do nothing" using the conditional operator. You always have to have valid expressions on both sides, although both expressions can be casted to void.
This is one of the dis-advantage of ternary operator ( ?: ).
It needs expressions in all the three places. You cant skip any of them.
You can do some tweak on it, however its True-part and / or False-part can be assigned to the same as :
int big=100;
big= (10 > 100) ? 0 : big;
if (!condition) {
// do something
}else{
// do nothing
}
Take a look at the !before contition now :-)
The ! before the condition just switches the "if", to "if not"...
Is that what you have been searching for?
originalValue = (condition) ? newValue : originalValue
The compiler should then remove the unnecessary assign of originalValue to itself.
GCC had an extension to do give you something more to what you where looking for, something like
originalValue = condition ?: newValue;
So it may be available in clang also. You would have to ! the condition though.
you could do something like this
someBool ? [self someFunction] : (^{})(); //empty block
In normal code the answer is there is no equivalent - all sub expressions of an expression must have a value, but...
the following is not a recommendation
Something along the lines of the following should work in the general case:
condition ? ( (^{ do something })(), 0 ) : 0;
That is for the general case. If do something is a single, non-compound, statement; such as a method call; then the block can be dropped to give:
condition ? (do something, 0) : 0;
Again this is NOT recommended in real code!

Boolean ? : operation syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the question mark and the colon (?: ternary operator) mean in objective-c?
I have seen code where it uses a syntax something like...
someValue = someBoolean ? valueOne : valueTwo;
Or something like this.
I've never used this and I'm not sure what it's called.
Please can someone explain how to use it or provide a link to a resource about it.
It's ternary opertaor.
It evaluates the someBoolean condition.
If it is true then pass the valueOne to someValue
If it is false then pass valueTwo to someValue
It is equal to:
if(someBoolean)
{
someValue = valueOne;
}
else
{
someValue = valueTwo;
}
This is a good link which explains about ternary operator
This is called ternary operator ( ?: )
1 ? 2 : 3
1 is the condition.
2 is executed when 1 it is true.
3 is executed when 1 is false.
Similar to: (Below is not a running code, 1,2,3 shows only placeholders for some expressions and statements.
if(1){ //condition
2 //true
}
else{
3 //false
}
You can shorten it as for :
int bigger;
(10<100) ? bigger=100 : bigger=10;
in short way:
int bigger = (10<100) ? 100 : 10 ;
NOTE:
Its precedence order is among the least and it is much slower then if-else and switch case statements.
It is a ternary operator (also known as the conditional operator). You can find explanation at this link.
Basically your expression is saying that if someBoolean is true someValue will get valueOne if not it will get valueTwo.
It is similar to:
if(someBoolean)
{
someValue = valueOne;
}
else
{
someValue = valueTwo;
}
which offers less visibility in your code. I recommend using this operator in case you want to assign a value which depends on one condition.
Note that it is an expression not specific to Objective-C, you can use it in C and C++ too.
The result of the assignment is valueOne is the condition is true, and valueTwo if the condition is false.
See it here on wikipedia. It also makes the case with other languages, just skip them and see the C syntax example.
Suppose user needs to answer some question and you change background color of your view to red if he was wrong, green if he was correct.
- (void)handleAnswer:(BOOL)correct {
UIColor *color = (correct) ? [UIColor greenColor] : [UIColor redColor];
self.view.backgroundColor = color;
}
It works same as the following
if (someBoolean)
{
someValue = valueOne;
}
else
{
someValue = valueTwo;
}