“Comparison is the death of joy” Mark Twain
Before reading this section, we’re assuming that you already read the operator section.
Comparison operators are those operators that are used for comparing two constants, variables or expressions. The result of this comparison is either true or false.
For example:
Let’s say we have a variable named jackAge and the value of this variable is 20. Some of the comparison that can be applied to this variable is:
jackAge == 21 // This means check if the value assigned to jackAge is equal to 21 and the result is false because the value is 20 and 20 is not equal to 21.
jackAge > 21 //This means check if the value of the jackAge variable is larger than 21. Because the value is not larger than 21 then the result is false.
In this section we will introduce comparison operators and try to give an example for each one of them.
Here’s an introductory list of Comparison operators:
| Operator | Symbol |
|---|---|
| Equal | == |
| Not equal | != |
| Greater than | > |
| Less than | < |
| Greater or equal to | >= |
| Less than or equal to | <= |
Comparison operators are mostly used within Conditional statements.
In short, conditional statements includes a set of keywords that we use for checking whether or not a condition is true and also part of a source code depend on it. That means if the condition was true, then part of the source code is allowed to be run otherwise that part will be skipped.
In this section we need to use one of these condition-statements in order to explain comparison-operators and so we chose if statement.
You can learn more about if in if-statement section but in short, its use case in programming is the same as what we use it in real world. For example we say if that person’s age is more than 21 then she can go to a Bar otherwise she is not allowed etc.
Here’s the structure of the if statement:
if (condition){
//body of the if-statement where code is and will be run if the condition is true
}else{
//the code here will only run if the condition is not true.
}
if: this is the keyword that we use for starting an if-statement.
(): followed by if keyword is the parentheses where we define the condition (expression) that we want to be true in order to run the instructions within the body of the if statement. This is where comparison operators can be used.
`{}`: the braces that come after `()` is the body of `if-statement` and we declare our instructions to be run if the condition was true.
`else`: if it turns out that defined condition is not true then the instructions within the body of `else` statement will be run. The body of `else` is within the braces` {} ` that comes after the `else` keyword.
Example:
#include <stdio.h>
int main() {
int age = 13;
//Is the person allowed to go to a Bar?
if (age < 21){
//This line will run if the condition is true.
printf("No, this person can't go to a Bar. ");
}else{
// In case the condition was not true, the instruction in this body will run.
printf("Yes, this person can go to a Bar. ");
}
return 0;
}
Result:
No, this person can’t go to a Bar.
In this example we’ve used the if condition to see if the value in the age variable is less than 21 or not via less-than-operator < (which you’ll learn more about it in this section).
The value in the age variable is not larger than 21 and so the condition of the if statement is true and the instructions in its body will run.
Note: depending on the result of the condition, either if statement or else statement’s body will run but not both!
There are more into if statement but for now this is enough. In if-statement section you’ll learn more about it.
Note: because in this section we’re focusing on explaining the comparison-operators, we won’t use complex if-statements for the sake of simplicity.
We use equal == operator to see if the value assigned to a variable or the result of an expression is equal to another value assigned to a variable or a constant-value or the result of another expression.
Example:
#include <stdio.h>
int main() {
int age = 13;
int second = 8;
if (age == (2* second -5 )){
printf("Yes, the value of age is equal to the result of the expression.");
}else{
printf("The result of the expression is not equal to the value of age-variable. ");
}
return 0;
}
Result:
Yes, the value of age is equal to the result of the expression.
In the example above, we’ve used the equal == operator to check whether or not the result of the (2* second -5 )expression is equal to the value assigned to age variable and because the result is true the body of the if statement ran and we have the result you see above.
Not-Equal operator `!=`:
This operator is exactly on the opposite side of equal == operator! Not-Equal != operator is used to check whether or not two values (constants, variables and expressions) are not equal! If the two values are not equal then the result is true otherwise is false.
Example:
#include <stdio.h>
int main() {
int jackAge = 30;
int ellenAge = 33;
if (jackAge != ellenAge){
printf("These two people are not in the same age.");
}else{
printf("These two people were born at the same year ");
}
return 0;
}
Result:
These two people are not in the same age.
In the example above we’ve checked to see if the two variables jackAge and ellenAge contain different values via Not-Equal != operator. If they have different values (which they have) the result of the if statement will be true and the instructions in its body will run.
Greater-than operator `>`:
We use greater-than > operator to compare two values (constants, variables, expression) and see if the value on the left side of the comparison is greater than the value on the right side or not. If the left side value was greater, the result is true otherwise false.
Example:
#include <stdio.h>
int main() {
int jackAge = 30;
int ellenAge = 33;
if (jackAge > ellenAge){
printf("Yes, Jack is older than Ellen");
}else{
printf("No, Jack is younger than Ellen ");
}
return 0;
}
Result:
No, Jack is younger than Ellen
In the example above, we’ve tried to see if Jack jackAge is older than Ellen ellenAge via greater-than > operator in the if statement. But in fact Ellen is older than Jack so the result of this comparison is false and the body of if statement in this case will not run but the body of the else statement will do. And that’s how we’ve got the result you saw above.
Less-than operator `<`:
We use less-than < operator to compare two values (constants, variables, expression) and see if the value on the left side of the comparison is less than the value on the right side or not. If the left value was lesser, the result is true otherwise false.
Example:
#include <stdio.h>
int main() {
int jackAge = 30;
int ellenAge = 33;
if (jackAge < ellenAge){
printf("Yes, Jack is younger than Ellen");
}else{
printf("No, Jack is older than Ellen ");
}
return 0;
}
Result:
Yes, Jack is younger than Ellen
In the example above, we’ve tried to see if Jack jackAge is younger than Ellen ellenAge via less-than < operator in the if statement. In fact Jack is younger than Ellen so the result of this comparison is true and the body of if statement in this case will run. And that’s how we’ve got the result you saw above.
Greater or Equal operator `>=`:
This operator is the combination of two other operators:
-
Greater operator `>`.
-
Equal operator `==`.
Basically via this operator we can compare two values (constants, variables or expressions) and see if the value on the left side of the comparison is larger or equal in size compared to the value on the right side.
If the left value was greater or equal compare to the value on the right side of the comparison then the result is true otherwise false.
Example:
#include <stdio.h>
int main() {
int age = 21;
if (age >= 21){
printf("This person is allowed to enter to a Bar");
}else{
printf("This person can't enter to a Bar");
}
return 0;
}
Result:
This person is allowed to enter to a Bar
In the example above, the condition is to check whether or not the value of the age variable is greater or equal to the value 21 in the if statement. Because the variable’s value is equal to 21 the condition is true and so the instructions in the body of the if statement will run and so that’s why we got the result mentioned above.
Less than or Equal Operator `<=`:
This operator is the combination of two other operators:
-
Less-Than operator `<`.
-
Equal operator `==`.
Basically via this operator we can compare two values (constants, variables or expressions) and see if the value on the left side of the comparison is lesser or equal in size compared to the value on the right side.
If the left value was lesser or equal compare to the value on the right side of the comparison then the result is true otherwise false.
Example:
#include <stdio.h>
int main() {
int age = 20;
if (age <= 21){
printf("This person is not allowed to enter to a Bar");
}else{
printf("This person can enter to a Bar");
}
return 0;
}
Result:
This person is not allowed to enter to a Bar
In this example, the condition is to check whether or not the value of the age variable is lesser or equal to the value 21 in the if statement. Because the variable’s value is equal to 20 the condition is true and so the instructions in the body of the if statement will run and so that’s why we got the result mentioned above.