assign and execute if/else conditions in single line

21,206

Solution 1

You can use java ternary operator,

result = testCondition ? value1 : value2

this statement can be read as, If testCondition is true, assign the value of value1 to result; otherwise, assign the value of value2 to result.

simple example would be,

minVal = a < b ? a : b;

In above code, if the variable a is less than b, minVal is assigned the value of a; otherwise, minVal is assigned the value of b.

here is another example using String,

// result is assigned the value "it's false"
String result = false ? "that was true" : "it's false";

this examples would clear it more. It prints the salutation properly for a person's gender:

// if `person.isMale()` then its Mr. else its Ms. 
return = "Thank you " + (person.isMale() ? "Mr. " : "Ms. ") + person.getLastName() + ".";

edit:

Question: you cannot call a method inside ternary operator.

No, this is incorrect.

You can call a method inside a ternary operator as long as it returns a value which is of the same type as the left-side of a ternary operator,

consider this example,

let's create a method that returns a String value,

String message(){
    return "This is returned value";
}

Now see this code,

String answer = false ? "true" : message();

this will execute just fine because return type of message() and variable type of answer are the same, i.e String.

output:

This is returned value

Solution 2

For putting a conditional statement on one line, you could use the ternary operator. Note, however, that this can only be used in assignments, i.e. if the functions actually return something and you want to assign that value to a variable.

int abs = x > 0 ? x : -1; 

If you want to execute methods, that do not return anything, but have some other side-effects, the ternary operator is not the right way to go. In fact, it does not even compile!

void doThis() {/* do this */}
void doThat() {/* do that */}

(condition) ? doThis() : doThat();  // does not work!

Instead, you should just use a plain-old if-then-else. It's also easier to read.

if (condition) {
    doThis();
} else {
    doThat();
}

Solution 3

You can not use Ternary Operator as you can not invoke any void method (i.e. the method which do not return anything ) from Ternary Operator

As it is not the intended use of the ternary operator.

If you really want it to be achieved in 1 line, you can write:

if (condition) doThisMethod(); else doThatMethod();

Solution 4

max = (a > b) ? a : b;

Is logically identical to

if (a > b) {
    max = a;
} else { max = b; }
Share:
21,206
peti446
Author by

peti446

Updated on January 31, 2020

Comments

  • peti446
    peti446 over 4 years

    How can I do that for example, if block checks if the condition is true or false and then execute action depends on that condition?

    like,

    if (check true or false) 
             if false do action
             if true do action
    

    Something like that do it all in the same line without blocks, it that possible ?

    Sorry for my bad English

    Thanks.