Game Development Reference
In-Depth Information
C++ also contains an inequality operator that can be thought of as a not equals operation. It can be
used as follows.
bool isNotEqual = number1 != number2;
As we can see, the inequality operator uses an exclamation mark before the equals symbol.
This operator provides true when the two values are not the same and false if they are the same.
Greater-Than Operators
C++ also allows us to work out if a value stored in a variable is greater than another. We can do this
as follows:
bool isGreater = number1 > number2;
We can also tell if a number is greater than or equal to another:
bool isGreaterThanOrEqual = number1 >= number2;
Less-Than Operators
We can work out if a value is less than another using the less-than operator:
bool isLessThan = number1 < number2;
As with greater-than, there is a less-than or equal to operator:
bool isLessThanOrEqual = number1 <= number2;
We will create some small programs to show off the relational operators just as we did with the
arithmetic operators.
Simple Comparison Calculators
Listing 3-6 shows the source code for a simple program that tests the equality of two values.
Listing 3-6. Using the Equality Operator
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Enter your first number: " << endl;
int number1 = 0;
cin >> number1;
 
Search WWH ::




Custom Search