Satish Patro
Satish Patro

Reputation: 4404

Assinging constant value to one among different variables based on a condition

Normally in ternary operation, LHSside is constant, RHS changes.

int marks;
String result = (Grade > marks) ? 'PASS' : 'FAIL';

But, what I want

int marks;
if(Grade > marks)
    passMark = marks;
else
    failedMark = marks;

Upvotes: 0

Views: 314

Answers (3)

Kudin
Kudin

Reputation: 466

Assignments a = b in Java yield no value, but ternary operator expects some values to be returned out of it.

EDIT: I just realised that I don't resolve your issue.

Please note that that hacking the ternary operator with assignments taken into brackets is like kicking Java in the face.

Solution: You should NOT be assigning multiple variables in one line. Use multiple lines, as much as you need and then once the code is working and has no bugs you can try to make it more efficient.

A very important thing to understand in programming is that finding the smallest most convoluted solution to your problem may result in losing much time once you revisit it in the future.

Upvotes: 1

Nexevis
Nexevis

Reputation: 4667

You can technically do:

marks = (Grade > marks) ? (passMark = marks) : (failedMark = marks);

Which will give you what you want, but the problem is marks is just being assigned the same value it already had because you cannot just have no variable assignment when using the ternary operator.

This is a bit confusing, so I don't know if I condone this.

Java Docs Source for conditional operator:

It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.

This is relevant because say you extract the passMark = marks to a method:

public static void passMarks(){
   passMark = marks;
}

And try to use it like:

(Grade > marks) ? passMarks() : failedMarks();

It would be a void method because you do not actually need to return anything, your logic is already complete. So this conditional operator only works because we are basically giving it a dummy return value that we do not need because void is against the specifications.

Now you could always add a return that means something like

public static String passMarks(){
   passMark = marks;
   return "PASSED";
}

And use it like this:

String passed = (Grade > marks) ? passMarks() : failedMarks();

Which will set the values AND return the String that says PASSED or FAILED, and do multiple things at once!

Note: I am pretending passMark and marks are class variables accessible from the method without passing it for ease of explanation.

Upvotes: 1

Gaurav Jeswani
Gaurav Jeswani

Reputation: 4592

You can do it like it (although not recommendable)

int marks;
int marks = (Grade > marks) ? (passMark = marks) : (failedMark = marks);

Because the ternary operator consists of a condition that evaluates to either true or false, plus a value that is returned if the condition is true and another value that is returned if the condition is false.

Upvotes: 1

Related Questions