1
votes

I am studying Ternary operators right now and I understand most of it but want to see if this is possible.

normal ternary : (condition) ? (result true): (result false);

question is how to write one with just a true part no false;

(condition) ? (result true):; or (condition) ? (result true); or Not possible

2

2 Answers

0
votes

It's just that would not make sense as expressions must represent a concrete value.

You cannot use if/else clauses as they are no expressions:

print(if (value > 0) { "yes" } else { "no" });

But you can use the ternary operator for "inline" evaluation:

print(value > 0 ? "yes" : "no");

Assume what happens if the condition is evaluated to false and there is no "result" - how should the compiler know what to print?

0
votes

In JavaScript a ternary example looks like this:

const value = 1;
const result = value > 0 ? "Bigger" : "Smaller";
console.log(result);

Result: Bigger

If you don't need show "Smaller", you can try this:

const value = 1;
const result = value > 0 && "Bigger";
console.log(result);

Result: Bigger