C++ Lecture 4 - 250220 - 134011
C++ Lecture 4 - 250220 - 134011
2025 02 20
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
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
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
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
*= Is multiplied by
/= Is divided by
MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Contn
❖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
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
MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
examples
a = 5 b = 8
Then,
MNCLab.
Lab.
Mobile Networking
Networking&&Computing
Computing
Lab exercise (6 minutes)
#include <iostream>
int main() {
int a = 3; int b = 5;
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
❖+ - 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.
i) ++j;
ii) k++;
iii) h=j--+I;
iv) h=--j+k;
v) m=j--+ ++I;
vi) h=++j;
m=j++;
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;
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
i) result = 7.43+4.39 * x;
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