Python Ternary Operator Without else

Yes, you can do this: <condition> and myList.append(‘myString’) If <condition> is false, then short-circuiting will kick in and the right-hand side won’t be evaluated. If <condition> is true, then the right-hand side will be evaluated and the element will be appended. I’ll just point out that doing the above is quite non-pythonic, and it would … Read more

Java Ternary without Assignment

Nope you cannot do that. The spec says so. The conditional operator has three operand expressions. ? appears between the first and second expressions, and : appears between the second and third expressions. The first expression must be of type boolean or Boolean, or a compile-time error occurs. It is a compile-time error for either … Read more

Ternary Operators Java

In this case, you don’t even need a ternary operator: cmdCse.setVisible(selection.toLowerCase().equals(“produkt”)); Or, cleaner: cmdCse.setVisible(selection.equalsIgnoreCase(“produkt”)); Your version: selection.toLowerCase().equals(“produkt”)? cmdCse.setVisible(true): cmdCse.setVisible(false); is semantically incorrect: ternary operator should represent alternative assignments, it’s not a full replacement for if statements. This is ok: double wow = x > y? Math.sqrt(y): x; because you are assigning either x or Math.sqrt(y) … Read more

Java: Ternary with no return. (For method calling)

No, you can’t. But what’s the point of this over an if-else statement? Are you really trying to save 7 characters? if (name.isChecked()) { name.setChecked(true); } else { name.setChecked(false); } or if you prefer bad style: if (name.isChecked()) name.setChecked(true); else name.setChecked(false); Never mind the fact that you can just do (in this case): name.setChecked(name.isChecked()); The … Read more