Section 3.4 - CS 010A: Introduction To Computer Science For Science, Mathematics & Engineering I
Section 3.4 - CS 010A: Introduction To Computer Science For Science, Mathematics & Engineering I
3.4: Equality and relational operators ' zyBooks catalog ( Help/FAQ ) Hayoung Kwon %
Students:
"
Section 3.4 is a part of 2 assignments: Week 3 Participation Activities
% Requirements: PA
This assignment's due date has passed. Activity will still be recorded, but will not
count towards this assignment (unless the due date is changed). See this article for Entire class was due: 01/18/2021, 10:00 PM PST
more info.
Equality operators
An equality operator checks whether two operands' values are the same (==) or different (!=). Note that equality is ==, not just =.
An expression involving an equality operator evaluates to a Boolean value. A Boolean is a type that has just two values: true or false.
x == 3 is true
== a == b means a is equal to b
x == 4 is false
x != 3 is false
!= a != b means a is not equal to b
x != 4 is true
Feedback?
PARTICIPATION
ACTIVITY
3.4.1: Evaluating expressions that have equality operators.
1) x == 5
True
False
2) x == y
True
False
3) y != 7
True
False
4) y != 99
True
False
5) x != y
True
False
6) Is x = = y a valid expression?
Yes
No
Feedback?
PARTICIPATION
ACTIVITY
3.4.2: Creating expressions with equality operators.
1) numDogs is 0
numDogs 0
Feedback?
Relational operators
A relational operator checks how one operand's value relates to another, like being greater than.
Some operators like >= involve two characters. A programmer cannot arbitrarily combine the >, =, and < symbols; only the shown two-
character sequences represent valid operators.
x < 4 is true
< a < b means a is less than b
x < 3 is false
x > 2 is true
> a > b means a is greater than b
x > 3 is false
x <= 4 is true
<= a <= b means a is less than or equal to b x <= 3 is true
x <= 2 is false
x >= 2 is true
>= a >= b means a is greater than or equal to b x >= 3 is true
x >= 4 is false
Feedback?
PARTICIPATION
ACTIVITY
3.4.3: Evaluating equations having relational operators.
1) x <= 7
True
False
2) y >= 7
True
False
Feedback?
PARTICIPATION
ACTIVITY
3.4.4: Creating expressions with relational operators.
3) numCars is 5 or greater
numCars 5
Feedback?
In golf and miniature golf, each hole has a "par" indicating the normal number of strokes to get the ball in the hole. The following program
outputs special names for numbers below par. The program uses one relational operator, and several equality operators, in a multi-branch
if-else statement.
This program outputs special names for different numbers of strokes for a par 4 hole. Press Run to see the name for the input strokes
of 2. Try changing the input strokes to different values, and press Run again to see the name.
If desired, try extending the program to print "Bogey" for 5, and "Double bogey" for 6. Remember that equals is ==, not =.
Try typing "= 1" instead of "== 1" and see what happens. Try typing an unsupported operator, like =>, and see what happens.
2
Load default template...
1 #include <iostream>
2 using namespace std; Run
3
4 int main() {
5 int numStrokes;
6
7 cin >> numStrokes;
8
9 // Assumes "par 4"
10 if (numStrokes <= 0) {
11 cout << "Invalid entry." << endl;
12 }
13 else if (numStrokes == 1) {
14 cout << "Hole in 1!!!" << endl;
15 }
16 else if (numStrokes == 2) {
17 cout << "Eagle!" << endl;
18 }
19 else if (numStrokes == 3) {
Feedback?
CHALLENGE
ACTIVITY
3.4.1: Enter the output for the branches with relational and equality operators.
Start
1
#include <iostream>
using namespace std;
3
int main() {
int numEggs;
4
numEggs = 6;
b
if (numEggs <= 6) {
cout << "b" << endl; h
}
else {
cout << "e" << endl;
}
return 0;
}
1 2 3 4
Check Next
Feedback?
CHALLENGE
ACTIVITY
3.4.2: Equality and relational expressions.
Start
1
Write an expression that will cause "Foot or more" to print if the value of numInches is greater than or equal to 12.
2
1 #include <iostream>
2 using namespace std;
3 3
4 int main() {
5 int numInches = 0;
6
7 cin >> numInches; // Program will be tested with values: 11, 12, 13, 14.
8
9 if (/* Your code goes here */) {
10 cout << "Foot or more" << endl;
11 }
12 else {
13 cout << "Less than a foot" << endl;
14 }
15
16 return 0;
17 }
1 2 3
Check Next
Feedback?
The relational and equality operators work for integer, character, and Noating-point built-in types.
Comparing characters compares their ASCII numerical encoding.
Floating-point types should not be compared using the equality operators, due to the imprecise representation of Noating-point numbers, as
discussed in a later section.
The operators can also be used for the string type. Strings are equal if they have the same number of characters and corresponding
characters are identical. If string myStr = "Tuesday", then (myStr == "Tuesday") is true, while (myStr == "tuesday") is false because T differs
from t.
PARTICIPATION
ACTIVITY
3.4.5: Comparing various types.
Which comparison will compile AND consistently yield expected results? Variables have types denoted by their names.
1) myInt == 42
OK
Not OK
2) myChar == 'q'
OK
Not OK
3) myDouble == 3.26
OK
Not OK
4) myString == "Hello"
OK
Not OK
Feedback?
Common errors
Perhaps the most common error in C and C++ is to use = rather than == in an if-else expression, as in: if (numDogs = 9) { ... }. That code is
not a syntax error. The statement assigns numDogs with 9, and then because that value is non-zero, the expression is considered true. C's
designers allowed assignment in expressions to allow compact code, and use = for assignment rather than := or similar to save typing.
Many people believe those language design decisions were mistakes, leading to many bugs. Some modern compilers provide a warning
when = appears in an if-else expression.
Another common error is to use invalid character sequences like =>, !<, or <>, which are not valid operators.
PARTICIPATION
ACTIVITY
3.4.6: Watch out for assignment in an if-else expression.
1) numItems = 3;
if (numItems == 3) {
numItems = numItems + 1;
}
2) numItems = 3;
if (numItems = 10) {
numItems = numItems + 1;
}
Feedback?
CHALLENGE
ACTIVITY
3.4.3: If-else statement: Fix errors.
Start
Feedback?
CHALLENGE
ACTIVITY
3.4.4: If-else statement: Print senior citizen.
Write an if-else statement that checks patronAge. If 55 or greater, print "Senior citizen", otherwise print "Not senior citizen" (without
quotes). End with newline.
1 #include <iostream>
2 using namespace std; 1 test
3 passed
4 int main() {
5 int patronAge;
6 All tests
7 cin >> patronAge; passed
8
9 /* Your solution goes here */
10
11 return 0;
12 }
Run
Activity summary for assignment: Week 3 Participation Activities % (163 of 173 points)
Completion details $