PF Lecture 3
PF Lecture 3
Lecture 3
Expressions
Any Questions from Last Lecture ??
Increment and Decrement Operators
Pre-increment ++x;
equivalent to x = x + 1;
Pre-decrement --x;
Changes the value before execution of a statement
y = ++x;
Post-increment int Val++;
Post-decrement int Val--;
Changes the value after execution of the statement
y = x++;
3
Constant Variables
Variables of type const may not be changed by your
program.
Assignment Operator
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operator
General Form
variable = expression;
Example:
a = a + 1;
a += 1;
a = b + c;
a = (b + (d – (x * y)));
Multiple Assignments
int x, y, z;
x = y = z = 0;
OR
int x = 0, y = 0, z = 0;
Arithmetic Operators
- (Subtraction, also called as unary minus)
+ (Addition)
* (Multiplication)
/ (Division)
% (Modulus)
-- (Decrement)
++ (Increment)
Pre and Post fix Operations
The ++ operator is called as increment operator.
The – operator is called as decrement operator.
int a = 10;
Output would be:
++a; 11
cout << a;
Pre and Post fix Operations
So what is the difference ?
int x = 10;
int y = x++; Output would be:
cout << y << endl; 10
cout << x; 11
10 % 2
=0
Calculate for following
11 % 2
=1 23/3?
13 % 3
27/9?
=1
14 % 4
37/5?
=2
19 % 5 8/11?
=4 3/13?
9 % 11
=9
Sample Code
#include <iostream>
using namespace std;
int main()
{
cout << "Result of Modulus of 10 % 2 = " << (10 % 2) << endl;
cout << "Result of Modulus of 11 % 2 = " << (11 % 2) << endl;
cout << "Result of Modulus of 13 % 3 = " << (13 % 3) << endl;
cout << "Result of Modulus of 14 % 4 = " << (14 % 4) << endl;
cout << "Result of Modulus of 19 % 5 = " << (19 % 5) << endl;
cout << "Result of Modulus of 9 % 11 = " << (9 % 11) << endl;
system("pause");
return 0;
}
Output
When You Mix Apples and Oranges: Type
Conversion
Operations are performed between operands of the
same type.
If not of the same type, C++ will convert one to be the
type of the other
This can impact the results of calculations.
Type Conversion
Type Conversion: automatic conversion of an operand to
another data type
Promotion: convert to a higher type
Demotion: convert to a lower type
Conversion Rules
1) Char automatically promoted to int
2) When operating on values of different data types, the
lower one is promoted to the type of the higher one.
3) When using the = operator, the type of expression on
right will be converted to type of variable on left
Type Casting
double y = 0.25;
cout << "Square root value of y=0.25 : " << sqrt(y) << endl;
int z = -10;
cout << "Absolute value of z=-10 : " << abs(z) << endl;
cout << "Power value: x^y = (2.3^0.25) : " << pow(x, y) << endl;
Math Library
float x = 3.0;
float y = 4.0;
cout << "Hypotenuse having other two sides as x=3.0 and"
<< " y=4.0 : " << hypot(x, y) << endl;
x = 4.56;
cout << "Floor value of x=4.56 is : " << floor(x) << endl;
x = -4.57;
cout << "Absolute value of x=-4.57 is : " << fabs(x) << endl;
x = 1.0;
cout << "Arc Cosine value of x=1.0 : " << acos(x) << endl;
cout << "Arc Sine value of x=1.0 : " << asin(x) << endl;
cout << "Arc Tangent value of x=1.0 : " << atan(x) << endl;
Math Library
float y = 12.3;
cout << "Ceiling value of y=12.3 : " << ceil(y) << endl;
y = 100.0;
// Natural base with 'e'
cout << "Log value of y=100.0 is : " << log(y) << endl;
return 0;
}