Lab 4 1 Using Relational and Logical Operators to Evaluate Logical
Lab 4 1 Using Relational and Logical Operators to Evaluate Logical
A conditional statement is a control structure that allows a program to execute one of two
alternative selections. A one-way alternative selection is executed depending on whether a
condition is true. A two-way alternative selection is executed depending on whether the
condition is true or false. The program evaluates a condition by comparing two operands. In
C++, this condition is called a logical (Boolean) expression. Additionally, true and false are
logical (Boolean) values. A conditional statement uses relational operators to perform the
relational comparison, which applies to all three simple data types and to strings of the
string class. It also uses the ASCII collating sequence. When C++ evaluates a logical
expression, it returns a nonzero integer value if the logical expression evaluates to true; it
returns an integer value of 0 otherwise. In C++, any nonzero value is treated as true. Table
4-1 describes each relational operator.
Operator Description
= = Equal to
!= Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
Comparison of real numbers for equality should be avoided because only approximations of
real numbers are stored, and so the results of such a comparison may not be accurate.
When comparing strings, comparison is done character-by-character using the ASCII
collating sequence, starting with the first character until a mismatch or the last characters
have been compared. If all characters are the same until one string ends, the shorter string
is evaluated as less than the larger string.
Logical (Boolean) operators enable you to combine logical expressions. The three logical
operators are and (&&), or (||), and not (!). The binary logical operators (&&) and (||)
combine logical expressions. The unary logical operator (!) reverses the value of the logical
expression. The && operator evaluates to false unless both Boolean expressions are
true. The || operator evaluates to true unless both Boolean expressions are false.
Like mathematical expressions, relational expressions have an order of precedence for
evaluation. The associativity of these operators is from left to right, but parentheses can
override the precedence of these operators. After a program determines whether a
compound expression is true or false, the rest of the expression is not evaluated. This is
known as short-circuit evaluation. Table 4-2 lists the precedence of operators and the order
of evaluation.
Table 4-2 The Precedence of C++ Relational Operators
Operator(s) Precedence
!, +, – (unary operators) 1
*, /, % 2
+, – 3
<, <=, >=, > 4
==, != 5
&& 6
|| 7
= (assignment operator) Last
More recent versions of C++ contain a built-in data type, bool, which has the logical values
true and false. The identifier true has the value 1, and the identifier false has the
value 0.
Objectives
In this lab, you evaluate Boolean expressions using relational and Boolean operators.
Additionally, you learn to evaluate these expressions by the order (or precedence) of
operators.