Introduction To Computer Programming (ICP) : Lecture-6 & 7: Operators
Introduction To Computer Programming (ICP) : Lecture-6 & 7: Operators
30/9/2015
Introduction to Computer Programming
(ICP)
Muhammad Ahmad
Lecturer
1
LECTURE CONTENTS
30/9/2015
Arithmetic & Sizeof Operators
Logical & Relational Operators
Assignment Operator
Compound Assignment
Logical Operators
2
PROGRAM ERRORS
30/9/2015
Conceptual Errors
#include <iostream>
Using namespace std;
int main ()
{
int a=4;
int b=10;
int c = b/a;
cout << “C = “ << c;
return 0;
}
The program is not doing what you expect it to do.
3
THE CIN OBJECT
30/9/2015
It is used to read data from keyboard
In simple words. user enter data from the
keyboard
Syntax:
Cin>>variable;
4
ARITHMETIC OPERATORS
30/9/2015
Arithmetic calculations
*
Multiplication
/
Division
Integer division truncates remainder
7 / 5 evaluates to 1
%
Modulus operator returns remainder
7 % 5 evaluates to 2
+ and –
Addition and Subtraction 5
CLASS ACTIVITY
30/9/2015
Write a program which has all the
mathematical operators?
+, -, /, %
6
ARITHMETIC OPERATORS
30/9/2015
Rules of operator precedence
30/9/2015
Priority of operators
a = 5 + 7 % 2;
we may doubt if it really means:
a = 5 + (7 % 2) with result 6 or
a = (5 + 7) % 2 with result 0
8
ARITHMETIC OPERATORS
30/9/2015
Given integer variables a, b, c, d, and e,
where a = 1, b = 2, c = 3, d = 4, Evaluate
the following expressions:
a + b - c + d
a * b / c
1 + a * b % c
a + d % b - c
e = b = d + c / b - a
9
COMPOUND ASSIGNMENT
30/9/2015
Arithmetic Assignment Operators
a = a + b;
a += b;
void main(void)
{
int number = 15;
number +=10;
cout << number << endl; 25
number -=7;
18
cout << number << endl;
number *=2; 36
cout << number << endl;
number %=2; 0
cout << number << endl;
}
10
ARITHMETIC OPERATORS
30/9/2015
Increment Operators (Unary Operator)
count = count + 1;
count +=1;
count++; OR ++count;
int a = 5; int a = 5;
int b = 10; int b = 10;
int c = a * b++; int c = a * +
+b;
50 55
11
ARITHMETIC OPERATORS
30/9/2015
Decrement Operators (Unary Operator)
count = count - 1;
count -=1;
count--; OR --count;
postfix prefix
12
ARITHMETIC OPERATORS
30/9/2015
int main()
{
int count = 10; 10
11
cout << count << endl; 11
cout << ++count << endl; 11
cout << count << endl; 12
cout << count++ << endl;
cout << count << endl;
}
13
RELEATIONAL OPERATORS
30/9/2015
To evaluate comparison between two
expressions
Result: True / xFalse
Assume variable holds 10 and variable y holds 20,
then:
Standard algebraic C++ equality Example Meaning of
equality operator or or relational of C++ C++ condition
relational operator operator condition
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y 14
RELATIONAL OPERATORS
30/9/2015
Examples
(7 == 5) would return false
(3 != 2) would return true
(6 >= 6) would return true
If a=2, b=3 and c=6
(a*b >= c) would return true since it is (2*3 >=
6)
(b+4 > a*c) would return false since it is (3+4 >
2*6)
((b=2) == a) would return true
15
LOGICAL OPERATORS
30/9/2015
Logical expressions - expressions that use
conditional statements and logical operators.
&& (And)
A && B is true if and only if both A and B are true
|| (Or)
A || B is true if either A or B are true
! (Not)
!(condition) is true if condition is false, and false if
condition is true
This is called the logical complement or negation
Examples
(salary < 10000) || (dependants > 5)
(temperature > 40.0) && (humidity > 90)
16
!(temperature > 90.0)
LOGICAL OPERATORS - EXERCISES
30/9/2015
NOT, AND, OR : ( !, &&, || )
Operator ! is equivalent to Boolean operation
NOT
! (5 == 5) returns false
! (6 <= 4) returns true
! true returns false.
! false returns true.
17
REMEMBER
30/9/2015
&& operator yields a true result only
when both its operands are true.
||
operator yields a false result only
when both its operands are false.
18
EXPRESSIONS
30/9/2015
A sequence of operators and operands that
specifies a computation
Operand
On which computation is performed
E.g: c = a + b; //a & b are operands & + is an
operator
C = 7 + 8; // C=15
19
ESCAPE SEQUENCE
30/9/2015
20
ESCAPE SEQUENCE EXAMPLE
30/9/2015
21
30/9/2015
QUESTIONS
?
22