comparison operators

“There’s no need for comparison. Be happy with yourself and find satisfaction in your work” Lailah Gifty Akita

Before reading this section, we’re assuming that you already read the operator section.

In real life we compare stuff and based on the result we decide to do something. For example we check the amount of money we have and if it was enough (like higher than 50K), we decide to buy a car or travel abroad etc. But if the money was less than 50K we might do something else instead.

As you can see, here we compared our money with the price of a car and based on that decided to buy it or let it go and instead travel or do something else if the money wasn’t in our expectation.

In C++ programming we often times need to compare two or more values (variables, constant values or the returned value from a function call etc.) and see if they are equal in value or one is larger than the other and based on the result, a set of instructions should either run or be skipped.

This is where comparison operators can help.

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. For example if-statement, while-loop, for-loop etc.

In order to explain these operators, we need to use one of the conditional-statements in the examples of this section. For this reason we chose the if-statement and we will start by an introduction to this statement and after that comes the operators.

Note: the explanation about if-statement in this section will be shallow but you can learn this statement deeply in the if-statement section.

The meaning that this statement has in real world also applies inside the C++ as well. For example in real world we use if statement to say things like: if I'm hungry, then I'll eat. Similarly in C++ we use if statement for checking one or more values and based on that we decide to run a set of instructions or skip it.

For example: if value of the variable that is named age is larger than 21, then print a message to the output that says “the age is higher than 21” otherwise print the message that says “the age is less than 21”.

Alright let’s see 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`: the `if` keyword is used to declare an if-statement. Simply
    when you want to create an if-statement, start with the keyword
    `if`.

  • `()`: after the `if` keyword comes the parentheses. Within these
    parentheses we put the condition that we want to check. This is where
    comparison-operators can help.

For example within this parentheses we can set age == 21 which means check and see if the age variable is equal in value to 21. If the result was true then instructions within the body of if-statement will run. Otherwise the body of the else statement is the one to run.

  • `{}`: the body of an `if-statement` starts from open brace `{`
    and ends at closing brace `}`. And within this body, we put the codes
    and instructions that want to be run if the condition of the
    `if-statement` was true.

  • `else`: the else keyword comes after the closing brace `}` of the
    `if-statement` and is used to set the instructions that should run if
    only the condition’s result of the `if-statement` was false.

Note: the body of else-statement is within the braces that comes after the else keyword. Also this else-statement does not have parentheses to put new condition.

Example:

#include <iostream>
using namespace std;
int main(){
int age = 20;
if (age >21){
cout<<"Yes the age is larger than 21"<<endl;
}else{
cout<<"The age is less than 21"<<endl;
}
return 0;
}

Output:

The age is less than 21

In this example we have a variable named age. We then run an if-statement to see if the value of this age variable is larger than 21 or not. Because the value of the age variable is actually 20 and is less than 21, the result of the condition within if-statement is false and so the body of this statement will be skipped and the body of else-statement will run instead. And that’s how we’ve got the message you saw in output.

Alright, now that we know how if-statement works, let’s jump into comparison-operators and explain them one by one:

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.

Equal `==` Operator:

The equal == operator is used to see whether or not two values are equal. These values (AKA operands) can be either variables, constant values, returned value from a function call or another expression that results a value.

Example:

#include <iostream>
using namespace std;
int main(){
int age = 20;
if (age ==20){
cout<<"The value of the age-variable is 20"<<endl;
}else{
cout<<"The value of the age-variable is not 20"<<endl;
}
return 0;
}

Output:

The value of the age-variable is 20

As you can see we compared the value assigned to the age variable with the constant value 20. And because they are equal in value, this comparison is true, and the body of the if-statement will run.

Note: as mentioned before, either the body of if or the body of else statement will run but not both. So because the instructions of if-statement in this example ran, the else statement is skipped.

This operator is on the opposite side of equal == operator. Basically if we use this operator in the condition of if-statement and the two values involved weren’t equal, the if-statement will result true and its body will run.

Example:

#include <iostream>
using namespace std;
int main(){
int age = 20;
if (age !=21){
cout<<"The value of the age-variable is not 21"<<endl;
}else{
cout<<"The value of the age-variable is 21"<<endl;
}
return 0;
}

Output: The value of the age-variable is not 21

In this example the comparison between the age variable and the value 21 shows that these two values are not equal and because we used Not-Equal != operator in the condition of the if-statement the result is true and its body executed.

Greater Than `>` Operator:

This operator is used to see if the value of the operand on the left side of this operator is larger compared to the operand on the right side and if it was then the result of the comparison is true otherwise the result is false.

Example:

#include <iostream>
using namespace std;
int main(){
int age = 20;
if (age >21){
cout<<"The value of the age-variable is larger than 21"<<endl;
}else{
cout<<"The value of the age-variable is less than 21"<<endl;
}
return 0;
}

Output:

The value of the age-variable is less than 21

In this example, because the value assigned to the age variable is less than 21, then the result of the age>21 comparison is false and so the body of else statement will run instead.

Less Than `<` Operator:

This operator is on the opposite side of Greater-Than > Operator. Which means this operator checks to see if the operand on the left side is lesser compared to the operand on the right side. If it was, then the result is true, otherwise false.

Example:

#include <iostream>
using namespace std;
int main(){
int age = 20;
if (age <21){
cout<<"The value of the age-variable is less than 21"<<endl;
}else{
cout<<"The value of the age-variable is larger than 21"<<endl;
}
return 0;
}

Output:

The value of the age-variable is less than 21

Because the value of the age variable is less than 21, the result of the age < 21 expression is true and so the body of the if statement will run.

Greater or Equal To `>=` Operator:

This operator is the combination of equal == and Greater-Than > operators.

Basically this operator will compare the two operands in both equality and also check to see if the left operand is larger than the right side operand. At the end, any of these two comparisons results true, the entire condition is true as well.

Example:

#include <iostream>
using namespace std;
int main(){
int height = 6;
if (height >=5){
cout<<"The height is more than 5 inches"<<endl;
}else{
cout<<"The height is less than 5 inches"<<endl;
}
return 0;
}

Output:

The height is more than 5 inches

In this example the value assigned to the height variable is not equal to 5 but it’s greater. So one condition which is being larger is true and it makes the end result true as well so the body of the if-statement will run.

Less Than or Equal To `<=` Operator:

This operator is the combination of equal `==` and Less-Than `<` operators.

Basically this operator will compare the two operands in both equality and check to see if the left operand is less than the right side operand. Any of these two comparisons results true, the entire condition is true as well.

Example:

#include <iostream>
using namespace std;
int main(){
int height = 6;
if (height <=5){
cout<<"The height is less than 5 inches"<<endl;
}else{
cout<<"The height is more than 5 inches"<<endl;
}
return 0;
}

Output:

The height is more than 5 inches

In this example the value assigned to the height variable is neither equal to 5 nor less than that. So none of the two conditions result true and for this reason the body of the else statement will run instead.