IF vs Switch
Ray Hunter on 2002 May 19
Ray Hunter on 2002 May 19
When I first started programming in C many of my programmers were littered with IF statements. Is this good programming or there are better ways? What about SWITCH? What is better to use and how can this help to speed up your script execution time?
Rules to Remember
1. Conditions in the IF statement need to be simple.
2. Try to stay away from complex conditions in IF statement if possible.
3. When you use more than two or three IF / ELSEIF - reconsider your application logic.
Here is the age-old question that needs answering - "When do I use an IF statement and when do I use a SWITCH statement?"
Some tips that have helped me out over the years:
Use IF statements when you'
1. Do not know what the value might be'
2. Need a True or False answers'
Use SWITCH statements when you'
1. Have a variable that can have multiple values'
2. Know what the values can be'
Now for the examples
Example 1:
Scenario: There is a variable with an unknown value that cannot be guessed at all, but is surely a number.
Solution: Use an if statement.
<?php
if( $a < 0 )
echo '$a is Less than Zero';
elseif( $a > 0 )
echo '$a is Greater than Zero';
else
echo '$a is Zero';
?>
Why didn't we use SWITCH here?
We used IF in this example simply because we don't know what value $a will be. We can only answer whether $a is Less, More or Equal compared to Zero or not.
Example 2:
Scenario: $a variable is known to be equal to one of these: 1, 5, 10, 15, 35. But, which one?
Use a SWITCH statement to figure it out:
<?php
switch ( $a ) {
case 1:
echo "$a = $a";
break;
case 5:
echo "$a = $a";
break;
case 10:
echo "$a = $a";
break;
case 15:
echo "$a = $a";
break;
case 35:
echo "$a = $a";
break;
default:
echo "$a is not known";
break;
}
?>
What SWITCH did for me more than IF?
The answer is simple - since $a is within a limited amount of results we simply evaluate it against each possibility and this way SWITCH serves us faster than an IF statement.
NOTE: We have also included default statement. Default is a catch-all in the switch conditions, it make us sure that nothing will escape. Just like ELSE.
Additional Info (Nested IF Statements)
Nested IF statements are IF, IF-ELSE statements within IF statements.
Example:
<?php
if ( $a ) {
// Nested if statement.
if( $a > 0 )
echo "$a = $a";
else
echo "$a is too small.";
}
else {
// Do something else;
}
?>
Things to know
1. Do not be lazy and use switch SWITCH statement whenever it makes sense: SWITCH always tend to be faster.
2. If you use IF statements, make sure to have your condition code as fastest to process as possible - you never know how many evaluation it has to do.
3. If you have complex conditions, try splitting them somehow using nested IF statements. These will be faster than having a long list of IF-ELSE statements.
3. Always, (I insist - always) set the most probable answer on top. That is, if 90% of the times $a is greater than Zero, make your statement be:
<?php
if ($a>0)
echo '$a if Greater';
elseif ($a<0)
echo '$a is Less';
else
echo '$a is 0';
?>
Reasons are simple: when parser evaluates your first condition to True, it executes the code within the parenthesis after what it will skip all the remaining ELSE's. In cases when you have the most probable condition to return True at the bottom parser, in most cases, will have to evaluate each of them and slow so down your application.
Ray "BigDog" Hunter
