0% found this document useful (0 votes)
17 views23 pages

C++ Lecture 4 - 250220 - 134011

The document is a lecture on C++ operators and expressions, detailing various types of operators including arithmetic, assignment, relational, logical, and bitwise operators. It explains the concepts of unary, binary, and ternary operators, as well as the order of precedence and associativity of operators. Additionally, it includes examples and exercises for practical understanding of operator usage in programming.

Uploaded by

wambeddetrevor
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)
17 views23 pages

C++ Lecture 4 - 250220 - 134011

The document is a lecture on C++ operators and expressions, detailing various types of operators including arithmetic, assignment, relational, logical, and bitwise operators. It explains the concepts of unary, binary, and ternary operators, as well as the order of precedence and associativity of operators. Additionally, it includes examples and exercises for practical understanding of operator usage in programming.

Uploaded by

wambeddetrevor
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/ 23

TEEE 1202 Lecture 4

2025 02 20

Dr. Mugerwa Dickson

Kyambogo University
Department of Electrical and Electronics engineering
Faculty of Engineering

MNC
MNCLab. Lab.
Mobile Networking & Computing
Mobile Networking & Computing
C++ Operators and expressions

MNC
MNCLab. Lab.
Mobile Networking & Computing
Mobile Networking & Computing
Teaser

❖What do you understand by the term “operator”

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Operators
❖ An operator is a symbol that operates on a value or a variable.
❖ Operators are symbols that perform operations on variables and values
For example, + is an operator used for addition, while - is an operator used for subtraction.
❖ We can also define operators as symbols that help us to perform specific mathematical and logical computations on operands
.
example, ‘+’ is an operator used for addition, as shown below:

c = a + b;
❖Here, ‘+’ is the operator known as the addition operator and ‘a’ and ‘b’ are operands.

❖The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’.

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Operators

Operators in C++ can be classified into 6 types


Arithmetic Operators

Assignment Operators

Relational Operators

Logical Operators

Bitwise Operators

Other Operators

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Operators

❖ Operators can further be categorized as; Unary, Binary and Ternary operators

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Operators

❖Unary operators: Operators that operate or work with a single operand.


❖ For example:
◆Increment (++) and Decrement (--) Operators

int val = 5;

cout<<++val; // 6
Binary Operators:
❖Operators that operate or work with two operands are binary operators. For example:
❖Addition (+), Subtraction (-), multiplication (*), Division (/) operators
int a = 7;

int b = 2; cout<<a+b; // 9

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Operators

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Operators
❖Assignment Operators
❖The assignment operator, =, should be read as “becomes” or is “replaced by”.

❖The computer evaluates the expression to the right of = and assigns the resulting value to the left of =.

Example 2.3.1

❖A = 8;

❖B = 12;

❖C = A % B;

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Contn

❖Compound Assignment Operators


These simplify writing some common arithmetic expressions

Operator Read As Example:


num = num * num;
+= Is increased by
Is equivalent to

-= Is decreased by num *= num;

*= Is multiplied by

/= Is divided by

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Contn

Increment and Decrement Operators

❖Increment/decrement operators increment/decrement the value of the object. C++ also provides in
crement and decrement operators: ++ and -- respectively.
❖++ increases the value of the operand by 1
❖-- decreases it by 1

❖The operators are:


• ++x // pre-increment operator
• x++ // post-increment operator
• --x // pre-decrement operator
• x-- // post-decrement operator

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Arithmetic operators
Order of precedence

Operators Associativity
() Left to Right (from inside out)
+(unary) – (unary) Right to Left
*/ % Left to Right
+ - Left to Right
= Right to Left

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Lab work (5 minutes)

❖Use of operators
❖Int i,j
❖i=7;
❖J=4+--I
❖i=7
❖J=4+i++
❖Print the values of I and j

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Contn
Relational Operators
Used for comparison of the value of two operands

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Operators

❖C++ Logical Operators


❖Logical operators are used to check whether an expression is true or false.
❖ If the expression is true, it returns 1 whereas if the expression is false,
it returns 0.
❖&& Logical AND True only if all the operands are true
❖|| Logical OR True if at least one of the operands is expressed true
❖! Logical NOT True if only the operand is false

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
examples

a = 5 b = 8

Then,

(a > 3) && (b > 5) evaluates to true


(a > 3) && (b < 5) evaluates to false

(a > 3) || (b > 5) evaluates to true


(a > 3) || (b < 5) evaluates to true
(a < 3) || (b < 5) evaluates to false

!(a < 3) evaluates to true


!(a > 3) evaluates to false
MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Lab exercise (20 min)

❖Write a program to compare two numbers


◆E.g 2 and 6

❖Write a program to check if a Number is Positive, Negative, or Zero


❖Find the largest of 3 numbers

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Lab exercise (6 minutes)
#include <iostream>

using namespace std;

int main() {

int a = 3; int b = 5;

cout<<(a < b)<< endl; // operator to check if a is smaller than b

return 0;

❖Example 2.3.5

int a,b;

if (a<b)

a += 3;

else

b -= 4;

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Order of precedence

❖Order of Precedence

❖Operators Associativity

❖() Left to Right

❖! ± (unary) ++ -- Right to Left

❖% / * Left to Right

❖+ - Left to Right

❖< <= > >= Left to Right

❖== != Left to Right

❖&& Left to Right

❖|| Left to Right

❖= += -= *= /= Right to Left
MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
QN
❖Work each exercise independently of the others. State the values of each variable involved after the computer executes each statement. Use the follo
wing declarations.

❖int i=8, j=5, k=-3, h, m;

i) ++j;

ii) k++;

iii) h=j--+I;

iv) h=--j+k;

v) m=j--+ ++I;

vi) h=++j;

m=j++;

i) j+= ++k-J++ + ++I;

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Ex.
❖Question One

Find the value assigned to the variable on the left of the assignment operator=. Work each problem independently of the
other. Assume the following declarations.

int i =4, j = 5, k = 6,

result;
i) result = 8 *i-j;

ii) result = 47% i+k*j+9;

iii) result= 37% (k-i)*j+9;

iv) result = i*(8+(j+5)/3)-k;

v) k=k+j;

vi) i=i/j+k;

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Qn
❖Question Two

Find the value assigned to the variable on the left of the assignment operator =. Work each problem independently of the othe
rs. Assume the following declarations

double x = 5.617, y = -14.238, z=0.75825, result;

i) result = 7.43+4.39 * x;

ii) result = y/x * z;

iii) x=x - y;

iv) y=y * y;

MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
The operators are first converted to bit-level and then the calculation is performed on the operands
int a = 5, b = 9; // a = 5(00000101), b = 9(00001001)
cout << (a ^ b); // 00001100 cout <<(~a); // 11111010

You might also like