Iodyne Posted October 18, 2019 Share Posted October 18, 2019 Hi, I just discovered the "ternary operator" for if else and found the page on this website for it, here: https://www.w3schools.com/java/java_conditions.asp under "Short Hand If...Else (Ternary Operator)." The description states that it can be used "if you have only one statement to execute" but I believe that to be innacurate (or at least worded improperly/confusingly) and should be fixed. For example, the following code I've attached seems to work just fine: Likewise I've tested this format on another method with a length of 26 of these statements, (as opposed to just the two above), and so it appears the statments can be strung together practically without limit. I just figured the information should be a little clearer. There aren't many people who would try to test something which says it won't work. Link to comment Share on other sites More sharing options...
Funce Posted October 21, 2019 Share Posted October 21, 2019 You would be correct. Ternaries can perform multiple statements of execution. I'll point out that you're still executing one statement by nesting ternaries but the ability is there. However to use ternaries to execute multiple statements, would be... not ideal. Why would you not use an if...else? Additionally nesting ternaries exponentially reduces readability past the first. Link to comment Share on other sites More sharing options...
Makwana Prahlad Posted April 21, 2020 Share Posted April 21, 2020 Hello, @lodyne Please try this code,To If Else using ternery operator. public class testpls { public static void main(String[] args){ int time=22; System.out.println(time<=18 ? "Good day. " : time<=22 ? "Good Evening." : "Good Night.." ); } } I hope this code will be usefull for you. Thank You. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now