Course Introduction to Programming

If-Else-If

Course's Page 14 min Text by
User Image
Rahilly Machado

In this lesson, we will deepen our knowledge of the if-else-if Decision Structure, an essential tool for programs that need to make decisions with multiple possibilities in an organized and efficient manner.

We will learn how to structure conditions in sequence, using if, else if, and else, to control the program's execution flow clearly. We will also see how to correctly evaluate different scenarios, ensuring that each situation is handled logically and precisely.

Problem Example

A student has an average grade of , and would like to know if they are approved, failed, or in final evaluation. Consider that a student is approved if their grade is greater than or equal to , failed if their grade is less than , and in final evaluation if they are neither approved nor failed. To solve this problem, we have the following 2 options:

  1. Use three ifs: one checking if the grade is greater than or equal to , another checking if it is less than , and a third checking 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!\n");
}

if( x < 4 ){ // checks if x is less than 4
    printf("Failed!\n");
}

if( x < 7 && x >= 4 ){ // checks if x is less than 7 and greater than or equal to 4
    printf("Final evaluation!\n");
}
  1. Use the if-else-if structure.

The if-else-if Structure

To use the second option presented, we need to learn what the if-else-if structure is and how to use it. It is a structure that specifies a set of steps if a condition is true and another set of steps if the first condition is false and a second condition is true. This structure follows the template below:

if( condicao1 ){
    // if condition1 is true
}

else if( condicao2 ){
    // if condition1 is false and condition2 is true
}

In the structure above, the code block contained within the first if will be executed only if condition 1 is true. If, and only if, condition 1 is false, then condition 2 will be tested and, if it is true, then the code block contained within the else if will be executed. Notice that the second condition is tested only 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 be further expanded by using multiple else ifs and ending with an else, as shown below:

if( condicao1 ){
    // if condition1 is true
}

else if( condicao2 ){
    // if condition1 is false and condition2 is true
}

else if( condicao3 ){
    // if condition2 is false and condition3 is true
}

else if( condicao4 ){
    // if condition3 is false and condition4 is true
}

else{
    // if condition4 is false
}

In the code above, the first condition will be tested. If it is true, its respective code will be executed and the entire if-else-if chain will be interrupted, as the next condition will not even be tested. Otherwise, the second condition will be tested. If it is true, then its respective code will be executed and the chain will be interrupted. If not, then the third condition will be tested, and so on. If all conditions are false, then the code contained within the else will be executed.

Thus, to solve the exercise presented above using the second method, we will have the following code:

if( x >= 7){ // checks if x is greater than or equal to 7
    printf("Approved!\n");
}

else if( x < 4 ){ // if the above condition is false, tests if x is less than 4
    printf("Failed!\n");
}

else{ // if all the above conditions are false
    printf("Final evaluation!\n");
}

Conclusion

Throughout this lesson, you have deepened your knowledge of the if-else-if decision structure, learning how to organize conditions in sequence so that the program executes different blocks according to each scenario. We explored how to use if, else if, and else logically and efficiently, ensuring that each possibility is handled correctly.

Now you know how to build clear and efficient decision flows, avoiding redundancies and ensuring that the code is executed in an organized and predictable way ๐Ÿ˜ƒ