else
due to return
CS-R1044Else
is redundant in else-if
9 Array.Reverse(arr);
10 if ( new string(arr) == s) {
11 return true;
12 } else {13 return false;14 }15 }
16 public int run() {
17 LinkedList<int> palendromes = new LinkedList<int>();
The block under else-if
is executed only when the preceding if
condition evaluates to false
. However, if the last statement of the block under the preceding if
statement is a return
statement, else
in else-if
becomes redundant as the controller never reaches it, and, it can be written as a separate if
statement.
if (x == 1) {
// Do something
return;
} else if (x == 2) {
// Do something
}
if (x == 1) {
// Do something
return;
}
if (x == 2) {
// Do something
}