Relational Operators (also known as Comparison Operators)in C++
There is often need to compare two values in program. Such as whether the value in one a variable is greater than the value in the other variable. Depending upon the situation we can perform different operation for different cases.Relational Operators (also known as Comparison Operators) are used to compare two values. The result of comparison is a boolean value.
The boolean value can either be true or false. In c and c++, 1 represents true and 0 represent false. However in c++ the data type bool has been introduced, which holds only two values either true or false. The value true in bool corresponds to 1 and 0 corresponds to false.
List of Relational Operators (aka Comparison Operators)
Operator | Operator's Name | Example | Result |
---|---|---|---|
< | less than | 5<10 | 1(true) |
<= | less than or equal to | 5<=10 | 1(true) |
> | greater than | 5>10 | 0(false) |
>= | greater than or equal to | 5>=10 | 0(false) |
== | equal to | 5==10 | 0(false) |
!= | not equal to | 5!=2 | 1(true) |
/* A c++ program example to demonstrate that the result of comparison is always a boolean value, either 1 or 0. The value 1 represents true and 0 represents false. */
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
cout << "5 <10" << setw (4) << ": " << (5 < 10) << endl;
cout << "5 <=10" << setw(3) << ": " << (5 <= 10) << endl;
cout << "5 >10" << setw (4) << ": " << (5 > 10) << endl;
cout << "5 >= 10" << setw (2) << ": " << (5 >= 10) << endl;
cout << "5 == 10" << setw (2) << ": " << (5 == 10) << endl;
cout << "5 != 10" << setw (2) << ": " << (5 != 10) << endl;
return 0;
}
This chapter is important to understand selection and looping statement. In coming chapters we will learn about selection and looping statement.
Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Want to learn more? View List Of All Chapters
I know this is quality based blogs along with other stuff.
ReplyDeletetutorial on c++
This comment has been removed by the author.
ReplyDelete