int y;
if (x > 0) y = 1;
else y = -1;Which single statement is equivalent?
- A
int y = x > 0 ? -1 : 1;
- B
int y = x ? 1 : -1;
- Ccheck_circle
int y = x > 0 ? 1 : -1;
- D
int y = (x > 0) - 1;
Explanation
The ternary cond ? a : b returns a when true, b when false: y becomes 1 if x > 0, otherwise -1.