All if statements require two cases, one to execute if the condition is true, the other (the else) to execute if the condition is false.


Every if statement requires an else


Here is what's right.

In Java it is perfectly legal to have if statements without else.

However, the conditional operator (c ? et : ef) does require expressions for both cases (et and ef). This is because the conditional operator is an expression, and thus it must produce a value no matter whether the condition is true. An if statement does not have that requirement, because it is a statement, and thus it does not produce any value.


Here we see an occurrence where the student added an else containing an assignment that changes nothing:

int count(int[] values, int value) {
  for (int v : values) {
    if (v==value) {
      count = count + 1;
    } else { // else branch is not necessary
      count = count;

This probably comes from learning to program in a pure functional language, where everything is an expression.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.