In addition to the arithmetic operators, C++ provides a variety of
other operators including six relational operators. A relational
operator allows two values to be compared, and it produces a boolean
value indicating their relationship. A boolean value is either
true
or false
indicating if the claim or relationship
is true or false.
== |
equality, returns true if and only if its operands are equal |
!= |
inequality, returns true if and only if its operands are different |
< |
less-than, returns true if and only if its left operand is less-than its right operand |
> |
greater-than, returns true if and only if its left operand is greater-than its right operand |
<= |
less-than-or-equal-to, returns true if and only if its left operand is not greater than its right operand |
>= |
greater-than-or-equal-to, returns true if and only if its left operand is not less than its right operand |
In C++, a boolean value is represented by the bool
type, so
these operators produce a bool
value. They are called the
relational operators because they are used to identify the
relationship between two values.
Add a line to your program that prints out these boolean values:
cout << "true is " << true << "\n" << "false is " << false << endl;Compile and execute your program.
Question #3.4.1: What does your program display for this output statement?
Depending on your compiler, you may actually get the boolean values
true
and false
to print out as words on the screen.
More likely, you will get integer values, probably 1
for
true
and 0
for false
. In C++, 0
is
taken to be false, and strictly speaking, any non-zero value is
true (although most compilers like to use the value 1
). When
reading and reporting the results of this experiment, translate any
integers into true
and false
.
Modify your program so that it
i
and j
from the keyboard.i
and j
using
each of the relational operators listed above.Compile and execute your program.
Question #3.4.2: Find values fori
andj
in each row to make the indicated relationship true:
Relationship i
j
i < j
i <= j
i == j
i != j
i >= j
i > j
Use your program to find and confirm your values.