Java – control structure used with 3 possible inputs (efficiency)

In my java program, I need a control structure that should provide three different results for three different inputs

Input 1: text1

Input 2: text2

Input 3: Text3

My question is: best practice wise and efficiency wise, which control structure should be used? My first idea was to transform cases, but why would I choose if structures or nesting? Operator –

Solution

I think you'll find it generally accepted that in this case, conversion statements are a mild first choice, purely based on readability If additional cases are added, it can be extended better. Even with three options, it is still more readable, especially when one input is only three variants Performance differences are negligible, but if you really want to enter this particular area, there will be discussion

For similar readability reasons, I recommend avoiding using ternary operators (i.e., inline if / '?') in more than two cases Personally, I don't parse it. I'll avoid it unless all the expressions involved are very brief

The main deviation is from the theme, but interestingly, the open string was not added to Java until Java 7

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>