Decision Making in C
Tags: c, beginners, tutorial, programming
Published: 2021-07-30 08:11:44

### Equality and Relational Operators
Executable statements either perform actions (such as calculations or input or output of data) or make decisions. We might make a decision in a program, for example, to determine whether a person’s grade on an exam is greater than or equal to 60 and whether the program should print the message “Congratulations! You passed.”<br/> 
C’s *if* statement allows a program to make a decision based on the truth or falsity of a statement of fact called a condition. If the condition is true (i.e., the condition is met), the statement in the body of the if statement is executed. If the condition is false (i.e., the condition isn’t met), the body statement isn’t executed.<br/>
Whether the body statement is executed or not, after the if statement completes, execution proceeds with the next statement in sequence after the if statement.
<br/>
```C
// Using if statements, relational operators, and equality operators
#include <stdio.h>

// function main begins program execution
int main(void)
{
    printf("Enter two integers, and I will tell you\n");
    printf("the relationships they satisfy: ");

    int num1; // first number to be read from user
    int num2; // second number to be read from user

    scanf("%d %d", &num1, &num2); // read two integers

    if (num1 == num2)
    {
        printf("%d is equal to %d\n", num1, num2);
    } // end if

    if (num1 != num2)
    {
        printf("%d is not equal to %d\n", num1, num2);
    } // end if

    if (num1 < num2)
    {
        printf("%d is less than %d\n", num1, num2);
    } // end if

    if (num1 > num2)
    {
        printf("%d is greater than %d\n", num1, num2);
    } // end if

    if (num1 <= num2)
    {
        printf("%d is less than or equal to %d\n", num1, num2);
    } // end if

    if (num1 >= num2)
    {
        printf("%d is greater than or equal to %d\n", num1, num2);
    } // end if
} // end function main
```
Samples of Output:
```cmd
Enter two integers, and I will tell you
the relationships they satisfy: 3 7
3 is not equal to 7
3 is less than 7
3 is less than or equal to 7
```

```cmd
Enter two integers, and I will tell you
the relationships they satisfy: 22 12
22 is not equal to 12
22 is greater than 12
22 is greater than or equal to 12
```
```cmd
Enter two integers, and I will tell you
the relationships they satisfy: 7 7
7 is equal to 7
7 is less than or equal to 7
7 is greater than or equal to 7
```

Conditions in *if* statements are formed by using the equality operators and relational operators. the relational operators all have the same level of precedence and they associate left to right. The equality operators have a lower level of precedence than the relational operators and they also associate left to right.<br/>
*Note: In C, a condition may actually be any expression that generates a zero (false) or nonzero (true) value.*

| Algebraic    | C Syntax         | Example of C   | Meaning of C Condition  |
| -------- | -------------- |-------------- | ------------- |
| *Relational operators* |               |           |            |
| > | > |x > y |x is greater than y |
| < | < |x < y |x is less than y |
| >= | > |x >= y |x is greater than or equal to y |
| <= | <= |x <= y |x is less than or equal to y |
| *Equality operators* |               |           |            |
| =  | == |x == y |x is equal to y |
|  | != |x != y |x is not equal to y |

***Comparing Numbers***
The if statement
```C
if (num1 == num2)
    {
        printf("%d is equal to %d\n", num1, num2);
    } // end if
```
compares the values of variables *num1* and *num2* to test for equality. If the values are equal, the statement in line 13 displays a line of text indicating that the numbers are equal. If the conditions are true in one or more of the if statements starting in lines 16, 19, 23, 27 and 31, the corresponding body statement displays an appropriate line of text.<br/>
Indenting the body of each if statement and placing blank lines above and below each if statement enhances program readability.

Some of the words we’ve used in the C programs —such as *int*, *if* and *void*— are keywords or reserved words of the language. These words have special meaning to the C compiler, so you must be careful not to use these as identifiers such as variable names.