3a Fop
3a Fop
Lecture 3a
Agenda
• Arithmetic operations
• Decision making : Equality and Relational
Operators
Assignment Operator
• An operator to give (assign) a value to a
variable.
• Denote as ‘=‘
• Only variable can be on the left side.
• An expression is on the right side.
• Variables keep their assigned values until
changed by another assignment statement or
by reading in a new value.
Arithmetic Operators
• Operators: +, -, * /
• For floating numbers, the result as same as Math
operations.
• Note on integer division: the result is an integer. 7/2
is 3.
• % (remainder or modulo) is the special operator just
for integer. It yields an integer as the result. 7%2 is
1.
• Both / and % can only be used for positive integers.
• Precedence rule is similar to Math.
Arithmetic Expressions
• Arithmetic operations can be used to express the
mathematic expression in C++:
b 2 4ac b *b 4* a *c
x( y z ) x * ( y z)
1
1 /( x * x x 3)
x x3
2
ab (a b) /(c d )
cd
Simple Flow of Control
• Three processes a computer can do:
– Sequential
expressions, insertion and extraction operations
– Selection (Branching)
if statement, switch statement
– Repetition/Iteration (Loop)
while loop, do-while loop, for loop
Relational Operators
are used in boolean expressions of form:
ExpressionA Operator ExpressionB
temperature > humidity
B * B - 4.0 * A * C > 0.0
abs (number) == 35
initial != ‘Q’
• Notes:
o == (equivalency) is NOT = (assignment)
o characters are compared alphabetically. However, lowercase letters are higher
ASCII value.
o An integer variable can be assigned the result of a logical expression
o You cannot string inequalities together:
Bad Code: 4<x<6 Good Code: (x > 4) &&(x < 6)
Relational Operators
int x, y ;
x = 4;
y = 6;
EXPRESSION VALUE
x<y true
x+2<y false
x != y true
x + 3 >= y true
y == x false
y == x+2 true
y=x+3 7
y=x<3 0
y=x>3 1
Logical Operators
are used in boolean expressions of form:
ExpressionA Operator ExpressionB
A || B (true if either A or B or both are true. It is false otherwise)
A && B (true if both A and B are true. It is false otherwise)
or
Operator Expression
!A (true if A is false. It is false if A is true)
Notes:
Highest precedence for NOT, AND and OR are low precedence.
Associate left to right with low precedence. Use parenthesis to override priority or for
clarification
– x && y || z will evaluate “x && y ” first
– x && (y || z) will evaluate “y || z” first
Logical Operators
int age ;
bool isSenior, hasFever ;
float temperature ;
age = 20;
temperature = 102.0 ;
isSenior = (age >= 55) ;
cout <<“isSenior Result”<< isSenior <<endl; // isSenior is false
hasFever = (temperature > 98.6) ;
cout <<“hasFever Result”<< hasFever <<endl; // hasFever is true
EXPRESSION VALUE
isSenior && hasFever false
isSenior || hasFever true
!isSenior true
!hasFever false
Precedence Chart
• ++, --, !, - (unary minus), + (unary plus) Highest
• *, /, %
• + (addition), - (subtraction)
• <<, >>
• <, <=, >, >=
• ==, !=
• &&
• ||
• = Lowest
Addition Code
Output
Average Marks Calculate
Output
Code Task
Write a C++ program to calculate the roots of a
quadratic equation (ax2 + bx +c = 0)