| by Arround The Web | No comments

Relational Operators in C++

C++ provides different operators to do different tasks easily and concisely like the arithmetic operators, relation operators, assignments, bitwise operators, and many others. When we discuss the relational operator, we define it as the operator that is used to compare two values. In the relation operator, when comparing two values or expressions, the comparison yields a Boolean value (either true or false). True is typically represented by “1”, and false is typically represented by 0.

The relational operators are binary operators that work with two values or operands. The C++ language facilitates us with six different relational operators which are greater than “>”, greater than equal to “>=”, less than “<”, less than equal to “<=”, equal to “==”, and not equal to “!=”.

Example 1:

Here, we will utilize this code’s equal to “==” operator. We include the “iostream” to utilize the functions that are contained in this header file. Then, the “std” namespace is placed and invoke the “main()” method. Underneath this, we initialize the “value1” with the value of “44”, “value2” with “25”, and “44” is assigned to the “value3” variable. The data type of all variables is “int”.

Now, we apply the “==” operator between “value1” and “value2”. It checks whether “44 == 25” is correct or not. If they are equal, it renders “1”. Otherwise, it renders “0”. Then, in the following “cout”, we place “==” between “value1” and “value3” which checks “44 == 44”. If yes, it displays “1”. Otherwise, it displays “0”. After this, the “==” operator is used to work with “value2” and “value3″. It determines whether or not “25 == 44”. Here, it renders “1” if they are equal and renders “0” otherwise.

Code 1:

#include <iostream>
using namespace std;
int main()
{
int value1 = 44;
int value2 = 25;
int value3 = 44;
cout << "The result of value1 == value2 is "  << (value1 == value2) << endl;
cout << "The result of value1 == value3 is "  << (value1 == value3) << endl;
cout << "The result of value2 == value3 is "  << (value2 == value3) << endl;
return 0;
}

 
Output:

Here, we get the “0” and “1” as the outcome of applying the “==” relational operator. It shows “0” where the result is “not true” and “1” where the result is “true”.

Example 2:

Here, we set the values of “a” and “b” to 11 and 2, respectively and “33” is now allocated to the “c” variable.  We now use the “<” operator to compare “a” and “b”. It determines whether or not “11 < 2”. Here, it renders “1” if “a” is less than “b”, and “0” otherwise. Then, to ensure that “11 < 33”, we insert “<” between “a” and “c” in the ensuing “cout”. If so, “1” is displayed. If not, “0” is displayed.

After that, “b” and “c” are worked with using the “<” operator. It decides whether 2 is less than 33 or not. If it is, it renders “1”. If not, it renders “0”.

Code 2:

#include <iostream>
using namespace std;
int main()
{
int a = 11;

int b = 2;
int c = 33;
cout << "The result of a < b is "  << (a < b) << endl;
cout << "The result of a < c is "  << (a < c) << endl;
cout << "The result of b < c is "  << (b < c) << endl;
return 0;
}

 
Output:

Applying the relational operator “<” in this case yields the values of “0” and “1” which indicate the “not true” and “true” outcomes, respectively.

Example 3:

Now, in this code, “x” and “y” are set to 21 and 12, respectively, and the value of “93” is assigned to the “z” variable. The data type for each variable is “int”. Now, let’s compare “x” and “y” using the “>” operator. It decides if “21 > 12” is true or not. In this case, “0” is rendered if “x” is not greater than “y”, and renders “1” otherwise.

Then, in the next “cout”, we add “>” between “x” and “z” to make sure that “21 > 93”. Then, “1” will appear if that case is true. Otherwise, “0” is shown. The “>” operator is then used to manipulate “y” and “z”. It determines whether or not 12 is greater than 93. It outputs “1” if it is and “0” if it is not.

Code 3:

