Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

L2 - Variables & Declarations, Data, and Arithmetic Operations

Uploaded by

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

L2 - Variables & Declarations, Data, and Arithmetic Operations

Uploaded by

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

CE 204: Computer Programming Sessional

Data
Arithmetic Operations 1
Ahmed Farhan Ahnaf Siddique

Assistant Professor,
Department of Civil Engineering,
BUET

2
Variables and Their Declarations
#include <iostream>
using namespace std;
int main()
{ // prints "m = 44 and n = 77":
int m, n;
m = 44; // assigns the value 44 to the variable m
cout << "m = " << m;
n = m + 33; // assigns the value 77 to the variable n
cout << " and n = " << n << endl;
return 0;}
➢ Both m and n are declared on the same line
➢ Any number of variables can be declared
together this way if they have the same type
3
Variables and Their Declarations
➢ Every variable in a C++ program must be
declared before it is used.

➢ The syntax is specifier type name initializer;


➢ Where specifier is an optional keyword such as
const

➢ type is one of the C++ data types such as int


➢ name is the name of the variable
➢ initializer is an optional initialization clause such
as = 44

4
Input Operator
➢ The input operator >> (also called the get
operator or the extraction operator) works like
the output operator <<
#include <iostream>
using namespace std;
int main()
{ // tests the input of integers,floats,and characters:
int m,n;
cout << "Enter two integers: ";
cin >> m >> n;
cout << "m = " << m << ", n = " << n << endl;
double x,y,z;
5
Input Operator

cout << "Enter three decimal numbers: ";


cin >> x >> y >> z;
cout << "x = " << x << ", y = " << y << ", z = " << z <<
endl;
char c1,c2,c3,c4 ;
cout << "Enter four characters: ";
cin >> c1 >> c2 >> c3 >> c4;
cout << "c1 = " << c1 << ", c2 = " << c2 << ", c3 = "
<< c3 << ", c4 = " << c4 << endl;
return 0;
}

6
Size of Different Data Types
• The number of bites used for data types vary from
compiler to compiler, however we can get this
information by using the sizeof() operator

• sizeof() is a unary operator so it takes a single


operand and it returns an integer value that
represents the number of bytes occupied by a type
or by a variable

• you can apply sizeof operator even to an


expression and the expression does not necessarily
have to be within parenthesis
7
Size of Different Data Types
#include <iostream>
using namespace std;
int main()
{
cout<<"Size (number of bytes) of different data
types\n\n";
cout<<"Size of \'int \'= "<<sizeof(int)<<endl;
cout<<"Size of \'double =\'="
<<sizeof(double)<<endl;
cout<<"Size of \'short \' = "<<sizeof(short)<<endl;
cout<<"Size of \'long \' = "<<sizeof(long)<<endl;
cout<<"Size of \'float \' = "<<sizeof(float)<<endl;
8
Size of Different Data Types

cout<<"Size of \'char \' = "<<sizeof(char)<<endl;


cout<<"Size of \'long double \' = "<<sizeof(long
double)<<endl;
float weight=25.0;
int serial_no=5;
cout<<"Size of \'weight \' = "<<sizeof weight<<endl;
cout<<"Size of \'serial_no \' =
"<<sizeof(serial_no)<<endl;
}

9
Size of Different Data Types

10
Arithmetic Operators
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (the remainder after division)

• For division, if the two operands are integer


(number or variable) then the expression returns
only the integer part of the actual result

• The operands of % must be integer


• Note: 5/2 -> 2, 5.0/2 -> 2.5, 5./2 -> 2.5, 5/2.0 -> 2.5

5%2 -> 1, 10%4 -> 2, 15%3 -> 0 11


Precedence and Associativity

• In an expression involving several different


operators, the order in which the operators are
executed is determined by their precedence rule.

▪ * / % are of higher precedence than + and –


▪ * / % are of equal precedence
▪ + and – are of equal precedence
• Suppose we have written an expression
4*5/3%4+7/3

• Which operator will be executed first?


12
Precedence and Associativity
• However, we can overrule the precedence by use of
parenthesis.

• When there are consecutive operators of equal


precedence (such as in the expression 4 * 10 / 6 % 4)
then the sequence in which these will be executed
is determined by their associativity.

