0% found this document useful (0 votes)
29 views

Operators and Conditional

- Common mathematical operators are available in C++ for manipulating values e.g. addition(+), subtraction(-), multiplication(*), division(/), and modulus (%).

Uploaded by

Zayn Ul Abdin
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)
29 views

Operators and Conditional

- Common mathematical operators are available in C++ for manipulating values e.g. addition(+), subtraction(-), multiplication(*), division(/), and modulus (%).

Uploaded by

Zayn Ul Abdin
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/ 45

Programming Fundamentals

CSC-112
Lecture 3
Operators
Operators
• “Operators are words or symbols that cause a program
to do something to variables.”

OPERATOR TYPES:
Type Operators Usage
Arithmetic ‘+’ ‘-’ ‘*’ ‘/’ ‘%’ a+b a-b a*b a/b a%b
Arithmetic ‘+=’ ‘-=’ ‘*=’ ‘/=’ ‘%=’ a+=b is the same as a=a+b
assignment a-=b a*=b a/=b a%=b
Increment and ‘++’ ‘- -’ a++ is the same as a=a+1
decrement a-- is the same as a=a-1
Relational ‘<’ ‘>’ ‘<=’ ‘>=’ ‘==’ ‘!=’
Logical ‘&&’ ‘||’
Common Operators
• Common Mathematical Operators:
- Common mathematical operators are available
in C++ for manipulating values e.g. addition(+),
subtraction(-), multiplication(*), division(/),
and modulus (%).
•C, C++ has many other operators also which
we will study in due course.
Arithmetic Expression Evaluation

•To evaluate an arithmetic expression two


concepts needs to be understood
- Operator Precedence
•Operator precedence controls the order in which
operations are performed
- Operator Associativity
•The associativity of an operator specifies the order
in which operations of the same precedence are
performed
Operator Precedence and
Associativity
• Operators Precedence and Associativity
for C++ is following

1. *, /, %  Do all multiplications, divisions


and remainders from left to right.
2. +, -  Do additions and subtractions from
left to right.
Evaluating an Expression similar to DMAS

6+2*3/6
• Three operators are in this expression.
• However, * and / both have the same precedence and + has
lower precedence then these two.
• * and / will be evaluated first but both have the same
precedence level.
• Therefore, operator associatively will be used here to
determine the first to get evaluated i.e. left to right.
• The left most sub expression will be evaluated followed by
the next right one and so on.

• * will be evaluated first then /


Arithemetic Operator

Type Operators Usage


Arithmetic ‘+’ ‘-’ ‘*’ ‘/’ ‘%’ a+b a-b a*b a/b a%b

• The Modulus Operator


– % is known as the Modulus Operator or the Remainder Operator.
– It calculates the remainder of two variables
– It can only be used with two ints..

– 3%2 =1
– 5%2=1
– 6%3=0
– 8%5=3
Arithmetic Operator Precedence and
associativity
Polynomials in C++
• In algebra
𝑎+𝑏+𝑐+𝑑+𝑒
𝑚=
5
• In c++
𝑚 = (𝑎 + 𝑏 + 𝑐 + 𝑑 + 𝑒)/5

• What happens if I don’t put the parenthesis ()?


Evaluate
In what order will the expression be evaluated?
Evaluate
In what order will the expression be evaluated?
2nd Degree Polynomial

𝑦= 𝑎𝑥 2 + 𝑏𝑥 + 𝑐
2nd Degree Polynomial

𝑦= 𝑎𝑥 2 + 𝑏𝑥 + 𝑐
Arithmetic Assignment Operators

Type Operators Usage


Arithmetic ‘+=’ ‘-=’ ‘*=’ ‘/=’ ‘%=’ a+=b is the same as a=a+b
assignment a-=b a*=b a/=b a%=b
Relational and Equality Operators
Standard algebraic C++ equality or Sample C++ Meaning of C++
equality or relational relational operator condition condition
operator
Relational operators
> > x>y x is greater than y

< < x<y x is less than y


 >= x >= y x is greater than or
equal to y
 <= x <= y x is less than or
equal to y
Equality operators
= == x == y x is equal to y
 != x != y x is not equal to y

(Deitel and Deitel (5th Ed), fig 2.12)


Logical Operators

Type Operators Usage


Logical ‘&&’ ‘||’

• Logical operators are carried out on statements, e.g. statement1


&& statement 2, etc.
• Logical AND (&&)
– false && false= false
– false && true = false
– true && false= false
– true && true = true
• Logical OR (||)
Logical Operators
• Logical OR (||)
– false || false = false
– false || true = true
– true || false = true
– true || true = true

• Logical NOT (!)


– !false = true
– !true = false
Bitwise Operators
• Used to manipulate bits rather than statements
• Common bitwise operators are:
• XOR(^)
– x^y
• NOT (~)
– ~x
• OR (|)
– x|y
• AND (&)
– x&y
Unary Increment and Decrement
Operators
Type Operators Usage
Increment and ‘++’ ‘- -’ a++ is the same as a=a+1
decrement a-- is the same as a=a-1

 Prefix
 ++a;
 --a;
 Postfix
 a++;
 a--;
