Expressions in C++

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

EXPRESSIONS IN C++

Arithmetic 2
Arithmetic 3

Arithmetic Operators
The built-in arithmetic operations are
• Addition +
• Subtraction 
• Multiplication *
• Division /
• Modulus % (remainder)
Arithmetic 4

Arithmetic expression
• An arithmetic operator can be used with any numeric type
• An operand is a number or a variable used by an operator
• Results of the operator depend upon the types of
operands

If both operands are int, the result is int


If one or both of the operands are float, the result is float
Arithmetic 5

Division /

Division with at least one operator of type float produces


the expected result
float divisor, dividend=5, quotient;
divisor=3
quotient=dividend/divisor;
quotient =1.666----
Result will be the same if either dividend or divisor is of type int

int divisor=3, dividend, quotient;


dividend=5;
quotient=dividend/divisor;
quotient=1
Integer division does not round the result, the fractional part is
discarded.
Arithmetic 6

Integer Division
Arithmetic 7

Modulus %
% operator gives the remainder from integer
division
int divisor, dividend, remainder;
divisor=3
dividend=5;
remainder=dividend % divisor;

The value of remainder is 2


Arithmetic 8

Writing C++ Expressions


Arithmetic 9

Order of evaluation of operations


1. ()
2. * / %
3. + -

Example. What is the order of operations in the following expression?


z= p * r % q + w / x - y;

Z = P * R % Q + W / X - Y ;

  6   1   2   4   3   5    
Arithmetic 10

Program
// Prints the sum, difference, etc. of given integers.
#include <iostream>
 
void main(){
int m = 6, n = 7;
cout << "The integers are " << m << " and " << n << endl;
cout << "Their sum is " << (m+n) << endl;
cout << "Their difference is " << (m-n) << endl;
cout << "Their product is " << (m*n) << endl;
cout << "Their quotient is " << (m/n) << endl;
cout << "Their remainder is " << (m%n) << endl << endl << endl;

}
Arithmetic 11

Assignment Expression
We will usually see an assignment operation in this format:
<variable> = <expression>

An expression is a combination of operators and operands.

For example,

c = 0;
c = c + 3;
average = sum / count;
Arithmetic 12

Compound Assignment
c = c + 3; same as c +=3;

The += operation adds the value of the expression of the right to the
value of the variable on the left and stores the result in the variable on the
left.
 
In general, <var> = <var> op <exp>; can be written as:
<var> op = <exp>;
 
Examples:
c = 4; //same as c = c  4;
c *= 5; //same as c = c *5;
c /= 6; //same as c = c /6;
 
Can we reverse the order of the double operator? Say, c = 4;
No. This simply is the same as the assignment c = 4;
Arithmetic 13

Increment / Decrement Operators


a++; is the same as a = a + 1; and also the same as ++a;

a; is the same as a = a  1; and also the same as a;


 
a ++ postincrement
++a preincrement
a postdecrement
a predecrement
 
• These are unary operators. They operate on only a single operand.
The operators we are more familiar with are binary operators; they
operate on two operands, much like the + operator in the expression
a+b.
• The degree of an operator refers to the number of operands it takes .
Arithmetic 14

Increment / Decrement Operators


Example:

int c;
c = 5;
cout << c << endl; // outputs 5
cout << c++ << endl; // outputs 5 (then increments)
cout << c << endl << endl; // outputs 6
c = 5;
cout << c << endl; // outputs 5
cout << ++c << endl; // outputs 6 (after incrementing)
cout << c << endl; // outputs 6
Grade Point Average Arithmetic 15

// This program will calculate grade point average.


#include <iostream>
void main(){
int A, B, C, D, F;
float sum, GPA;
cout << "Tell me your grades and I will calculate your GPA.";
cout << endl << endl;
cout << "How many units of A? ";
cin >> A;
cout << "How many units of B? ";
cin >> B;
cout << "How many units of C? ";
cin >> C;
cout << "How many units of D? ";
cin >> D;
cout << "How many units of F? ";
cin >> F;
sum = A + B + C + D + F;
GPA = (4*A + 3*B + 2*C + D)/sum;
cout << endl;
cout << "Your grade point average is " << GPA <<endl;

}
Arithmetic 18

Functions in the math library


• To use these built-in functions we need to include the
<math.h>
function
header filewhat it does returned value

abs(a) absolute value of a same data type as argument

pow(a1,a2) a1 raised to the power of a2 data type of argument a1

sqrt(a) square root of a same data type as argument

sin(a) sine of a (a in radians) double

cos(a) cosine double

tan(a) tangent double

log(a) natural logarithm of a Double

log10(a) base 10 log of a Double

exp(a) e raised to the power of a Double


Arithmetic 19

Relational operator
• A Boolean expression is an expression that is either
true or false.
• Boolean expressions are evaluated using relational
operations such as:-
==
!=
<
>
<=
>=
Arithmetic 20

Logical operator
Combine two relational expressions
• &&
• ||
•!
Arithmetic 21
Arithmetic 22

Boolean Expressions
Boolean expressions are evaluated using values from the truth table

For example, if y is 8 then the expression

!((y<3) ||(y>7)) is evaluated in the following sequence

!(false)||(true)

!(true)

false
Arithmetic 23

Review

• binary operator • operand


• degree of an operator • operator
• expression • unary operator

You might also like