Relational and Logical Operators

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.";




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net