C - Relational Operators
Relational operators in C are used to compare two values and determine the relationship between them. These operators are often used in conditional statements, such as if statements, to make decisions based on the outcomes of comparisons. Relational operators return a Boolean value, either true (1) if the comparison is true or false (0) if the comparison is false.
Here are the common relational operators in C:
Equality (==):
The Equality (==) operator in C is used to compare two values to determine if they are equal to each other. It checks whether the left operand is equal to the right operand. If the values are equal, the operator returns true (1); otherwise, it returns false (0). The result is a Boolean value.
Key points about the Equality (==) operator:
-
It is a binary operator because it requires two operands (values) for comparison.
- It is used to compare values of the same or compatible data types, such as integers, floating-point numbers, characters, etc.
- The operator returns true (1) if the values on both sides of == are equal and false (0) if they are not equal.
- The result of the comparison can be used in conditional statements (e.g., if statements) to make decisions in a program based on whether the condition is true or false.
Let's illustrate the first relational operator, the Equality (==
) operator, with an example.
#include <stdio.h>
int main() {
int a = 5;
int b = 5;
if (a == b) {
printf("a is equal to b\n");
} else {
printf("a is not equal to b\n");
}
return 0;
}
In this program, we have two integer variables, a
and b
, both of which are assigned the value 5
. We then use the Equality (==
) operator to compare these two variables inside an if
statement.
Here's how the program works:
-
int a = 5; and int b = 5; initialize the variables a and b with the value 5.
-
The if statement checks the condition a == b. It compares the value of a (which is 5) with the value of b (also 5) using the Equality (==) operator.
-
Since a is indeed equal to b, the condition is true. Therefore, the code block inside the if statement is executed.
-
The program prints the message "a is equal to b" to the console.
So, the output of this program is:
a is equal to b
This demonstrates how the Equality (==) operator is used to compare two values in C and make decisions based on whether they are equal or not.
Inequality (!=):
The Inequality (!=
) operator in C is used to compare two values to determine if they are not equal to each other. It checks whether the left operand is not equal to the right operand. If the values are not equal, the operator returns true
(1); otherwise, it returns false
(0). The result is a Boolean value.
Key points about the Inequality (!=) operator:
-
It is a binary operator because it requires two operands (values) for comparison.
- It is used to compare values of the same or compatible data types, such as integers, floating-point numbers, characters, etc.
- The operator returns
true
(1) if the values on both sides of !=
are not equal and false
(0) if they are equal.
- The result of the comparison can be used in conditional statements (e.g.,
if
statements) to make decisions in a program based on whether the condition is true or false.
Let's illustrate the second relational operator, the Inequality (!=
) operator, with an example:
#include <stdio.h>
int main() {
int a = 5;
int b = 3;
if (a != b) {
printf("a is not equal to b\n");
} else {
printf("a is equal to b\n");
}
return 0;
}
In this program, we have two integer variables, a
and b
, with the values 5
and 3
, respectively. We then use the Inequality (!=
) operator to compare these two variables inside an if
statement.
Here's how the program works:
-
int a = 5;
initializes the variable a with the value 5
, and int b = 3;
initializes the variable b
with the value 3
.
-
The
if
statement checks the condition a != b
. It compares the value of a (which is 5
) with the value of b
(which is 3
) using the Inequality (!=
) operator.
-
Since
a
is not equal to b
, the condition is true
. Therefore, the code block inside the if
statement is executed.
-
The program prints the message "a is not equal to b" to the console.
So, the output of this program is:
a is not equal to b
This demonstrates how the Inequality (!=
) operator is used to compare two values in C and make decisions based on whether they are not equal.
Greater Than (>
):
The Greater Than (>
) operator in C is used to compare two values to determine if the left operand is greater than the right operand. It checks whether the value on the left side is larger than the value on the right side. If the left value is greater, the operator returns true
(1); otherwise, it returns false
(0). The result is a Boolean value.
Key points about the Greater Than (>
) operator:
-
It is a binary operator because it requires two operands (values) for comparison.
- It is used to compare values of numeric data types, such as integers or floating-point numbers.
- The operator returns
true
(1) if the value on the left side of '>' is greater than the value on the right side and false
(0) otherwise.
- The result of the comparison can be used in conditional statements (e.g.,
if
statements) to make decisions in a program based on whether the condition is true or false.
Let's illustrate the third relational operator, the Greater Than (>) operator, with an example:
#include <stdio.h>
int main() {
int a = 7;
int b = 3;
if (a > b) {
printf("a is greater than b\n");
} else {
printf("a is not greater than b\n");
}
return 0;
}
In this program, we have two integer variables, a and b, with the values 7 and 3, respectively. We then use the Greater Than (>) operator to compare these two variables inside an if statement.
Here's how the program works:
-
int a = 7;
initializes the variable a with the value 7
, and int b = 3;
initializes the variable b
with the value 3
.
-
The
if
statement checks the condition a > b
. It compares the value of a (which is 7
) with the value of b
(which is 3
) using the Greater Than (>
) operator.
-
Since
a
is indeed greater than b
, the condition is true. Therefore, the code block inside the if
statement is executed.
-
The program prints the message "a is greater than b" to the console.
So, the output of this program is:
a is greater than b
This demonstrates how the Greater Than (>
) operator is used to compare two values in C and make decisions based on whether the left value is greater than the right value.
Less Than (<):
The Less Than (<
) operator in C is used to compare two values to determine if the left operand is less than the right operand. It checks whether the value on the left side is smaller than the value on the right side. If the left value is smaller, the operator returns true
(1); otherwise, it returns false
(0). The result is a Boolean value.
Key points about the Less Than (<
) operator:
-
It is a binary operator because it requires two operands (values) for comparison.
- It is used to compare values of numeric data types, such as integers or floating-point numbers.
- The operator returns true (1) if the value on the left side of < is less than the value on the right side and false (0) otherwise.
- The result of the comparison can be used in conditional statements (e.g., if statements) to make decisions in a program based on whether the condition is true or false.
Let's illustrate the fourth relational operator, the Less Than (<) operator, with an example:
#include <stdio.h>
int main() {
int a = 4;
int b = 8;
if (a < b) {
printf("a is less than b\n");
} else {
printf("a is not less than b\n");
}
return 0;
}
In this program, we have two integer variables, a
and b
, with the values 4
and 8
, respectively. We then use the Less Than (<
) operator to compare these two variables inside an if
statement.
Here's how the program works:
-
int a = 4;
initializes the variable a with the value 4
, and int b = 8;
initializes the variable b
with the value 8
.
-
The
if
statement checks the condition a < b
. It compares the value of a
(which is 4
) with the value of b
(which is 8
) using the Less Than (<
) operator.
-
Since
a
is indeed less than b
, the condition is true. Therefore, the code block inside the if
statement is executed.
-
The program prints the message "a is less than b" to the console.
So, the output of this program is:
a is less than b
This demonstrates how the Less Than (<
) operator is used to compare two values in C and make decisions based on whether the left value is less than the right value.
Greater Than or Equal To (>=):
The Greater Than or Equal To (>=
) operator in C is used to compare two values to determine if the left operand is greater than or equal to the right operand. It checks whether the value on the left side is either greater than or equal to the value on the right side. If the left value is greater than or equal, the operator returns true
(1); otherwise, it returns false
(0). The result is a Boolean value.
Key points about the Greater Than or Equal To (>=
) operator:
-
It is a binary operator because it requires two operands (values) for comparison.
- It is used to compare values of numeric data types, such as integers or floating-point numbers.
- The operator returns
true
(1) if the value on the left side of >=
is greater than or equal to the value on the right side and false
(0) otherwise.
- The result of the comparison can be used in conditional statements (e.g.,
if
statements) to make decisions in a program based on whether the condition is true or false.
Let's illustrate the fifth relational operator, the Greater Than or Equal To (>=
) operator, with an example:
#include <stdio.h>
int main() {
int a = 5;
int b = 5;
if (a >= b) {
printf("a is greater than or equal to b\n");
} else {
printf("a is not greater than or equal to b\n");
}
return 0;
}
In this program, we have two integer variables, a
and b
, both of which are assigned the value 5
. We then use the Greater Than or Equal To (>=
) operator to compare these two variables inside an if
statement.
Here's how the program works:
-
int a = 5;
and int b = 5;
initialize the variables a
and b
with the value 5
.
-
The
if
statement checks the condition a >= b
. It compares the value of a
(which is 5
) with the value of b
(also 5
) using the Greater Than or Equal To (>=
) operator.
-
Since
a
is equal to b
, the condition is true
. Therefore, the code block inside the if
statement is executed.
-
The program prints the message "a is greater than or equal to b" to the console.
So, the output of this program is:
a is greater than or equal to b
This demonstrates how the Greater Than or Equal To (>=
) operator is used to compare two values in C and make decisions based on whether the left value is greater than or equal to the right value.
Less Than or Equal To (<=):
The Less Than or Equal To (<=
) operator in C is used to compare two values to determine if the left operand is less than or equal to the right operand. It checks whether the value on the left side is either less than or equal to the value on the right side. If the left value is less than or equal, the operator returns true
(1); otherwise, it returns false
(0). The result is a Boolean value.
Key points about the Less Than or Equal To (<=
) operator:
-
It is a binary operator because it requires two operands (values) for comparison.
- It is used to compare values of numeric data types, such as integers or floating-point numbers.
- The operator returns
true
(1) if the value on the left side of <=
is less than or equal to the value on the right side and false
(0) otherwise.
- The result of the comparison can be used in conditional statements (e.g.,
if
statements) to make decisions in a program based on whether the condition is true or false.
Let's illustrate the sixth relational operator, the Less Than or Equal To (<=
) operator, with an example:
#include <stdio.h>
int main() {
int a = 3;
int b = 5;
if (a <= b) {
printf("a is less than or equal to b\n");
} else {
printf("a is not less than or equal to b\n");
}
return 0;
}
In this program, we have two integer variables, a
and b
, with the values 3
and 5
, respectively. We then use the Less Than or Equal To (<=
) operator to compare these two variables inside an if
statement.
Here's how the program works:
-
int a = 3;
initializes the variable a with the value 3
, and int b = 5;
initializes the variable b
with the value 5
.
-
The
if
statement checks the condition a <= b
. It compares the value of a
(which is 3
) with the value of b
(which is 5
) using the Less Than or Equal To (<=
) operator.
-
Since
a
is indeed less than b
, the condition is true. Therefore, the code block inside the if
statement is executed.
-
The program prints the message "a is less than or equal to b" to the console.
So, the output of this program is:
a is less than or equal to b
This demonstrates how the Less Than or Equal To (<=
) operator is used to compare two values in C and make decisions based on whether the left value is less than or equal to the right value.