3

I have a if else short statement as follows:

(one == two) ? "do this" : "do this"

Is there anyway to add an if else into this statement?

I can't seem to find anything with an if else...

I'm being specific to the short statement, as opposed to do if if else else longhand.

Thanks.

2
  • 7
    What do you mean? Commented Dec 22, 2014 at 14:33
  • Although you can do it (like @CommuSoft stated in his anwer) you should avoid such constructs, as they create code that is harder to understand. For simple checks the abbreviated if is fine, for anything more complex you should use if...else if...else. Memory is not a restricting factor and anyone who will work with your code in the future will be thankful to not have to handle with loooong oneliner if constructions. Commented Oct 1, 2015 at 9:18

6 Answers 6

12

If you want to convert something like:

if(A) {
    return X;
}
else if(B) {
    return Y;
}
else {
    return Z;
}

You can write this as:

A ? X : (B ? Y : Z);

You thus write the else if as a condition in the else-part (after :) of the upper expression.

However, I would strongly advice against too much cascading. The code becomes extremely unreadable and the ? : code structure was never designed for this.

Sign up to request clarification or add additional context in comments.

2 Comments

It's purely for the use of if, if else, else, 3 conditions. I can understand why, for readability, you wouldn't want to be adding too many else if's in! Thanks.
@sark9012: Sure :). The Java compiler has no problem with nesting a lot of code. But it would be very unpleasant for a human to understand hundreds of nested statements. It's not incorrect, but one eventually pays a price if one produces human-unreadable code.
9

You can extend this to any number of clauses, in perfect analogy to the if-else construct.

return a == b? "b"
     : a == c? "c"
     : a == d? "d"
     : "x";

In this form it quite closely resembles Lisp's cond, both in shape and in semantics.

But, do note that this is not a "shorthand for if/else" because it is an expression whereas if/else is a statement. It would be quite bad abuse of the ternary operator if the expressions had any side effects.

1 Comment

Great answer, can go on forever then!
5

The ":" is the else

(one == two) ? "do this" : "do that"

If one equals two then "do this", otherwise (if one not equals two) than "do that".

Comments

1

I sometimes use Maps for such situations:

private final static Map <String, String> codesMap = <generate the map with values>
...
codesMap.get(one)

2 Comments

This is not entirely equivalent since if statements can be complex and have an inherent priority (namely the first condition has priority over the second). Furthermore the lookup of a dictionary is expensive compared with a single condition.
@CommuSoft you could try an orderedMaps for priorities. as for performance - yes, that could be a problem in some applications, not a silver bullet.
1

Yes, Above statement can be written using if-else. Here Ternary operator is used.

if(one==two)
 {
    //Code
 }
else
 {
    //code
 }

Ternary operator reduces Line Of Code(LOC) by writing condition in one statement instead of many using "? :".

For more information please refer:

http://java.meritcampus.com/t/48/Ternary-operator?tc=mm71

http://java.meritcampus.com/t/60/If-else-if-ladder?tc=mm72

Comments

1

This works like an if-else-statement but technically you could convert this into an if-else-statement. That would look like this:

if (one == two) {
"do this"
} else {
"do that"
}

if your question is whether or not you could insert an if statement into (one == two) ? "do this" : "do this" ... no, rather you should use nested if statements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.