Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short java if else if else statement

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.

like image 301
sark9012 Avatar asked Mar 30 '26 00:03

sark9012


1 Answers

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.

like image 139
Willem Van Onsem Avatar answered Apr 02 '26 12:04

Willem Van Onsem