Introduction To Computing
Introduction To Computing
Lecture 17
1/6/17
1/6/17
Operator Types
1. Arithmetic Operators
2. Logical Operators
3. Relational Operators
1/6/17
1- Arithmetic Operators
Used to manipulate integral and
floating-point data types.
+ (addition)
- (subtraction or negation)
* (multiplication)
/(division)
% (mod, (modulus or remainder)
++ (increment)
-- (decrement)
1/6/17
Arithmetic Operators
Operators +, -, *, and / can be used with
both integral and floating-point data types.
% is used with only integral data type to
find the remainder in ordinary division.
Integer division (i.e., where both the
numerator and the denominator are
integers) yields an integer quotient
for example, the expression 7 / 4 evaluates to
1 and the expression 17 / 5 evaluates to 3.
1/6/17
1/6/17
1/6/17
Examples
Associativity:
2+3+5
2+3-4+10
1/6/17
10
Examples
6
1/6/17
5
11
Crhr
Grade
Weight
ITC
Calculus
English
Pak/Isl
Elective
CGPA= ((weight*crhr)+(weight*crhr)+..+(weight*crhr))/
(4+3+3+3+3)
1/6/17
12
2- Relational operators
In C++, a condition is represented by
a logical (Boolean) expression.
An expression that has a value of
either true or false is called a logical
(Boolean) expression.
A relational operator allows you to
make
comparisons in a program.
1/6/17
13
Relational operators
1/6/17
14
1/6/17
15
Comparing Characters
1/6/17
16
3- Logical Operators
Logical (Boolean) operators enable you
to combine logical expressions.
Logical operators take only logical
values as operands and yield only
logical values as results.
There are three logical operators
AND &&
OR ||
NOT !
1/6/17
17
Not operator
1/6/17
18
And operator
1/6/17
19
Or operator
1/6/17
20
Order of precedence
Complex logical expressions can be
difficult to evaluate.
To work with complex logical
expressions, there must be some
priority scheme for evaluating
operators.
1/6/17
21
Order of precedence
1/6/17
22
Examples
1/6/17
23
1/6/17
24
25
Example 1
/* Demonstration of if statement */
#include<iostream>
using namespace std;
int main( )
{
int num ;
cout<< "Enter a number less than 10 :" ;
cin>>num ;
if ( num <= 10 )
cout<< Yes . The number is less than 10." ;
}
Num<
10
Yes your are
right
Example 2
While purchasing certain items, a
discount of 10% is offered if the
quantity purchased is more than 1000.
If quantity and price per item are input
through the keyboard, write a program
to calculate the total expenses.
Example 3
The current year and the year in which the employee
joined the organization are entered through the
keyboard. If the number of years for which the
employee has served the organization is greater than
3 then a bonus of Rs. 2500/- is given to the employee.
If the years of service are not greater than 3, then the
program should do nothing.
1/6/17
31
1/6/17
33
1/6/17
34
Examples
Example Fee Calculation
If CGPA >= 3.8 50% (Discount)
CGPA >= 3.5 25% (Discount)
CGPA <3.5 0% (Discount)
1/6/17
35
Example 1
1/6/17
36
Example 2
1/6/17
37
Short-Circuit Evaluation
A process in which the computer
evaluates a logical expression from
left to right and stops as soon as the
value of the expression is known.
1/6/17
38
if (a >= b)
max = a;
else
max = b;
1/6/17
39
Problems
1. Input two numbers and find absolute value
of difference of the numbers.
2. Draw flow chart that reads in two integers
and determines and prints if the first is a
multiple of the second.
3. Input two numbers from the user and do
the following:
If second number is greater than 0, divide them
If second number is less than 0, multiply them
If second number is equal to 0, add them
1/6/17
40