Course Introduction to Programming

Decision Structures

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

In this lesson, we will deepen our knowledge of relational operators and the if Decision Structure, fundamental elements in building programs that make decisions based on logical conditions.

We will explore how to compare values using different operators, understand how the computer interprets expressions as true or false, and learn to apply the if decision structure to control the execution flow of a program in a logical and structured way.

Relational operators

Relational operators are used to compare two values and check if a relationship between them is true (1) or false (0).

Examples:

Expression Result
2 > 1 true (1)
3 <= 0 false (0)

The main operators that will be used in C are represented in the table below:

Operator Meaning Example Result
< Less than 3 < 5 1
> Greater than 5 > 9 0
<= Less than or equal to 4 <= 4 1
>= Greater than or equal to 7 >= 10 0
== Equal to 8 == 8 1
!= Not equal to 6 != 5 1

โš ๏ธ Notes: The relationship is always applied by comparing the value to the left of the operator with the value to the right, in that order. Also, the equality operator is ==, do not confuse it with the assignment operator, which is =.

False or true

In programming in general, the values of false and true are assigned to numbers. That is, if a certain comparison is true, then it will have the value . Otherwise, it will have the value . Knowing this, we can print the result of a comparison using the printf command:

printf("%d\n", 1 < 2);
printf("%d\n", 3 > 4);
/* Output:
1
0

*/

Notice that the output of the first command was , because the comparison is true.
The output of the second command was , because the comparison is false.

If decision structure

Using the relational operators above, we can apply them to create a decision structure based on the truthfulness of a statement. That is, if such a statement is true, execute a certain code. This structure is the if (from English), and it does exactly what was said above. Its syntax is as follows:

if( condition ){
    Code
}

When the program reaches this structure, it will check if the statement/condition contained within the parentheses is true. If it is, then the code contained within the curly braces will be executed. Otherwise, this code will be ignored and the program will continue its execution after the curly braces.

Example:

if( 1 < 2 ){
    printf("1 less than 2\n");
}
/* Output:
1 less than 2

*/

Notice that, since the comparison is true, the printf command was executed.

โš ๏ธ Note: Notice that the comparison used resulted in the value , because it is true. In C, any value different from will be considered as true. For a better understanding, see the following code:

if(3){
    printf("Value different from 0\n");
}
/* Output:
Value different from 0

*/

Since is a value different from , it is considered true, allowing the printf command to be executed. With all the knowledge obtained so far, we can create a program that reads a certain value and tells us if it is even or odd.
Before proceeding with the reading, we recommend that you try to perform this task on your own. If you can't, here is one way to do it:

#include <stdio.h>

int main(){
    int x;

    scanf("%d", &x);

    if(x % 2 == 0){
        printf("%d is even!\n", x);
    }

    if(x % 2 != 0){
        printf("%d is odd!\n", x);
    }
}

Explanation: For a value to be even, the remainder of its division by must be equal to , and that is exactly what the code above does. It starts by declaring the variable , reads it with scanf and checks if , that is, it checks if has a remainder of when divided by .
If this statement is true, the first printf will be executed.
Regardless of whether the condition in the first if is true or false, the second if will also be checked.
If has a remainder different from when divided by , then the second printf will be executed.

Conclusion

Throughout this lesson, you have deepened your knowledge of relational operators and the if decision structure, understanding how to express logical conditions and control the execution flow of a program in C. We explored how to compare values, interpret true or false results, and apply the if command so that the program makes decisions autonomously and consistently with the established logic.

Now you are able to create programs that analyze conditions and execute specific actions based on logical results โ€” an essential step to developing smarter and more dynamic algorithms! ๐Ÿ˜ƒ