• * / % are left associative and these will be executed


from left to right.

• + and – are also left associative.


13
Precedence and Associativity

#include <iostream>

using namespace std;

int main()

cout<<endl<<endl<<" 20/4/2 = "<<20/4/2;

cout<<endl<<endl<<" (20/4)/2 = "<<(20/4)/2;

cout<<endl<<endl<<" 20/(4/2) = "<<20/(4/2)<<endl;

14
Precedence and Associativity

15
Precedence and Associativity
#include <iostream>

using namespace std;

int main()

cout<<endl<<" 4*10/6%4 ="<<4*10/6%4;

cout<<endl<<" 4 * 10 = "<<4*10;

cout<<endl<<" 40 / 6 = "<<40/6;

cout<<endl<<" 6 % 4 = "<<6%4;

cout<<endl<<" ((4*10)/6)%4 ="<<((4*10)/6)%4;


16
Precedence and Associativity

cout<<endl<<" If * / % were right associative then"

<<endl<<" the following would have occurred";

cout<<endl<<" 6 % 4 ="<<6%4;

cout<<endl<<" 10 / 2 ="<<10/2;

cout<<endl<<" 4 * 5 ="<<4*5;

cout<<endl<<" 4*(10/(6%4)) ="<<4*(10/(6%4))<<endl;

17
Precedence and Associativity

18
Precedence and Associativity

• However, in an expression such as

4*5/3%4+7/3

we cannot say whether the sub-expression

▪ 4 * 5 / 3 % 4 will be evaluated first or,


▪ 7 / 3 will be evaluated first

• It depends on the compiler, and we don’t need to


know it because the result is same for both the
cases.

19
Assignment Operator
int age;

age = 25;

• 25 is stored in the integer memory location


named age.

• We cannot write statements like:

25 = age;

• The left side of the “=” operator must be a single


variable.

20
Assignment Operator
a = 5.2;

b = a + 4;

• The right side is first evaluated and then the result is


stored in the variable to the left.

int weight;

weight = 25.5;

cout<<weight;

• The output will be 25 not 25.5 because weight is


declared as int type.
21
Assignment Operator
• We can write statements as below :

length = width = 5;

apples = oranges = 50;

a = b = c / 2.2;

• Remember the “=” operator is right associative

• Therefore, in the first one of the above examples 5 is


first stored in width and then the value in width is
stored in length.

• So, it is effectively

length = (width = 5); 22


Assignment Operator
fruits = ( oranges = 10) + ( apples = 11) ;

• This will store 10 in oranges, 11 in apples, then add


the two together and store the result in fruits.

• Suppose part of a program is as below:


double length;

length = 5.5;

length = 7.0;

cout << length;

• You will get 7.0 as output.


23
Assignment Operator
a = a+2;

• This statement will first evaluate a+2 and then store


the value back into variable a (it is implied that the
variable a must have been assigned a value before
this statement)

• Instead of the above statement we can write a+=2;


this statement has the same effect.

• a *= b+5; is same as a =a * (b+5);

• length /= 5; is same as length = length /5;


24
Composite Assignment Operators

• The standard assignment operator in C++ is the equals


sign “=”

• In addition to this operator, C++ also includes the


following composite assignment operators: +=, -=, *=,
/=, and %=

• When applied to a variable on the left, each applies the


indicated arithmetic operation to it using the value of
the expression on the right.

25
Composite Assignment Operators
#include <iostream>

using namespace std;

int main()

{ // tests arithmetic assignment operators:

int n=22;

cout << "n = " << n << endl;

n += 9; // adds 9 to n

cout << "After n += 9, n = " << n << endl;

n -= 5; // subtracts 5 from n
26
Composite Assignment Operators
cout << "After n -= 5, n = " << n << endl;

n *= 2; // multiplies n by 2

cout << "After n *= 2, n = " << n << endl;

n /= 3; // divides n by 3

cout << "After n /= 3, n = " << n << endl;

n %= 7; // reduces n to the remainder from dividing


by 7

cout << "After n %= 7, n = " << n << endl;

}
27
Composite Assignment Operators

28
Library Functions

29
Thank You!
30

You might also like