0% found this document useful (0 votes)
47 views40 pages

Introduction To Computing

This document discusses operators and control structures in C++ programming. It covers arithmetic, relational, and logical operators as well as operator precedence. It also discusses selection control structures like if-else statements for one-way and two-way selection as well as nested if statements. Examples are provided to illustrate calculating discounts, bonuses, and maximum values using these conditional logic operators and statements. Short-circuit evaluation and the ternary operator are also introduced.

Uploaded by

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

Introduction To Computing

This document discusses operators and control structures in C++ programming. It covers arithmetic, relational, and logical operators as well as operator precedence. It also discusses selection control structures like if-else statements for one-way and two-way selection as well as nested if statements. Examples are provided to illustrate calculating discounts, bonuses, and maximum values using these conditional logic operators and statements. Short-circuit evaluation and the ternary operator are also introduced.

Uploaded by

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

Introduction to computing

Lecture 17

1/6/17

Operators and Operator


Precedence

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

Arithmetic expression is constructed by


using arithmetic operators and numbers or
variables.
The numbers appearing in the expression are
called operands.
Unary operator: An operator that has only
one operand.
-5, b++, --b

Binary operator: An operator that has two


operands.
a+b
+ is a operator
a and b are operands
a + b is an expression
1/6/17

1/6/17

Operator precedence Rules


Precedence: The set of rules of operator precedence
defines the order in which C++ applies operators.
1. Operators in expressions within parentheses () are evaluated
first. In cases of nested parentheses , the operators in the
innermost pair of parentheses are applied first.
2. *, / , % are evaluated next and are on the same level of
precedence
3. + and operators are evaluated last and have the same
level of precedence.

Associativity: If an expression contains several


operations of same level operators are applied from left
to right.
1/6/17

1/6/17

Examples
Associativity:
2+3+5
2+3-4+10

Mixing of precedence and Associativity:


2+6/2-1
3*7-6+2*5/4+6

1/6/17

10

Examples

6
1/6/17

5
11

Example: Calculate CGPA


Course

Crhr

Grade

Weight

ITC

Calculus

English

Pak/Isl

Elective

Problem : Student got A in ITC, B in Calculus,


C in English, D in Pak/Isl and B in Elective.
Calculate CGPA of Ali using the formula given
below.

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

Comparing Integers and


floats

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

Decision control Structure

1/6/17

24

Selection: one way selection

The expression is called a


decision
maker
because it decides
whether to execute
the
statement
that
follows it.
The expression is usually
a logical expression.
1/6/17

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.

Selection: Two way selection

1/6/17

31

Selection: Compound (Block of)


Statements

1/6/17

33

Multiple Selections: Nested if


or

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

Conditional Operator (?:)


Ternary operator

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

You might also like