Selection
All the programs/functions we have written so far involved only the concept of sequence. We wrote our code by stating all the steps in the order they need to be performed. In this section, we are looking at how we can perform different steps depending on some situation. This is known as selection. Essentially we are selecting different pieces of code depending on some condition.
In C there are various constructs that can accomplish selection. In this chapter we will focus on the if/else constructs. The other forms of selection (switch and conditional operator) will be discussed towards the end of the course. Focus on learning how to write your code using just the if/else construct for now.
if - an optional statement
The if statement allows you to effectively have an optional sequence.
<A>
if (condition){
<B>
}
<C>
If the condition is true, the code will do statements A,B,C. However, if the condition was false, the code will do statements A,C (skipping B)
if/else - do one thing or another
The if/else statement allows you to choose between two mutually exclusive. This means that there is a complete splitting of paths, do one or other.
<A>
if(condition){
<B>
}
else{
<C>
}
<D>
If the condition is true, the code will do statements A,B,D. However, if the condition was false, the code will do statements A,C,D. B and C are mutually exclusive statements, if B is done, C is not and vice versa
if/if/if... - do 0 or more optional statements
The if/if/if set of statements allow you to choose between multiple optional steps based on a set of conditions. Each condition being true selects the optional step associated with the condition. the choices are not mutually exclusive. Selecting one does not mean the others are are selected or not selected. The easiest way to think about this is to view each if as an option. Any number of options can be chosen. In the example below, A and E will always occur. However, B,C and D are all optional statements. Whether they are chosen or not depends on conditions 1, 2 and 3.
<A>
if(condition 1){
<B>
}
if(condition 2){
<C>
}
if(condition 3){
<D>
}
<E>
if/else if/else multiple mutually exclusive conditions
The if/else if/else expression is used for selecting between multiple mutually exclusive conditions. in if/else there exactly two paths. with if/else if there is actually 3 paths. if/else if involves 2 conditions. If the first condition is false, there is a second condition that is tested, its results determines the next step. This type of statement can have multiple else if conditions where a condition testing as false result in the testing of the next else if statement. At the end there is an optional else that covers all remaining possibilities. The difference between if/else if/else if/... and if/if/if is that the statements are mutually exclusive. The moment one of the conditions are true, the other conditions are no longer considered.
<A>
if (condition 1){
<B>
}
else if(condtion 2){
<C>
}
else{
<D>
}
<E>
In this example, the first statement is A. After that it is either B, C or D followed by E. Only one of B, C or D is ever chosen (but one of them will be chosen)
Less is more! If there are only two mutually exclusive possibilities for a condition, it is better to test the condition and use if/else. Do not test a condition then use if/else if to test the opposite condition as you are more likely to create errors AND it will cause your code to be slower as it requires an extra check
| Testing where result has only 2 outcomes | Good/Bad |
|---|---|
| ✅ Good! |
| ❌ Bad! |