Compile time Error

exercise No. 273

Q:

We consider:

public static String getResult(int a, int b) {
  if (0 == a % 2) {
    return "o.K.";
  } else if (a + b < 0) {
    return "Quite o.K.";
  } else if (4 < a * b) {
    return "bad";
  }
}

Why does the compiler complain about a Missing return statement despite several being present?

A:

The if-clause is incomplete. In case all three conditions are false no return statement is being reached. Among several solutions a final else clause solves the issue:

public static String getResult(int a, int b) {
  if (0 == a % 2) {
    return "o.K.";
  } else if (a + b < 0) {
    return "Quite o.K.";
  } else if (4 < a * b) {
    return "bad";
  } else {
    return "very bad";
  }
}