﻿ Operators for Comparison - Painfully Learned C
C language learned by suffering

### Operators for comparison

##### equivalence operator
The == operator was introduced in the previous section, and this line of operators is called the equality operator.
Two types of equality operators are available

Symbols turn out to be true False.
Two values are equal Two values are not equal
! = ! Two values are not equal Two values are equal

These operators are used to compare whether values are equal or not.
The following program determines if the number entered is 10.

source code
`````` #include <stdio.h>

int main(void)
{
int suuti;
scanf("%d", &suuti);
if (suuti == 10)
printf("Input value is 10. \n");
if (suuti ! = 10)
printf("Input value is not 10. \n");
return 0;
}``````

If you let this program run and enter 10, the result will be as follows

Execution Result
10 Data input
Input value is 10.

If you run this program and enter anything other than 10, the results will be as follows

Execution Result
135 Data input
Input value is not 10.

Since it is an operator
This comparison operator, as well as the relation and logical operators described next, are also operators.
That is, they can be placed in formulas and used in calculations as usual.

The following equation computes the value of the variables val1 and val2 as
If they are equal, answer is set to 1 (in many compilers), and
If they are not equal, 0 is assigned.

Some advanced users prefer to do it this way.
It is not easy to understand and is not recommended.

##### relational operator
While the equality operator was an operator to check if two values are equal, the
The relational operator is an operator that examines the relationship between two values.

Symbols turn out to be true False.
< Left value is less than right Left value is not less than right
> Left value is greater than right Left value is not greater than right
<= Left value is less than or equal to right Left value is not less than right
>= Left value is greater than or equal to right Left value is not greater than right

It probably goes without saying by now, but the difference between small and
The difference is whether it includes or excludes equality. Less than & greater than return true even when they are equal.

These operators are used to test the relationship between two values.
The following program determines if the number entered is greater than 10.

Source Code
`````` #include <stdio.h>

int main(void)
{
int suuti;
scanf("%d", &suuti);

if (suuti == 10)
printf("Input value is 10. \n");

if (suuti > 10)
printf("Input value is greater than 10. \n");

if (suuti < 10)
printf("Input value is less than 10. \n");

return 0;
}``````

If you let this program run and enter 10, the result will be as follows

Execution Result
10 Data input
Input value is 10.

If you let this program run and input greater than 10, the results will be as follows

Execution Result
135 Data input
Input value is greater than 10.

If you let this program run and input less than 10, the result will be as follows

Execution Result
5 Data input
Input value is less than 10.

##### logical operator
The logical operators have slightly different properties than the previous operators, and are
It can be used to merge multiple conditions or to reverse a decision.

Symbols Meaning become true False.
&& AND Both right and left conditions are true Either one of the right or left condition is false
|| or (OR) Either one of the right or left condition is true Both right and left conditions are false
! NO (NOT) Condition is false Condition is true

In an if statement, you can only make a decision on one condition at a time.
These operators make it possible to make decisions based on multiple conditions.
The following program determines if the number entered is between 8 and 12.

Source code
`````` #include <stdio.h>

int main(void)
{
int suuti;
scanf("%d", &suuti);

if (suuti >= 8 && suuti <= 12)
printf("Between 8 and 12. \n");

if (! (suuti >= 8 && suuti <= 12))
printf("Not between 8 and 12. \n");

return 0;
}``````

If you let this program run and enter between 8 and 12, the results will be as follows

Execution Result
9 Input data
8-12.

If you run this program and enter anything other than between 8 and 12, the results will be as follows

Execution Result
3 Data input
Not between 8 and 12.

The point of this program is in the conditional part of the if statement.
Since the "if" statement allows only one condition, it is not possible to determine the condition of 8 or more and 12 or less in the usual way.
However, by using the && operator, it is possible to make a decision based on two conditions.

Also, the second if statement's decision is the same as the first one with ! operator.
! operator has the function of reversing the result of a decision, so the
The result is exactly the opposite of the first if statement.

Comment
COMMENT

Open the 💬 comment submission box