Saturday, 28 April 2012

Program Of Compare Operators In C++

#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int marks;
cout<<"Enter Marks=";
cin>>marks;
if(marks>=40)     // >= ,==,<= ,etc Comapare Operator
{
cout<<"Pass";
}
else
{
cout<<"Fail";
}
getch();
}
--------------------------------
  Define Compare Operator

There are 6 relational or compare operators:
OperatorSymbolFormOperation
Greater than>x > ytrue if x is greater than y, false otherwise
Less than<x < ytrue if x is less than y, false otherwise
Greater than or equals>=x >= ytrue if x is greater than or equal to y, false otherwise
Less than or equals<=x <= ytrue if x is less than or equal to y, false otherwise
Equality==x == ytrue if x equals y, false otherwise
Inequality!=x != ytrue if x does not equal y, false otherwise
You have already seen how all of these work, and they are pretty intuitive. Each of these operators evaluates to the boolean value true (1), or false (0).
Keep in mind that comparing floating point values using any of these operators is dangerous. This is because small rounding errors in the floating point operands may cause an unexpected result. See the section on floating point numbers for more details.
However, sometimes the need to do floating point comparisons is unavoidable. In this case, the less than and greater than operators (>, >=, <, and <=) are typically used with floating point values as normal. The operators will produce the correct result most of the time, only potentially failing when the two operands are almost identical. Due to the way these operators tends to be used, a wrong result typically only has slight consequences.



No comments:

Post a Comment