SlideShare a Scribd company logo
Arithmetic Operators and Order of Precedence Intro to Programming
MUHAMMAD HAMMAD WASEEM 1
Arithmetic Operators
The arithmetic operators are the symbols that represent arithmetic operations. These are used
in arithmetic expressions. Each arithmetic operators upon two numeric values (constants or variables)
and return a value.
The following arithmetic operators are used in C++.
 Addition (+)
 Subtraction (-)
 Multiplication (*)
 Division (/)
 For remainder (%)
All arithmetic operators, except the remainder operator, are used for all type of numeric data.
A C arithmetic instruction consists of a variable name on the left hand side of = and variable
names & constants on the right hand side of =. The variables and constants appearing on the right hand
side of = are connected by arithmetic operators like +, -, *, and /.
The variables and constants together are called ‘operands’ that are operated upon by the
‘arithmetic operators’ and the result is assigned, using the assignment operator, to the variable on left-
hand side.
A C arithmetic statement could be of three types. These are as follows:
Integer mode arithmetic statement - This is an arithmetic statement in which all operands are
either integer variables or integer constants.
Ex.: int i, king, issac, noteit;
i = i + 1;
king = issac * 234 + noteit - 7689 ;
Real mode arithmetic statement - This is an arithmetic statement in which all operands are either
real constants or real variables.
Ex.: float qbee, antink, si, prin, anoy, roi;
qbee = antink + 23.123 / 4.5 * 0.3442 ;
si = prin * anoy * roi / 100.0 ;
Mixed mode arithmetic statement - This is an arithmetic statement in which some of the
operands are integers and some of the operands are real.
Ex.: float si, prin, anoy, roi, avg;
int a, b, c, num ;
si = prin * anoy * roi / 100.0 ;
avg = ( a + b + c + num ) / 4 ;
It is very important to understand how the execution of an arithmetic statement takes place.
Firstly, the right hand side is evaluated using constants and the numerical values stored in the variable
names. This value is then assigned to the variable on the left-hand side.
Arithmetic Operators and Order of Precedence Intro to Programming
MUHAMMAD HAMMAD WASEEM 2
Though Arithmetic instructions look simple to use one often commits mistakes in writing them.
Let us take a closer look at these statements. Note the following points carefully.
a) C allows only one variable on left-hand side of =. That is, z=k*l is legal, whereas k*l=z is illegal.
b) In addition to the division operator C also provides a modular division operator. This operator returns
the remainder on dividing one integer with another. Thus the expression 10 / 2 yields 5, whereas, 10
% 2 yields 0.
Note: the modulus operator (%) cannot be applied on a float. Also note that on using % the sign of the remainder is
always same as the sign of the numerator. Thus –5 % 2 yields –1, whereas, 5 % -2 yields 1.
c) An arithmetic instruction is often used for storing character constants in character variables.
char a, b, d ;
a = 'F';
b = 'G';
d = '+';
When we do this the ASCII values of the characters are stored in the variables. ASCII values are
used to represent any character in memory. The ASCII values of ‘F’ & ‘G’ are 70 & 71 (refer ASCII Table).
d) Arithmetic operations can be performed on ints, floats and chars.
char x, y ;
int z ;
x = 'a';
y = 'b';
z = x + y;
Thus the statements, are perfectly valid, since the addition is performed on the ASCII values of
the characters and not on characters themselves. The ASCII values of ‘a’ and ‘b’ are 97 and 98, and hence
can definitely be added.
e) No operator is assumed to be present. It must be written explicitly. In the following example, the
multiplication operator after b must be explicitly written.
a = c.d.b(xy) //usual arithmetic statement
b = c * d * b * (x * y) //C statement
f) Unlike other high level languages, there is no operator for performing exponentiation operation.
Thus following statements are invalid.
a = 3 ** 2;
b = 3 ^ 2;
Integer and Float Conversions
In order to effectively develop C programs, it will be necessary to understand the rules that are
used for the implicit conversion of floating point and integer values in C. These are mentioned below.
Note them carefully.
a) An arithmetic operation between an integer and integer always yields an integer result.
b) An operation between a real and real always yields a real result.
c) An operation between an integer and real always yields a real result. In this operation the integer is
first promoted to a real and then the operation is performed. Hence the result is real.
I think a few practical examples shown in the following figure would put the issue beyond doubt.
Arithmetic Operators and Order of Precedence Intro to Programming
MUHAMMAD HAMMAD WASEEM 3
Class Practice:
It has been assumed that k is an integer variable and a is a real variable.
Order of Precedence of Operation
The priority or precedence or order in which the operations in an arithmetic statement are
performed is called the order of precedence. It is also known as hierarchy of operations. While executing
an arithmetic statement, which has two or more operators, we may have some problems as to how
exactly does it get executed. For example, does the expression 2 * x - 3 * y correspond to (2x)-(3y) or to
2(x-3y)? Similarly, does A / B * C correspond to A / (B * C) or to (A / B) * C? To answer these questions
satisfactorily one has to understand the ‘hierarchy’ of operations.
In an expression in C/C++, the operations are performed in the following order:
1. All multiplications, divisions and modular division are performed first from left to right.
2. All additions and subtractions are then performed from left to right.
3. If parenthesis are used in an expression, the expressions within parenthesis are first
computed from left to right.
4. When parenthesis are used within parenthesis, the expression within innermost parenthesis
is evaluated first.
5. Assignment is done at the end.
Arithmetic Operators and Order of Precedence Intro to Programming
MUHAMMAD HAMMAD WASEEM 4
For Example (4-(3*5)) + 2 is evaluated as follows:
1. (3*5) is computed and returns value of 15
2. 4-15 is computed and returns value of -11
3. -11+2 is computed and returns value of -9
Example 1: Determine the hierarchy of operations and evaluate the following expression:
i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8
Stepwise evaluation of this expression is shown below:
i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8
i = 6 / 4 + 4 / 4 + 8 - 2 + 5 / 8 operation: *
i = 1 + 4 / 4 + 8 - 2 + 5 / 8 operation: /
i = 1 + 1+ 8 - 2 + 5 / 8 operation: /
i = 1 + 1 + 8 - 2 + 0 operation: /
i = 2 + 8 - 2 + 0 operation: +
i = 10 - 2 + 0 operation: +
i = 8 + 0 operation : -
i = 8 operation: +
Note that 6 / 4 gives 1 and not 1.5. This so happens because 6 and 4 both are integers and
therefore would evaluate to only an integer constant. Similarly 5 / 8 evaluates to zero, since 5 and 8 are
integer constants and hence must return an integer value.
Example 2: Determine the hierarchy of operations and evaluate the following expression:
kk = 3 / 2 * 4 + 3 / 8 + 3
Stepwise evaluation of this expression is shown below:
kk = 3 / 2 * 4 + 3 / 8 + 3
kk = 1 * 4 + 3 / 8 + 3 operation: /
kk = 4 + 3 / 8 + 3 operation: *
kk = 4 + 0 + 3 operation: /
kk = 4 + 3 operation: +
kk = 7 operation: +
Note that 3 / 8 gives zero, again for the same reason mentioned in the previous example.

