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

PF Lecture 3

Uploaded by

zeeshanilaghari
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PF Lecture 3

Uploaded by

zeeshanilaghari
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Programming Fundamentals

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.

 They are given an initial value though.

 const int size = 10;

 size = 12; // this statement would produce an error


 // Error: assignment of read-only variable size

 Helps in prevention of accidental change to a variable.


Operators
 In C/C++, there are many kinds of operators. These are:

 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.

 Consider the following examples.

 int a = 10; Output would be:


 a++; 11
 cout << a;

 int a = 10;
Output would be:
 ++a; 11
 cout << a;
Pre and Post fix Operations
 So what is the difference ?

 Consider this example:

 int x = 10;
 int y = x++; Output would be:
 cout << y << endl; 10
 cout << x; 11

 int x = 10; Output would be:


 int y = ++x; 11
 cout << y << endl; 11
 cout << x;
Addition/Subtraction/Multiplication/Division

 int x = 30, y = 20, z;


z = x + y;
cout << "Result of addition: " << z << endl;
z = x - y;
cout << "Result of subtraction: " << z << endl;
z = x * y;
cout << "Result of multiplication: " << z << endl;
z = x / y;
cout << "Result of division: " << z << endl;
Output
Result of Division ?
 Why is the result 1 and not 1.5?

 int x = 30, y = 20;


double z;
z = x / y;
cout << "Result of division: " << z << endl;

 What about now ?


Division
 For proper division, at least one of the operands would
be of type double.

 int x = 30, y = 20;


double z;
z = double(x) / y;
cout << "Result of division: " << z << endl;
Division
The Modulus Operator
 % is called the modulus operator.

 Modulus operator calculates the


remainder of two numbers.

 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

 Used for manual data type conversion


 Useful for floating point division using ints: float m =
static_cast<float>(y2-y1)
/(x2-x1);
 Useful to see int value of a char variable:
char ch = 'C';
cout << ch << " is "
<< static_cast<int>(ch);
C-Style and Prestandard Type Cast
Expressions
 C-Style cast: data type name in ()
cout << ch << " is " << (int)ch;
 Prestandard C++ cast: value in ()
cout << ch << " is " << int(ch);
 Both are still supported in C++, although static_cast
is preferred
Example
#include <iostream>
Using namespace std;
Void main()
{
int months, books;
float perMonth;
cout << "How many books do you plan to read? ";
cin >> books;
cout << "How many months will it take you to read them? ";
cin >> months;
perMonth = float(books) / months;
cout << "That is " << perMonth << " books per month.\n";
}
Typecast Warnings
 In Program 3-11, the following statement would still have resulted in
integer division:
 perMonth = float(books / months);//Incorrect
 perMonth = float(books) / months;//Correct
Math Library
 Math library contains functions such as for square root,
power, sin etc.
 Explore this library and see which functions are available
in math C++ library.
Math Library
 // C++ program to illustrate some of the
 // above mentioned functions
 #include <iostream>
 #include <math.h>
 using namespace std;
 int main()
 {
 double x = 2.3;
 cout << "Sine value of x=2.3 : " << sin(x) << endl;
 cout << "Cosine value of x=2.3 : " << cos(x) << endl;
 cout << "Tangent value of x=2.3 : " << tan(x) << endl;

 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;

float x = 57.3; // in degrees


cout << "Hyperbolic Cosine of x=57.3 : " << cosh(x) << endl;
cout << "Hyperbolic tangent of x=57.3 : " << tanh(x) << endl;

y = 100.0;
// Natural base with 'e'
cout << "Log value of y=100.0 is : " << log(y) << endl;

 return 0;
 }

You might also like