Precedence of Operators
OPERATORS TYPE
++ -- unary
* / % multiplicative
+ - additive
< > <= >= relational
== != equality
&& || logical
= *= /= %= += assignment
-=
Home Task
Precedence of Operators
• a += b-- + ++d * c % e / f

• Consider all variables to be equal to 2 and


calculate the answer
• a=?
Decision Making / Conditional
Statements
Control Structures
• Control structures are the block that analyzes
the variable(s) and choose which way the
program should be executed
• Every program is build on these structures,
there are three types of common control
structures
– the sequence structure,
– the selection structure (Branching) and
– the repetition structure (Looping).
Selection/Conditional statements

• Selection statements cause one section of code


to be executed or not depending on a
conditional clause.
– if statement
– switch statement
The if statement

• Used where the execution of an


action depends on the satisfaction of
a condition. T

– if condition is true -> action is done


The if statement

• Used where the execution of an


action depends on the satisfaction of F

a condition.
– if condition is true -> action is done
– if condition is false -> action is not done
The if statement
Any statement that
results in True or False

Syntax Logical or Relational

if (condition)
T
{ statement1; F

statement 2;
….
statement n;
}
The if statement
• Develop a system that tells the user that he is
over-speeding if his speed crosses120km/hr.

• Pseudocode
IF speed is greater than 120km/hr
THEN print “over-speeding”
The if statement
if (speed>120)
{cout<<"over speeding";
cout << "reduce speed now";
} T
… F



If - else

• if is a single-selection statement which


performs an indicated action only when the
condition is TRUE; otherwise the action is
skipped.

• if-else is a double-selection statement which


performs an action when the condition is
TRUE and a different action when the
condition is FALSE.
If - else
• Used where there are
different actions to be
executed depending on the
result of a given condition.
– if condition is true -> action is
done
– if condition is false -> a
different action is done
If - else
• Syntax:
if (condition)
{statement1; statement 2;…. statement n;
}
else
{statement1; statement 2;….statement n;
}

• Note: if the specified condition is true, then all the


statements in the first curly braces will be executed, else all
the statements in the next curly braces will be executed.
If - else
• Determine whether a number is positive or
negative.

• Pseudocode
• IF number is greater than or equal to 0,
THEN print “positive”
• ELSE print “negative”
If - else
if (number>=0)
cout<<"number is positive";
else
cout<<"number is negative";
If - else
• TRY ME! : for even numbers add half the
number to y. For odd numbers, subtract half.

if (x%2==0)
y += x/2;
else
y -= x/2;
Some notes
• If there is only one statement following the if,
then the curly braces are optional.
• If there are multiple statements, the braces
are necessary.
• Same is true for the statements following the
else.
Some notes
– E.g.
if (a<b)
a=b+1;
b = 10;
cout<<"print A"<<a;

Output (if condition is true)


 a = b+1;
 the cout statement is ignored in this case because of the
missing { }
‘else’ cannot exist without ‘if’
• An if can exist without an else.
• An else, without an if has no meaning.
The else if keyword

• Used where there are multiple actions to be


executed based on different conditions.
– if condition is true -> action is done
– if condition is false -> next condition is checked
The else if keyword
• Syntax:
if (condition)
{statement1; statement 2;…. statement n;
}
else if (condition 2)
{statement1; statement 2;….statement n;
}
else
{statement1; statement 2;….statement n;
}
The else if keyword
if (x==0)
{cout << " x is ZERO" <<endl;}
else if (x>0)
{cout << " x is positive"
<<endl;}
else
{cout << " x is negative"
<<endl;}
The else if keyword
• Additional alternative control paths
• Conditions evaluated in order until one is met;
inner statement (or statements) are then
executed
• If multiple conditions are true, only first
condition is executed, the remaining are
ignored
Quiz # 1
Question
• Write a C or C++ program to calculate GPA and percentage from five
3 credit hours subjects. Take the input marks from user. Total
subjects marks are 100. The grading scheme is given below:
– A >= 87 marks (4 GP)
– B+ >= 80 marks (3.5 GP)
– B >= 72 marks (3 GP)
– C+ >= 66 marks (2.5 GP)
– C >=60 marks (2 GP)
– F < 60 marks (0 GP)

• GPA is calculated as:


σ5𝑖=1 𝐺𝑃 𝑖 ∗ 𝑆𝑢𝑏𝑗𝑒𝑐𝑡 𝑖 . 𝐶𝑟𝑒𝑑𝑖𝑡𝐻𝑜𝑢𝑟𝑠
𝑇𝑜𝑡𝑎𝑙 𝐶𝑟𝑒𝑑𝑖𝑡 𝐻𝑜𝑢𝑟𝑠

You might also like