More Related Content

PPTX
Windows installation
Zeeshan_5858
 
PPTX
The-Solar-System, About The solar system, In the Solar System
Sai Sanketh Reddy Kolli
 
PPT
Network operating systems
Sachin Awasthi
 
PPTX
Load runner & win runner
Himanshu
 
PPTX
INSTALLING SOFTWARE
EchelleOgatis
 
PPTX
Stress Testing -CPU -Stress-Testing.pptx
iankeithsomoza
 
PDF
Assembling and disassembling pc. pdf
JohnRebenRequinto1
 
Windows installation
Zeeshan_5858
 
The-Solar-System, About The solar system, In the Solar System
Sai Sanketh Reddy Kolli
 
Network operating systems
Sachin Awasthi
 
Load runner & win runner
Himanshu
 
INSTALLING SOFTWARE
EchelleOgatis
 
Stress Testing -CPU -Stress-Testing.pptx
iankeithsomoza
 
Assembling and disassembling pc. pdf
JohnRebenRequinto1
 

What's hot (20)

PPTX
Computer maintenance 1 lesson 1
abbas mohd
 
PPT
How to install windows 10
Annu Ahmed
 
PPTX
Installing and configuring computer System
iankeithsomoza
 
DOCX
Computer Fundamentals
Vikram Nandini
 
