SlideShare uma empresa Scribd logo
1 de 4
Baixar para ler offline
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.

Mais conteúdo relacionado

Mais procurados (20)

Geometric algorithms
Geometric algorithmsGeometric algorithms
Geometric algorithms
 
Data Structures & Algorithm design using C
Data Structures & Algorithm design using C Data Structures & Algorithm design using C
Data Structures & Algorithm design using C
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
Counting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort AlgorithmsCounting Sort and Radix Sort Algorithms
Counting Sort and Radix Sort Algorithms
 
Page replacement algorithms
Page replacement algorithmsPage replacement algorithms
Page replacement algorithms
 
Page replacement
Page replacementPage replacement
Page replacement
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
1.10. pumping lemma for regular sets
1.10. pumping lemma for regular sets1.10. pumping lemma for regular sets
1.10. pumping lemma for regular sets
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
ambiguity grammar.pdf
ambiguity grammar.pdfambiguity grammar.pdf
ambiguity grammar.pdf
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
Hash table
Hash tableHash table
Hash table
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Memory organization (Computer architecture)
Memory organization (Computer architecture)Memory organization (Computer architecture)
Memory organization (Computer architecture)
 
Os services
Os servicesOs services
Os services
 
Automata theory -RE to NFA-ε
Automata theory -RE to  NFA-εAutomata theory -RE to  NFA-ε
Automata theory -RE to NFA-ε
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Cat database
Cat databaseCat database
Cat database
 
State space search
State space searchState space search
State space search
 
Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
 

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

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

Theory3
Theory3Theory3
Theory3
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptx
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
ICP - Lecture 5
ICP - Lecture 5ICP - Lecture 5
ICP - Lecture 5
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
C Sharp Jn (2)
C Sharp Jn (2)C Sharp Jn (2)
C Sharp Jn (2)
 
Ch03
Ch03Ch03
Ch03
 
C++ revision tour
C++ revision tourC++ revision tour
C++ revision tour
 
Operators
OperatorsOperators
Operators
 
Chapter 5 - Operators in C++
Chapter 5 - Operators in C++Chapter 5 - Operators in C++
Chapter 5 - Operators in C++
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
C++ revision add on till now
C++ revision add on till nowC++ revision add on till now
C++ revision add on till now
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
 
C Programming
C ProgrammingC Programming
C Programming
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 

Mais de Muhammad Hammad Waseem

[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++Muhammad Hammad Waseem
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to PointersMuhammad Hammad Waseem
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++Muhammad Hammad Waseem
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++Muhammad Hammad Waseem
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)Muhammad Hammad Waseem
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++Muhammad Hammad Waseem
 
[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++Muhammad Hammad Waseem
 
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of ProgrammingMuhammad Hammad Waseem
 
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming LanguagesMuhammad Hammad Waseem
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnTypeMuhammad Hammad Waseem
 

Mais de Muhammad Hammad Waseem (20)

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

Último

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 

Último (20)

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 

[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.