Here, the program discusses the relational operators in C++. These relational operators are used in decision-making and loops.
In computer science, a relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. These include numerical equality (e.g., 5 = 5) and inequalities (e.g., 4 ≥ 3).
In C++, Relational Operators are used for comparing two or more numerical values. C++ has different types of Operators which are used for carrying out numerous functions in the program. One such type used is the Relational Operator. We use Relational Operators for the decision-making process.
For example,
// checks if a is greater than b
a > b;
Here, > is a relational operator. It checks if a is greater than b or not.
If the relation is true, it returns 1 whereas if the relation is false, it returns 0.
Operator Meaning Example
== Is Equal To 3 == 5 gives us false
!= Not Equal To 3 != 5 gives us true
> Greater Than 3 > 5 gives us false
< Less Than 3 < 5 gives us true
>= Greater Than or Equal To 3 >= 5 give us false
<= Less Than or Equal To 3 <= 5 gives us true
Declare two integer type variables a and b and assign two values to the variables; Declare another Boolean type variable result to store the result. Check whether the value entered to a is equal to the value entered for b by using the operator ‘ ==’. If true then return a value 1 otherwise return a value 0. Check whether the value entered to a is not equal to the value entered for b by using the operator ‘ !=’. If true then return a value 1 otherwise return a value 0. Check whether the value entered to a is greater than the value entered for b by using the operator ‘ >’. If true then return a value 1 otherwise return a value 0. Check whether the value entered to a is less than the value entered for b by using the operator ‘ <’. If true then return a value 1 otherwise return a value 0. Check whether the value entered to a is greater than or equal to the value entered for b by using the operator ‘ >=’. If true then return a value 1 otherwise return a value 0. Check whether the value entered to a is less than or equal to the value entered for b by using the operator ‘ <=’. If true then return a value 1 otherwise return a value 0. Display the result on the screen
Algorithm
Step 1: Call the header file iostream
.
Step 2: Use the namespace std.
Step 3: Open the integer type main function; int main();
Step 4: Declare two integer variables a and b and assign two values; int a = 3; int b = 5;
Step 5: Declare a variable result to store the status of the result;
Step 6: Check for a == b; return 1 for true and 0 for false;
Step 7: Check for a != b; return 1 for true and 0 for false;
Step 8: Check for a > b; return 1 for true and 0 for false;
Step 9: Check for a < b; return 1 for true and 0 for false;
Step 10: Check for a >= b; return 1 for true and 0 for false;
Step 11: Check for a >= b; return 1 for true and 0 for false;
Step 12: Print the status of the variable result for every operators;
Step 13: Exit;
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 3;
b = 5;
bool result;
result = (a == b); // false
cout << "3 == 5 is " << result << endl;
result = (a != b); // true
cout << "3 != 5 is " << result << endl;
result = a > b; // false
cout << "3 > 5 is " << result << endl;
result = a < b; // true
cout << "3 < 5 is " << result << endl;
result = a >= b; // false
cout << "3 >= 5 is " << result << endl;
result = a <= b; // true
cout << "3 <= 5 is " << result << endl;
return 0;
}
3 == 5 is 0 3 != 5 is 1 3 > 5 is 0 3 < 5 is 1 3 >= 5 is 0 3 <= 5 is 1