#include <iostream>
using namespace std;
int main()
{
int x = 21;
int y = 12;
int z = 93;
cout << "The result of x > y is "  << (x > y) << endl;
cout << "The result of x > z is "  << (x > z) << endl;
cout << "The result of y < z is "  << (y > z) << endl;
return 0;
}

 
Output:

In this instance, we use the relational operator “>” results in the numbers “0” and “1”, denoting the “false” and “true” outcomes, respectively.

Example 4:

Here, we utilize the “>=” relational operator. The variables that we initialize here are “mynum1”, “mynum2”, and “mynum3” with the values of “90”, “64”, and “22”, respectively. Then, we insert the “cout” in which we place “mynum1 >= mynum2” which checks that “90 >= 64”. If it is, “1” is rendered. Otherwise, “0” is rendered as the “false” outcome.

Underneath this, we place “mynum1 >= mynum3” which checks “90 >= 22”. If no, it displays “0”. If yes, it displays “1” as the outcome. Now, we utilize this “>=” relational operator again between the “mynum2” and “mynum3” which checks if “64 >= 90”is true or not. This returns “1” or “0” according to the condition as the outcome.

Code 4:

#include <iostream>
using namespace std;
int main()
{
int mynum1 = 90;
int mynum2 = 64;
int mynum3 = 22;
cout << "The result of 90 >= 64 is "  << (mynum1 >= mynum2) << endl;
cout << "The result of 90 >= 22 is "  << (mynum1 >= mynum3) << endl;
cout << "The result of 64 >= 90 is "  << (mynum2 >= mynum1) << endl;
return 0;
}

 
Output:

Here, we can see that “1” renders as the result of “90 >=64” where “90” is greater than the “64” condition which is true. This is the outcome of utilizing the “>=” relational operator.

Example 5:

We use the relational operator “<=” in this case. The “i”, “j” and “k” variables are initialized with the following values: “38”, “42”, and “21” in that order. Next, we add the “cout” function and enter “i <= j” to check if “38 <= 42”. If it is, “1” is rendered. If not, “0” is rendered as the “false” result. Beneath this, we have “i <= k” which checks if “38 <= 21”. If not, it displays “0”. If it is, it displays “1.”

To verify whether or not “42 <= 38”, we utilize the relational operator “<=” between “j” and “i” once more. This result is either “1” or “0”, depending on the criteria.

Code 5:

#include <iostream>
using namespace std;
int main()
{
int i = 38;
int j = 42;
int k = 21;
cout << "The result of 38 <= 42 is "  << (i <= j) << endl;
cout << "The result of 38 <= 21 is "  << (i <= k) << endl;
cout << "The result of 42 <= 38 is "  << (j <= i) << endl;
return 0;
}

 
Output:

Here, we can observe that when the criterion is met, “1” renders as the outcome. Otherwise, “0” renders. The result of using the relational operator “<=” is as follows:

Example 6:

Now, we utilize the “!=” operator here. For this, we initialize “m”, “n”, and “o” as the variables with the “int” data type and then assign “85”, “92”, and “81” to these variables. Below this, we place the “m != n” condition in the “cout” which checks “85 != 92”. If it is true, “1” is displayed. Otherwise, “0” is displayed here.

In the same way, it checks “m != o” which determines whether “85” is not equal to “81”. Then, we put the “n != m” case which checks “92 != 85” and render the outcome accordingly.

Code 6:

#include <iostream>
using namespace std;
int main()
{
int m = 85;
int n = 92;
int o = 81;
cout << "The result of 85 != 92 is "  << (m != n) << endl;
cout << "The result of 85 != 81 is "  << (m != o) << endl;
cout << "The result of 92 != 85 is "  << (n != m) << endl;
return 0;
}

 
Output:

The following results from applying the relational operator “!=” in the code:

Conclusion

We explored the need for the relational operator in C++ programming whenever we need a comparison between two values. These operators return “0” and “1” according to the criteria as the outcome. We explored six relational operators in this guide and utilized them in our codes.

Share Button

Source: linuxhint.com

Leave a Reply