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

Lecture 08 - Programming Fundamentals

The document discusses arithmetic expressions in C++. It covers various operators like arithmetic, assignment, relational, logical, increment/decrement, and bitwise operators. It provides examples of using these operators and the order of precedence when evaluating expressions. The key topics are the use of operators to perform mathematical or logical manipulations, operator precedence rules, and writing expressions using C++ operators.

Uploaded by

Saad Qayyum
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Lecture 08 - Programming Fundamentals

The document discusses arithmetic expressions in C++. It covers various operators like arithmetic, assignment, relational, logical, increment/decrement, and bitwise operators. It provides examples of using these operators and the order of precedence when evaluating expressions. The key topics are the use of operators to perform mathematical or logical manipulations, operator precedence rules, and writing expressions using C++ operators.

Uploaded by

Saad Qayyum
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Lecture 08:

ARITHMETIC EXPRESSIONS

1
Lecture 08 Objectives

In this lecture, we will learn about:

• Built-in C++ operators for specific


mathematical or logical manipulations.

• The evaluation of operators in arithmetic


expressions according to operator precedence

• Writing expressions using the mathematical


operators.
Lecture 08 Contents

8.1 Introduction
8.2 Operators in C++
8.3 Summary
Lecture 08 8.1 Introduction

• An expression is a programming statement that has a value.

• Expressions include arithmetic expressions that express the


results in numeric values i.e. integer and floating-point
numbers.

• Expressions comprise operators and operands.


• Operator describes the operation to be performed
• The operation is performed on the operands
Lecture 08 8.1 Introduction

• Most C++ programs perform calculations using the arithmetic


operators

____________________________________________________________________________________________________________________________________
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++

• An operator is a symbol that tells the compiler to perform


specific mathematical or logical manipulations on data.

• Built-in C++ operators are of following types:


• Arithmetic operators
• Assignment operators
• Relational operators
• Logical operators
• Increment / Decrement operators
• Bitwise operators
Lecture 8 8.2 Operators in C++
Arithmetic operators

Assume, int a = 4, b = 5, d;

Arithmetic Value of d after


Operation C++ Expression
Operator assignment

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

Logical Operators Name Sample Operation

&& AND expression1 && expression 2

|| OR expression1 || expression2

! NOT ! (expression)

Expression !Expression Sample Expression


Assume,
int x = 50; F T !(x == 60)

T F !(x != 60)
Lecture 8 8.2 Operators in C++

Example: Assume, int x = 4, y = 5, z = 8;

Expression1 &&
Expression1 Expression2 Sample Expression
Expression2

F F F (y > 10) && (z <= x)

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++

Example: Assume, int x = 4, y = 5, z = 8;

Expression1 ||
Expression1 Expression2 Sample Expression
Expression2

F F F (y > 10) || (z <= x)

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++

Increment / Decrement operators


Sample Equivalent
Operator Explanation
Name Expression Expression

Increment a by 1, then use


a = a +1
++a the new value of a in the
++ preincrement   expression in which a is
a += 1 being used
Use the current value of a
a = a +1
postincrement a++ in the expression in which
++   a is being used, then
a += 1 increment a by 1
Decrement a by 1, then
a = a -1
predecrement --a use the new value of a in
--   the expression in which a
a -= 1 is being used
Use the current value of a
postdecrement
a = a -1
a-- in the expression in which
--   a is being used, then
a -= 1 decrement a by 1
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

• Parentheses are used in C++ expressions in the same


manner as in algebraic expressions.

• For example, to multiply a with the quantity b + c, we


write a * ( b + c ).

• C++ rules of operator precedence are generally the same


as those in algebra.

____________________________________________________________________________________________________________________________________
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++

• The rules of operator precedence specify the order C++ uses


to evaluate expressions from left to right.
• Summarized rules of operator precedence for the operators:

____________________________________________________________________________________________________________________________________
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++

• Example : Evaluation of the operator precedence rules

y = a * x * x + b * x + c;

• The circled numbers under the statement indicate the order


in which C performs the operations

• Suppose variables a, b, c and x in the preceding second-


degree polynomial are initialized as follows:
a = 2 b = 3 c = 7 x = 5
____________________________________________________________________________________________________________________________________
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++

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

1. e=a*b/(-15*31%13)*d 1. d *= ++b – a/3+ c


2. e=a*b/(-465%13)*d 2. d*=7- a/3+c
3. e=a*b/(-10)*d 3. d*=7- 5+c
4. e=200/(-10)*d 4. d*=2 + c
5. e= -20*d 5. d*= 7
6. e= -160 6. d = d*7
7. d = 28
Lecture 8 8.2 Operators in C++

Activity 8.1: Solve the following expressions.


Lecture 8 8.2 Operators in C++

Activity 8.2: Solve the following expressions.


Lecture 8 8.2 Operators in C++

Assume, int a = 9, b = 2, c = 6, d = 10;


solve the statement,
a * b / c + 4 > d != 3 * c % a;
based on the operator precedence. Show your steps.
Solution:
step operator
0 9 * 2/6+4>10 != 3 * 6 % 9
1 * 18 /6+4>10 != 3 * 6 % 9
2 / 3 +4>10 != 3 * 6 % 9
3 * 3 +4>10 != 18 % 9
4 % 3 +4>10 != 0
5 + 7 >10 != 0
6 > 0 != 0
7 != 0
Lecture 8 8.2 Operators in C++
Sample algebraic and C++ Expressions

• Example: The following expression calculates the arithmetic


mean (average) of five terms.

• The parentheses are required to group the additions


because division has higher precedence than addition.
• The entire quantity ( a + b + c + d + e ) should be
divided by 5.
• If the parentheses are erroneously omitted, we obtain
a + b + c + d + e / 5, which evaluates incorrectly
as:

____________________________________________________________________________________________________________________________________
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: The following expression is the equation of a


straight line:

• No parentheses are required because the multiplication


evaluated first as its precedence higher than addition.

____________________________________________________________________________________________________________________________________
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++

Activity 8.3: Write the following algebraic expressions as a C+


+ statement.

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

• Computation of the data needs specific mathematical or


logical manipulations.

• The arithmetic expressions are operated according to


the rules of operator precedence.

• Parenthesis are often required to convert algebraic


expressions into C++ expressions

You might also like