Lecture 08 - Programming Fundamentals
Lecture 08 - Programming Fundamentals
ARITHMETIC EXPRESSIONS
1
Lecture 08 Objectives
8.1 Introduction
8.2 Operators in C++
8.3 Summary
Lecture 08 8.1 Introduction
____________________________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education.
Lecture 8 Contents
8.1 Introduction
8.2 Operators in C++
• Operators precedence
• Sample algebraic and C++
expression
8.3 Summary
Lecture 8 8.2 Operators in C++
Assume, int a = 4, b = 5, d;
Addition + d = a + b 9
Substraction - d = b - 2 3
Multiplication * d = a * b 20
Division / d = a/2 2
Modulus % d = b%3 2
Lecture 8 8.2 Operators in C++
Assignment operators
Assume, int x = 4, y = 5, z = 8;
Assignment Sample Equivalent Value of variable
Operator Expression Expression after assignment
+= x += 5 x = x + 5 x = 9
-= y -= x y = y - x y = 1
*= x = x * z
x *= z x = 32
/= z /=2 z = z / 2 z = 4
%= y %=x y = y % x y = 1
Lecture 8 8.2 Operators in C++
Relational operators
Assume, int y = 6, x = 5;
Relational Operators Sample Expression Value
> y > x T
< y < 2 F
>= x >= 3 T
<= y <= x F
== x == 5 T
!= y ! = 6 F
Lecture 8 8.2 Operators in C++
Logical operators
|| OR expression1 || expression2
! NOT ! (expression)
T F !(x != 60)
Lecture 8 8.2 Operators in C++
Expression1 &&
Expression1 Expression2 Sample Expression
Expression2
F T F (z <= y) && (x == 4)
T F F (y != z) && (z < x)
T T T (z >= y) && (x != 3)
Lecture 8 8.2 Operators in C++
Expression1 ||
Expression1 Expression2 Sample Expression
Expression2
F T T (z <= y) || (x == 4)
T F T (y != z) || (z < x)
T T T (z >= y) || (x != 3)
Lecture 8 8.2 Operators in C++
Bitwise Operators
• Bitwise AND -> &
• Operand1 & Operand2
• Bitwise OR -> |
• Operand1 | Operand2
• Bitwise NOT -> ~
• ~Operand
Lecture 8 8.2 Operators in C++
Bitwise Operators
• Examples:
Lecture 8 8.2 Operators in C++
Operators precedence
____________________________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. (pp.52)
Lecture 8 8.2 Operators in C++
____________________________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. (pp.52)
Lecture 8 8.2 Operators in C++
Operators Associative
( ) Left to right
++ -- + - ! Right to left
* / % Left to right
+ - Left to right
< <= > >= Left to right
= = != Left to right
&& Left to right
| | Left to right
= *= += - = Right to left
/= %=
Lecture 8 8.2 Operators in C++
y = a * x * x + b * x + c;
____________________________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. (pp.52)
Lecture 8 8.2 Operators in C++
Example 1: Example 2:
int a=10,b=20,c=15,d=8,e; int a=15, b=6, c=5, d=4;
e=a*b/(-c*31%13)*d d *= ++b – a/3 + c
____________________________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. (pp.52)
Lecture 8 8.2 Operators in C++
____________________________________________________________________________________________________________________________________
I
Deitel, H.M. and Deitel, P.J (2013). C How to Program 7/E. United State of America: Pearson Education. (pp.52)
Lecture 8 8.2 Operators in C++
a) .
b) .
c) .
Lecture 8 8.2 Operators in C++
Solution 8.3:
a) (b*b)-(4*a*c)
b) (a+b)/(c+d)
c) 1/(1+(x*x))
Lecture 8 Contents
8.1 Introduction
8.2 Operators in C++
8.3 Summary
Lecture 8 8.3 Summary