The relational and logical operators are used to produce Boolean (true/false) results and are often used together. In C/C++, any nonzero number evaluates as true. Zero is false. In C++, the outcome of the relational and logical operators is of type bool. In C, the outcome is a zero or nonzero integer. The relational operators are listed here:
Operator |
Meaning |
---|---|
> |
Greater than |
>= |
Greater than or equal |
< |
Less than |
<= |
Less than or equal |
== |
Equal |
!= |
Not equal |
The logical operators are shown here:
Operator |
Meaning |
---|---|
&& |
AND |
|| |
OR |
! |
NOT |
The relational operators compare two values, producing a Boolean result. The logical operators connect two Boolean values or, in the case of !, reverse a value. The precedence of these operators is shown here:
Precedence |
Operators |
---|---|
Highest |
! |
> >= < <= |
|
== != |
|
&& |
|
Lowest |
|| |
As an example, the following if statement evaluates to true and prints the line x is less than 10:
x = 9; if(x < 10) cout << "x is less than 10";
However, in the following example, no message is displayed because both operands associated with && must be true for the outcome to be true:
x = 9; y = 9; if(x < 10 && y > 10) cout << "This will not print.";