PPTX
CPU and Storage devices
Adeela Armaghan
 
PDF
Computer Operations & Packages
Makaha Rutendo
 
PPTX
Microsoft windows command prompt
Abdulqadir001
 
PPTX
INSTALL APLLICATION SOFTWARE.pptx
karmaYonten5
 
PPTX
Computer operation
Munim Chaudhry
 
PPT
Parts of a Computer
Matt Shea
 
PPTX
OHS in CSS Grade 10
nanette sansano
 
PPTX
Introduction to internet.
Anish Thomas
 
PPTX
Introduction and brief history of computers
DIrectorate of Information Technology, Govt. of KPK
 
PPTX
History of the internet
Amal Jith
 
PDF
Software and hardware
meryy21
 
PDF
Operating Systems Basics
nishantsri
 
PPTX
5 pc maintenance
Rheigh Henley Calderon
 
PPT
Computer maintenance
Danladi Gambo
 
PPSX
Computer repair and maintenance
Cornelius Micah
 
Computer maintenance 1 lesson 1
abbas mohd
 
How to install windows 10
Annu Ahmed
 
Installing and configuring computer System
iankeithsomoza
 
Computer Fundamentals
Vikram Nandini
 
CPU and Storage devices
Adeela Armaghan
 
Computer Operations & Packages
Makaha Rutendo
 
Microsoft windows command prompt
Abdulqadir001
 
INSTALL APLLICATION SOFTWARE.pptx
karmaYonten5
 
Computer operation
Munim Chaudhry
 
Parts of a Computer
Matt Shea
 
OHS in CSS Grade 10
nanette sansano
 
Introduction to internet.
Anish Thomas
 
Introduction and brief history of computers
DIrectorate of Information Technology, Govt. of KPK
 
History of the internet
Amal Jith
 
Software and hardware
meryy21
 
Operating Systems Basics
nishantsri
 
5 pc maintenance
Rheigh Henley Calderon
 
Computer maintenance
Danladi Gambo
 
Computer repair and maintenance
Cornelius Micah
 
Ad

Similar to [ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence (20)

PDF
ICP - Lecture 5
Hassaan Rahman
 
PPT
chap-3-operators.ppt it has all maths problems as comp operation is concerned...
kiokocurtis
 
PPTX
programing in c PPT Gaurav Nautiyal.pptx
Hacker301428
 
PPTX
Operators and expressions
vishaljot_kaur
 
PDF
Arithmetic instructions
Learn By Watch
 
PPTX
C - programming - Ankit Kumar Singh
AnkitSinghRajput35
 
PPT
Operation and expression in c++
Online
 
PDF
Chapter 13.1.2
patcha535
 
PPTX
Lesson 1 - Intro to Arithmetic Operators.pptx
takatorifernandez
 
PPTX
Cs1123 4 variables_constants
TAlha MAlik
 
PPTX
Operators and expressions in C++
Neeru Mittal
 
PPT
C Prog. - Operators and Expressions
vinay arora
 
PPTX
Csc240 -lecture_5
Ainuddin Yousufzai
 
PDF
Lec13
Sri Harsha Pamu
 
PDF
02 - Data Types and Expressions using C.pdf
jpradha86
 
PPTX
C++ Programming Basics.pptx
ZntalemAbebe
 
PDF
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
PPT
operators and arithmatic expression in C Language
ParamesswariNataraja
 
PPT
Operator & Expression in c++
bajiajugal
 
ICP - Lecture 5
Hassaan Rahman
 
chap-3-operators.ppt it has all maths problems as comp operation is concerned...
kiokocurtis
 
programing in c PPT Gaurav Nautiyal.pptx
Hacker301428
 
Operators and expressions
vishaljot_kaur
 
Arithmetic instructions
Learn By Watch
 
C - programming - Ankit Kumar Singh
AnkitSinghRajput35
 
Operation and expression in c++
Online
 
Chapter 13.1.2
patcha535
 
Lesson 1 - Intro to Arithmetic Operators.pptx
takatorifernandez
 
Cs1123 4 variables_constants
TAlha MAlik
 
Operators and expressions in C++
Neeru Mittal
 
C Prog. - Operators and Expressions
vinay arora
 
Csc240 -lecture_5
Ainuddin Yousufzai
 
02 - Data Types and Expressions using C.pdf
jpradha86
 
C++ Programming Basics.pptx
ZntalemAbebe
 
Unit ii chapter 1 operator and expressions in c
Sowmya Jyothi
 
operators and arithmatic expression in C Language
ParamesswariNataraja
 
Operator & Expression in c++
bajiajugal
 
Ad

More from Muhammad Hammad Waseem (20)

PDF
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 16] Structures in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 14] Recursion
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 12] Functions in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 11] Loops in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 09] Conditional Operator in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 08] Decision Control Structures (If Statement)
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 07] Comments in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 05] Datatypes
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 04] Variables and Constants in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 03] Introduction to C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
Muhammad Hammad Waseem
 
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 16] Structures in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
[ITP - Lecture 14] Recursion
Muhammad Hammad Waseem
 
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
[ITP - Lecture 12] Functions in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 11] Loops in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 09] Conditional Operator in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
Muhammad Hammad Waseem
 
