0% found this document useful (0 votes)
5 views22 pages

Week 9 Operators 05122024 023311pm

This document covers C++ operators, including arithmetic, relational, logical, and bitwise operators, along with their usage and precedence. It explains concepts such as operator precedence and associativity, and provides examples of expressions and their evaluations. Additionally, it includes a home task and a programming question related to the content discussed.

Uploaded by

shahzaib6374
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views22 pages

Week 9 Operators 05122024 023311pm

This document covers C++ operators, including arithmetic, relational, logical, and bitwise operators, along with their usage and precedence. It explains concepts such as operator precedence and associativity, and provides examples of expressions and their evaluations. Additionally, it includes a home task and a programming question related to the content discussed.

Uploaded by

shahzaib6374
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

COMPUTING

FUNDAMENTALS
[CSC110]

Lecture No. 10
C++ Operators and
Conditional Structures

DEPARTMENT OF ELECTRICAL ENGINEERING


BAHRIA SCHOOL OF ENGINEERING AND APPLIED SCIENCES
Operators
An operator, in computer programing, is a symbol that
usually represents an action or process.

OPERATOR TYPES:
Type Operators Usage
Arithmetic ‘+’ ‘-’ ‘*’ ‘/’ ‘%’ a+b a-b a*b a/b a%b
Arithmetic ‘+=’ ‘-=’ ‘*=’ ‘/=’ ‘%=’ a+=b is the same as a=a+b
assignment a-=b a*=b a/=b a%=b
Increment and ‘++’ ‘- -’ a++ is the same as a=a+1
decrement a-- is the same as a=a-1
Relational ‘<’ ‘>’ ‘<=’ ‘>=’ ‘==’ ‘!=’
Logical ‘&&’ ‘||’

DEPARTMENT OF ELECTRICAL ENGINEERING


Common Operators
• Common Mathematical Operators:
- Common mathematical operators are available
in C++ for manipulating values e.g. addition(+),
subtraction(-), multiplication(*), division(/),
and modulus (%).

•C, C++ has many other operators also which


we will study in due course.

DEPARTMENT OF ELECTRICAL ENGINEERING


Arithmetic Expression Evaluation
•To evaluate an arithmetic expression two
concepts needs to be understood
- Operator Precedence
•Operator precedence controls the order in which
operations are performed
- Operator Associativity
• The associativity of an operator specifies the order
in which operations of the same precedence are
performed

DEPARTMENT OF ELECTRICAL ENGINEERING


Operator Precedence and Associativity

• Operators Precedence and Associativity


for C++ is following
1. *, /, %  Do all multiplications, divisions
and remainders from left to right.

2. +, -  Do additions and subtractions from


left to right.

DEPARTMENT OF ELECTRICAL ENGINEERING


Evaluating an Expression similar to DMAS
6+2*3/6
• Three operators are in this expression.
• However, * and / both have the same precedence and + has
lower precedence then these two.
• * and / will be evaluated first but both have the same
precedence level.
• Therefore, operator associatively will be used here to
determine the first to get evaluated i.e. left to right.
• The left most sub expression will be evaluated followed by
the next right one and so on.

• * will be evaluated first then /

DEPARTMENT OF ELECTRICAL ENGINEERING


Arithemetic Operator
Type Operators Usage
Arithmetic ‘+’ ‘-’ ‘*’ ‘/’ ‘%’ a+b a-b a*b a/b a%b

• The Modulus Operator


• % is known as the Modulus Operator or the Remainder Operator.
• It calculates the remainder of two variables
• It can only be used with two ints..

• 3%2 =1
• 5%2=1
• 6%3=0
• 8%5=3

DEPARTMENT OF ELECTRICAL ENGINEERING


Arithmetic Operator Precedence and associativity

DEPARTMENT OF ELECTRICAL ENGINEERING


Polynomials in C++
• In algebra

• In c++

• What happens if I don’t put the parenthesis ()?

DEPARTMENT OF ELECTRICAL ENGINEERING


Evaluate
In what order will the expression be evaluated?

DEPARTMENT OF ELECTRICAL ENGINEERING


2nd Degree Polynomial

DEPARTMENT OF ELECTRICAL ENGINEERING


2nd Degree Polynomial

DEPARTMENT OF ELECTRICAL ENGINEERING


Arithmetic Assignment Operators
Type Operators Usage
Arithmetic ‘+=’ ‘-=’ ‘*=’ ‘/=’ ‘%=’ a+=b is the same as a=a+b
assignment a-=b a*=b a/=b a%=b

DEPARTMENT OF ELECTRICAL ENGINEERING


Relational and Equality Operators
Standard algebraic C++ equality or Sample C++ Meaning of C++
equality or relational relational operator condition condition
operator
Relational operators
> > x>y x is greater than y

< < x<y x is less than y


 >= x >= y x is greater than or
equal to y
 <= x <= y x is less than or
equal to y
Equality operators
= == x == y x is equal to y
 != x != y x is not equal to y

(Deitel and Deitel (5th Ed), fig 2.12)

DEPARTMENT OF ELECTRICAL ENGINEERING


Logical Operators
Type Operators Usage
Logical ‘&&’ ‘||’

• Logical operators are carried out on statements, e.g.


statement1 && statement 2, etc.
• Logical AND (&&)
• false && false= false
• false && true = false
• true && false= false
• true && true = true

• Logical OR (||)

DEPARTMENT OF ELECTRICAL ENGINEERING


Logical Operators
• Logical OR (||)
• false || false = false
• false || true = true
• true || false = true
• true || true = true

• Logical NOT (!)


• !false = true
• !true = false

DEPARTMENT OF ELECTRICAL ENGINEERING


Bitwise Operators
• Used to manipulate bits rather than statements
• Common bitwise operators are:
• XOR(^)
• x^y
• NOT (~)
• ~x
• OR (|)
• x|y
• AND (&)
• x&y

DEPARTMENT OF ELECTRICAL ENGINEERING


Unary Increment and Decrement Operators
Type Operators Usage
Increment and ‘++’ ‘- -’ a++ is the same as a=a+1
decrement a-- is the same as a=a-1

 Prefix
 ++a;
 --a;
 Postfix
 a++;
 a--;
DEPARTMENT OF ELECTRICAL ENGINEERING
DEPARTMENT OF ELECTRICAL ENGINEERING
Precedence of Operators
OPERATORS TYPE
++ -- unary
* / % multiplicative
+ - additive
< > <= >= relational
== != equality
&& || logical
= *= /= %= += assignment
-=

DEPARTMENT OF ELECTRICAL ENGINEERING


Home Task
Precedence of Operators
• a += b-- + ++d * c % e / f

• Consider all variables to be equal to 2 and calculate the answer


• a=?

DEPARTMENT OF ELECTRICAL ENGINEERING


Question
Develop a program that takes a three digit number and display the sum
of these numbers.

• Languages: C++

DEPARTMENT OF ELECTRICAL ENGINEERING

You might also like