0% found this document useful (0 votes)
6 views2 pages

Lab 4 5 Confusing The Assignment Operator With Equality Test

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)
6 views2 pages

Lab 4 5 Confusing The Assignment Operator With Equality Test

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/ 2

Lab 4.

5 Confusing the Assignment Operator (=) with the Equality


Operator (==)

Any Boolean expression evaluates to true or false. An assignment expression used in a


Boolean expression is evaluated after the assignment occurs. Any nonzero value evaluates to
true. An if statement erroneously consisting of an assignment statement evaluates to true
any time the value is not zero; otherwise, it evaluates to false. C++ does not show this as a
compile error. This error is a logical error.

Objectives
In this lab, you distinguish the assignment operator from an equality operator in a Boolean
expression.

After completing this lab, you will be able to:


• Recognize the problems related to confusing the assignment operator with the equality
operator.

Estimated completion time: 5–10 minutes


Lab 4.5 Steps: Confusing the Assignment Operator (=) with the
Equality Operator (==)

1. Write what the following statements display as output after the program executes:
int x = 8;
if (x == 10)
cout << “x is equal to 10\n”;
else
cout << “x is not equal to 10\n”;

2. The following code is the same as the code in Exercise 1 but uses the assignment operator
rather than the equality operator. Write what the following statements display as output after
the program executes:

int x = 8;
if (x = 10)
cout << “x is equal to 10\n”;
else
cout << “x is not equal to 10\n”;

You might also like