[ITP - Lecture 07] Comments in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 05] Datatypes
Muhammad Hammad Waseem
 
[ITP - Lecture 04] Variables and Constants in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 03] Introduction to C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
Muhammad Hammad Waseem
 
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
Muhammad Hammad Waseem
 
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
Muhammad Hammad Waseem
 

Recently uploaded (20)

PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PDF
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
Understanding operators in c language.pptx
auteharshil95
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
Landforms and landscapes data surprise preview
jpinnuck
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 

[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence

  • 1. Arithmetic Operators and Order of Precedence Intro to Programming MUHAMMAD HAMMAD WASEEM 1 Arithmetic Operators The arithmetic operators are the symbols that represent arithmetic operations. These are used in arithmetic expressions. Each arithmetic operators upon two numeric values (constants or variables) and return a value. The following arithmetic operators are used in C++.  Addition (+)  Subtraction (-)  Multiplication (*)  Division (/)  For remainder (%) All arithmetic operators, except the remainder operator, are used for all type of numeric data. A C arithmetic instruction consists of a variable name on the left hand side of = and variable names & constants on the right hand side of =. The variables and constants appearing on the right hand side of = are connected by arithmetic operators like +, -, *, and /. The variables and constants together are called ‘operands’ that are operated upon by the ‘arithmetic operators’ and the result is assigned, using the assignment operator, to the variable on left- hand side. A C arithmetic statement could be of three types. These are as follows: Integer mode arithmetic statement - This is an arithmetic statement in which all operands are either integer variables or integer constants. Ex.: int i, king, issac, noteit; i = i + 1; king = issac * 234 + noteit - 7689 ; Real mode arithmetic statement - This is an arithmetic statement in which all operands are either real constants or real variables. Ex.: float qbee, antink, si, prin, anoy, roi; qbee = antink + 23.123 / 4.5 * 0.3442 ; si = prin * anoy * roi / 100.0 ; Mixed mode arithmetic statement - This is an arithmetic statement in which some of the operands are integers and some of the operands are real. Ex.: float si, prin, anoy, roi, avg; int a, b, c, num ; si = prin * anoy * roi / 100.0 ; avg = ( a + b + c + num ) / 4 ; It is very important to understand how the execution of an arithmetic statement takes place. Firstly, the right hand side is evaluated using constants and the numerical values stored in the variable names. This value is then assigned to the variable on the left-hand side.
  • 2. Arithmetic Operators and Order of Precedence Intro to Programming MUHAMMAD HAMMAD WASEEM 2 Though Arithmetic instructions look simple to use one often commits mistakes in writing them. Let us take a closer look at these statements. Note the following points carefully. a) C allows only one variable on left-hand side of =. That is, z=k*l is legal, whereas k*l=z is illegal. b) In addition to the division operator C also provides a modular division operator. This operator returns the remainder on dividing one integer with another. Thus the expression 10 / 2 yields 5, whereas, 10 % 2 yields 0. Note: the modulus operator (%) cannot be applied on a float. Also note that on using % the sign of the remainder is always same as the sign of the numerator. Thus –5 % 2 yields –1, whereas, 5 % -2 yields 1. c) An arithmetic instruction is often used for storing character constants in character variables. char a, b, d ; a = 'F'; b = 'G'; d = '+'; When we do this the ASCII values of the characters are stored in the variables. ASCII values are used to represent any character in memory. The ASCII values of ‘F’ & ‘G’ are 70 & 71 (refer ASCII Table). d) Arithmetic operations can be performed on ints, floats and chars. char x, y ; int z ; x = 'a'; y = 'b'; z = x + y; Thus the statements, are perfectly valid, since the addition is performed on the ASCII values of the characters and not on characters themselves. The ASCII values of ‘a’ and ‘b’ are 97 and 98, and hence can definitely be added. e) No operator is assumed to be present. It must be written explicitly. In the following example, the multiplication operator after b must be explicitly written. a = c.d.b(xy) //usual arithmetic statement b = c * d * b * (x * y) //C statement f) Unlike other high level languages, there is no operator for performing exponentiation operation. Thus following statements are invalid. a = 3 ** 2; b = 3 ^ 2; Integer and Float Conversions In order to effectively develop C programs, it will be necessary to understand the rules that are used for the implicit conversion of floating point and integer values in C. These are mentioned below. Note them carefully. a) An arithmetic operation between an integer and integer always yields an integer result. b) An operation between a real and real always yields a real result. c) An operation between an integer and real always yields a real result. In this operation the integer is first promoted to a real and then the operation is performed. Hence the result is real. I think a few practical examples shown in the following figure would put the issue beyond doubt.
  • 3. Arithmetic Operators and Order of Precedence Intro to Programming MUHAMMAD HAMMAD WASEEM 3 Class Practice: It has been assumed that k is an integer variable and a is a real variable. Order of Precedence of Operation The priority or precedence or order in which the operations in an arithmetic statement are performed is called the order of precedence. It is also known as hierarchy of operations. While executing an arithmetic statement, which has two or more operators, we may have some problems as to how exactly does it get executed. For example, does the expression 2 * x - 3 * y correspond to (2x)-(3y) or to 2(x-3y)? Similarly, does A / B * C correspond to A / (B * C) or to (A / B) * C? To answer these questions satisfactorily one has to understand the ‘hierarchy’ of operations. In an expression in C/C++, the operations are performed in the following order: 1. All multiplications, divisions and modular division are performed first from left to right. 2. All additions and subtractions are then performed from left to right. 3. If parenthesis are used in an expression, the expressions within parenthesis are first computed from left to right. 4. When parenthesis are used within parenthesis, the expression within innermost parenthesis is evaluated first. 5. Assignment is done at the end.
  • 4. Arithmetic Operators and Order of Precedence Intro to Programming MUHAMMAD HAMMAD WASEEM 4 For Example (4-(3*5)) + 2 is evaluated as follows: 1. (3*5) is computed and returns value of 15 2. 4-15 is computed and returns value of -11 3. -11+2 is computed and returns value of -9 Example 1: Determine the hierarchy of operations and evaluate the following expression: i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8 Stepwise evaluation of this expression is shown below: i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8 i = 6 / 4 + 4 / 4 + 8 - 2 + 5 / 8 operation: * i = 1 + 4 / 4 + 8 - 2 + 5 / 8 operation: / i = 1 + 1+ 8 - 2 + 5 / 8 operation: / i = 1 + 1 + 8 - 2 + 0 operation: / i = 2 + 8 - 2 + 0 operation: + i = 10 - 2 + 0 operation: + i = 8 + 0 operation : - i = 8 operation: + Note that 6 / 4 gives 1 and not 1.5. This so happens because 6 and 4 both are integers and therefore would evaluate to only an integer constant. Similarly 5 / 8 evaluates to zero, since 5 and 8 are integer constants and hence must return an integer value. Example 2: Determine the hierarchy of operations and evaluate the following expression: kk = 3 / 2 * 4 + 3 / 8 + 3 Stepwise evaluation of this expression is shown below: kk = 3 / 2 * 4 + 3 / 8 + 3 kk = 1 * 4 + 3 / 8 + 3 operation: / kk = 4 + 3 / 8 + 3 operation: * kk = 4 + 0 + 3 operation: / kk = 4 + 3 operation: + kk = 7 operation: + Note that 3 / 8 gives zero, again for the same reason mentioned in the previous example.