Course Introduction to Programming
If-Else-If
In this lesson, we will learn what is and how to use the decision structure if-else-if. The lesson is divided into the following topics:
- Example of a problem
- Structure if-else-if
Example of a problem
A student has a grade of
Consider that a student is approved if his/her grade average is greater than or equal to
To solve this problem, we have the following 2 options:
- Do three if's: one asking if the average is greater than or equal to
, another asking if it is less than and another asking if it is less than and greater than or equal to .
if( x >= 7 ){ // checks if x is greater than or equal to 7
printf("Approved!");
}
if( x < 4 ){ // checks if x is less than 4
printf("Failed!");
}
if( x < 7 && x >= 4 ){ // checks if x is less than 7 and greater than or equal to 4
printf("Summer School");
}
- Use the if-else-if structure.
Structure if-else-if.
To use the second option presented, we will need to learn what it is and how to use the if-else-if structure.
It is a structure that specifies a code to run if one condition is true and another code if the first condition is false and a second condition is true.
This structure follows the template below:
if( condition1 ){
// if condition 1 is true
}
else if( condition2 ){
// if condition 1 is false and condition 2 is true
}
In the above structure, the code block contained within the first if will only be executed if condition 1 is true. If, and only if, condition 1 is false, then condition 2 will be tested and, if true, then the code block contained within the else if will be executed.
Note that the second condition is only tested if the first one is false.
Therefore, we can read the code above as follows: "If this condition is true, execute this. Otherwise, if this other condition is true, execute this".
This structure can still be expanded, using multiple else if's and ending with an else, as shown below:
if( condition1 ){
// if condition 1 is true
}
else if( condition2 ){
// if condition 1 is false and condition 2 is true
}
else if( condition3 ){
// if condition2 is false and condition3 is true
}
else if( condition4 ){
// if condition3 is false and condition4 is true
}
else{
// if condition 4 is false
}
In the above code, the first condition will be tested. If it is true, its respective code will be executed and the whole if-else-if chain will be interrupted because the next condition will not even be tested. If not, then the second condition will be tested. If it is true, then its respective code will be executed and the chain will be interrupted. Otherwise, then the third condition will be tested, and so on. If all conditions are false then the code within the else will be executed.
This way, to solve the exercise presented at the start of this class by the second method, we will have the following code:
if( x >= 7){ // checks if x is greater than or equal to 7
printf("Approved!");
}
else if( x < 4 ){ // if the above condition is false, test if x is less than 4
printf("Failed!");
}
else{ // if all the above conditions are false
printf("Summer School");
}
See you in the next lesson!