[prev] 70 [next]

Exercise 8: Conditionals

For each of the following:
  • either rewrite it using a conditional expression
  • state why it cannot be rewritten in this way

// (a)
if (x > 0)
   y = x - 1;
else
   y = x + 1;

// (b)
if (x > 0)
   y = x - 1;
else
   z = x + 1;

// (c)
if (x > 0) {
   y = x - 1;  z = x + 1;
} else {
   y = x + 1;  z = x - 1;
}