Page 3 of 3
Now that you are aware that "else is hard" we can look at the default statement which essentially gives the switch an "else" or catch all option. If you end a switch with a default statement then if none of the case statements are obeyed the default is. For example:
You can see that the default instructions are only obeyed if none of the other conditions hold - that is if $a isn't equal to 1, 2, 3 or 4. As with all "else" type of constructions you should ask yourself what the condition is that causes the default statement to be obeyed? The answer is that the condition that is true when the default instructions are obeyed is $a<1 or $a>4.
It can be difficult to work out when an else or a default is going to be obeyed but there is a conditional instruction that is even tougher to get right - the elseif. The elseif can be placed within an if statement in the same way as an else but it can include a condition that has to be true before its instructions are obeyed. You can have as many additional elseifs within a single if and have a final else to deal with anything that doesn't satisfy any of the conditions.
The idea is that you can use the elseif to place additional conditions on the else part of the if statement. For example:
echo("the value is one");
echo("the value is two");
echo("the value is three");
echo("the value is
none of the above");
In this case the first echo is obeyed if $a is one. If this isn't the case then the elseif is evaluated and its echo statement is only obeyed if $a isn't one but is equal to two. Similarly the third echo statement is only obeyed if a$ isn't one or two but is equal to three. The final echo is only obeyed if a$ isn't one, two or three.
You can see that the elseif gives us an alternative way to implement a switch but usually when an if statement is easily reducible to a switch then it is better to use a switch. The real power and so danger of the elseif occurs when the conditions are such that they aren't mutually exclusive - in the previous example $a could only be one of the values specified. Consider the following small modification on the previous example:
echo("the value of a is one");
echo("the value of b is two");
echo("the value of c is three");
echo("what is the condition?");
What is printed? The answer is that the first echo is obeyed because $a is one and the else parts of the if statement aren't even considered because the if condition is true. However, if $a was a value other than one then what would be printed would depend on the value of $b or $c. Notice that only one of the messages would ever appear because as soon as a condition evaluates to true the following elseifs or else are skipped.
Now answer the final question - what is the condition that is satisfied for the echo belonging to the else to be obeyed?
The answer is that $a has to be something other than one, $b has to be something other than 2 and $c has to be something other than 3 i.e. ($a!=1) and ($b!=2) and ($c!=3) has to be true.
In general when you are reading or trying to understand a complicated if-elseif-else construction always remember that for the if part to be obeyed the first condition has to be true. For the first elseif to be obeyed the first condition has to be false and the second condition true. For the second else if the first two conditions have to be false and the third has to be true and so on down to the final else for which all of the conditions have to be false.
If the else is hard the elseif can be very hard. Building complicated if statements is error prone but sometimes there is no choice. Always try to create the if statement or the switch that best conveys your meaning and always check that the elseif, else or default conditions are what you expect.