0% found this document useful (0 votes)
5 views

Lab 4 1 Using Relational and Logical Operators to Evaluate Logical

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 4 1 Using Relational and Logical Operators to Evaluate Logical

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 4.

1 Using Relational and Logical Operators to Evaluate Logical


(Boolean) Expressions

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.

Table 4-1 C++ Relational Operators

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.

After completing this lab, you will be able to:


• Evaluate logical expressions formed by the relationship of two operands.
• Use relational operators to evaluate all three simple data types and strings.
• Use logical (Boolean) operators to evaluate combined logical expressions.
• Evaluate logical expressions by the correct order of precedence.
• Recognize logical expressions that can be short-circuit evaluations.

Estimated completion time: 30–40 minutes


Lab 4.1 Steps: Using Relational and Logical Operators to Evaluate
Logical (Boolean) Expressions
Evaluate the logical (Boolean) expression in the Expression column, and circle all possible
answers in the Result column. For char and string types, use the ASCII collating
sequence.

Expression Result (Circle all possible answers)

1. int num1 = 3, num2 = 2; True False short-circuit


(num1 > num2)
2. double hours = 12.8; True False short-circuit
(hours > 40.2)
3. int funny = 7; True False short-circuit
(funny !=1)
4. char letter = ’A’; True False short-circuit
(’a’ < letter)
5. short count = 1; True False short-circuit
count <=4;
6. double y = -2.3; True False short-circuit
y >=0.0;
Expression Result (Circle all possible answers)

7. ! (false || false) || true True False short-circuit

8. false && true && false True False short-circuit

9. ! false || (false || ! true) True False short-circuit

10. false || (true && (false ||


True False short-circuit
false) )

11. 1 || ! true && false True False short-circuit

12. ! (false || False) || false True False short-circuit

13. true && false && false True False short-circuit

14. ! false || (false || ! true) True False short-circuit

15. false || (false && (false ||


True False short-circuit
true) )
16. 0 || ! false && false True False short-circuit

Expression Result (Circle all possible answers)


17. string name1 = Aaron ; True False short-circuit
string name2 = aaron ;
(name1 == name2)
18. string name1 = Aaron ; True False short-circuit
string name2 = Aardvark ;
(name1 <= name2)

19. bool flag = true; True False short-circuit


int a = 2, b = 5, c = 10;
(a * b <= c && ! flag)
20. char letter = ’A’; True False short-circuit
string word = A ;
(letter == word)

You might also like