# Question regarding nested ifs and conditional operator in Java

This topic is 5178 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys. I hope I am posting this in the right forum. I have a general question regarding the conditional operator in Java. I have the following if statement as an example:
if (a>b){
m=a;
}else if (c<d){
m=c;
}else if (b>d){
m=d;
}else{
m=b;
}

Is there a way I can do this using a single shorthand nested if with the conditional (?) operator? I have only seen the shortcut notation used in single if/else statements before and was just curious if it could be done with nested if's. I tried experimenting with it but I cant seem to get it to work. I appreciate any help you can give me. Thanks so much!!!

##### Share on other sites
See if (a>b)?a:(c<d)?b:(b>d)?d:b works.

##### Share on other sites
Thanks for the reply Fruny! I tested it out and it works perfectly! Now that I look at how its done, I see that I was trying to make it a lot more complicated than it needed to be. Thanks again for the help!!!

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633329
• Total Posts
3011382
• ### Who's Online (See full list)

There are no registered users currently online

×