Many if statments

Hi community, I was going to create an extension which is a Unit converter. There needs to be a lot of If-then-else in the java. Can anyone say any idea to make the code less.

Why not use the switch statement?


int value = 29;

switch(value) {
  case 29:
         // Do something 
  case 30:
         // Do something 

I was thinking of this-

variable = (condition) ? expressionTrue :  expressionFalse;

There are called in-line else if.

it's like

boolean result = 1 * 1 == 1 ? true : false;

How can I do if-then-else if with this.

You cannot.

Oh :slightly_smiling_face:

1 Like

Hey but, the POST2 should be ticked as the solution.

That thing is the same which is tried and the length is not shortened. :grin:

You see a large amount of difference when you write big code!

1 Like

My idea is somewhat similar to @Kumaraswamy's

switch(true) {
  case Boolean1:
         // Do something 
  case Boolean2:
         // Do something else
case Boolean3:
         // Do something else

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.