Course Introduction to Programming

If-Else

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

In this lesson, we will deepen our knowledge of the if-else Decision Structure, one of the fundamental concepts of programming. This structure allows the program to make automatic decisions, choosing between two possible paths based on a logical condition.

We will learn how to apply the Logical NOT Operator (!) to invert conditions, understand how if and else work together in code execution, and build programs capable of reacting intelligently to different situations.

Problem Example

A student has an average grade of , and would like to know if they are approved or not. Consider that a student is approved if their average is greater than or equal to , and failed otherwise. To solve this problem, we have the following 3 options:

  1. Use two ifs: one checking if the average is greater than or equal to and another checking if it is less than .
if( x >= 7 ){ // checks if x is greater than or equal to 7
    printf("Approved!\n");
}

if( x < 7 ){ // checks if x is less than 7
    printf("Failed!\n");
}
  1. Use two ifs: one checking if the average is greater than or equal to and another checking if it is not greater than or equal to .

  2. Use the if-else structure.

Logical NOT Operator

To use the second option presented, we will need to learn what the logical NOT operator is and how to use it. This operator can be used to ask if a condition is false instead of true. In other words, it will invert the value of the statement.
In C, the logical NOT operator is represented by an exclamation mark (!) and must come before the statement to be analyzed, following the template below:

if( !  condition ){ // checks if the condition is false
    
}

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");
}

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

โš ๏ธ Note: notice that the statement preceded by the logical NOT operator was enclosed in parentheses. If we hadn't done this, then the logical value of would be inverted (if were different from zero, then from true it would become false, and vice-versa) and our code would not work.

The if-else Structure

To use the third option presented, we will need to learn what the if-else structure is and how to use it. It is a structure that specifies a step-by-step process if a condition is true and another step-by-step process if it is false. This structure follows the template below:

if( condition ){
    // True
}
else{
    // False
}

In the structure above, the block of code contained within the if will be executed only if the condition is true. Meanwhile, the block of code contained within the else will be executed only if the condition analyzed by the if is false.

Thus, we can read the code above as follows: "If this condition is true, execute this. Otherwise, execute this". The word else means "otherwise" and, therefore, must always be preceded by an if command.

Thus, to solve the exercise presented above using the third 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 the above condition is false
    printf("Failed!\n");
}

But why use the if-else structure instead of two ifs?

  • Efficiency: the if-else evaluates only one condition; if the if is true, the else is ignored, avoiding unnecessary checks.
  • Clarity: it makes it explicit that there are two mutually exclusive paths, making the code easier to read and understand.
  • Error reduction: it ensures that only one block of code is executed, preventing duplicate or conflicting outputs that can occur with multiple ifs.
  • Best practices: using if-else demonstrates organized and structured logic, being the recommended way when there are only two possible alternatives.

Conclusion

Throughout this lesson, you learned how to use the if-else decision structure to control the execution flow of a program clearly and efficiently. You also understood the functioning of the logical NOT operator (!) and how it can be used to invert conditions.

Now you know how to create programs that make automatic decisions, responding to different situations in a logical, precise, and elegant way! ๐Ÿ˜ƒ