Relational operators in C programming
Relational Operators in C
Relational operators in C are used to compare two values. They return a boolean result, which is typically used in conditional statements to control the flow of the program. Here’s a guide to the relational operators available in C:
1. Equal to Operator (==)
The == operator checks if two values are equal.
int a = 5;
int b = 10;
if (a == b) {
printf("a is equal to b");
} else {
printf("a is not equal to b");
}
2. Not Equal to Operator (!=)
The != operator checks if two values are not equal.
int a = 5;
int b = 10;
if (a != b) {
printf("a is not equal to b");
} else {
printf("a is equal to b");
}
3. Greater than Operator (>)
The > operator checks if one value is greater than another.
int a = 5;
int b = 10;
if (a > b) {
printf("a is greater than b");
} else {
printf("a is not greater than b");
}
4. Less than Operator (<)
The < operator checks if one value is less than another.
int a = 5;
int b = 10;
if (a < b) {
printf("a is less than b");
} else {
printf("a is not less than b");
}
5. Greater than or Equal to Operator (>=)
The >= operator checks if one value is greater than or equal to another.
int a = 5;
int b = 5;
if (a >= b) {
printf("a is greater than or equal to b");
} else {
printf("a is less than b");
}
6. Less than or Equal to Operator (<=)
The <= operator checks if one value is less than or equal to another.
int a = 5;
int b = 5;
if (a <= b) {
printf("a is less than or equal to b");
} else {
printf("a is greater than b");
}
Relational Operators Summary
| Operator | Description | Example | Usage |
|---|---|---|---|
== |
Equal to | a == b |
Checks if two values are equal |
!= |
Not equal to | a != b |
Checks if two values are not equal |
> |
Greater than | a > b |
Checks if one value is greater than another |
< |
Less than | a < b |
Checks if one value is less than another |
>= |
Greater than or equal to | a >= b |
Checks if one value is greater than or equal to another |
<= |
Less than or equal to | a <= b |
Checks if one value is less than or equal to another |
Note: Relational operators are used in conditional statements such as
if and while to control the flow of the program based on comparisons.
Comments
Post a Comment