Sunteți pe pagina 1din 11

Introduction to Computer Programming (ICP)

Lecture

6|7

Operators

n operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

C++ is rich in built-in operators and provides the following types of operators:

Arithmetic Operators

Relational Operators

Logical Operators

Assignment Operators

Arithmetic Operators:
There are following arithmetic operators supported by C++ language:
Assume variable A holds 10 and variable B holds 20, then:
Operator

Description

Example

Adds two operands

A + B will give 30

Subtracts second operand from the first

A - B will give -10

Multiplies both operands

A * B will give 200

Divides numerator by de-numerator

B / A will give 2

Modulus Operator and remainder of after an integer division


%

B % A will give 0

Lecturer: ARSHAD FARHAD, CS Department | email: arshadfarhad@ciitsahiwal.edu.pk

Introduction to Computer Programming (ICP)

Increment and Decrement operators


The increment operator ++ adds 1 to its operand, and the decrement operator -- subtracts 1 from its operand. Thus:
x = x+1;
is the same as
x++;
And similarly:
x = x-1;
is the same as
x--;
Both the increment and decrement operators can either precede (prefix) or follow (postfix) the operand. For example:
x = x+1;
can be written as
++x;// prefix form
or as:
x++;// postfix form
When an increment or decrement is used as part of an expression, there is an important difference in prefix and postfix
forms. If you are using prefix form, then increment or decrement will be done before rest of the expression, and if you
are using postfix form, then increment or decrement will be done after the complete expression is evaluated.

Example:
Following is the example to understand this difference:
#include<iostream>
Using namespace std;
int main()
{
int a=21;
int c ;
// Value of a will not be increased before assignment.
c = a++;
cout <<"Line 1 - Value of a++ is :"<< c << endl ;
// After expression value of a is increased
cout <<"Line 2 - Value of a is :"<< a << endl ;
// Value of a will be increased before assignment.
c =++a;
cout <<"Line 3 - Value of ++a is :"<< c << endl ;
return0;
}

Lecturer: ARSHAD FARHAD, CS Department | email: arshadfarhad@ciitsahiwal.edu.pk

Introduction to Computer Programming (ICP)

When the above code is compiled and executed, it produces the following result:
Line1-Value of a++is:21
Line2-Value of a is:22
Line3-Value of ++a is:23

Example
Try the following example to understand all the arithmetic operators available in C++.

#include<iostream>
using namespace std;
int main()
{
int a=21;
int b=10;
int c ;
c = a + b;
cout <<"Line
c = a - b;
cout <<"Line
c = a * b;
cout <<"Line
c = a / b;
cout <<"Line
c = a % b;
cout <<"Line
c = a++;
cout <<"Line
c = a--;
cout <<"Line
return0;
}

1 - Value of c is :"<< c << endl ;


2 - Value of c is

:"<< c << endl ;

3 - Value of c is :"<< c << endl ;


4 - Value of c is

:"<< c << endl ;

5 - Value of c is

:"<< c << endl ;

6 - Value of c is :"<< c << endl ;


7 - Value of c is

:"<< c << endl ;

When the above code is compiled and executed, it produces the following result:
Line1-Value
Line2-Value
Line3-Value
Line4-Value
Line5-Value
Line6-Value
Line7-Value

of
of
of
of
of
of
of

c
c
c
c
c
c
c

is:31
is:11
is:210
is:2
is:1
is:21
is:22

Lecturer: ARSHAD FARHAD, CS Department | email: arshadfarhad@ciitsahiwal.edu.pk

Introduction to Computer Programming (ICP)

Example:

Relational Operators:
There are following relational operators supported by C++ language
Assume variable A holds 10 and variable B holds 20, then:
Operator

Description

Example

==

Checks if the values of two operands are equal or


not, if yes then condition becomes true.

(A == B) is not true.

!=

Checks if the values of two operands are equal or


not, if values are not equal then condition becomes (A != B) is true.
true.

>

Checks if the value of left operand is greater than


the value of right operand, if yes then condition
becomes true.

(A > B) is not true.

<

Checks if the value of left operand is less than the


value of right operand, if yes then condition
becomes true.

(A < B) is true.

Lecturer: ARSHAD FARHAD, CS Department | email: arshadfarhad@ciitsahiwal.edu.pk

Introduction to Computer Programming (ICP)

>=

Checks if the value of left operand is greater than


or equal to the value of right operand, if yes then
condition becomes true.

(A >= B) is not true.

<=

Checks if the value of left operand is less than or


equal to the value of right operand, if yes then
condition becomes true.

(A <= B) is true.

Example
Try the following example to understand all the relational operators available in C++.
#include<iostream>
Using namespace std;
int main ()
{
int
int
int
if(
{

a
b
c
a

=21;
=10;
;
== b )

cout <<"Line
} else
{
cout <<"Line
}
if( a < b )
{
cout <<"Line
} else
{
cout <<"Line
} if( a > b )

1 - a is equal to b"<< endl ;


1 - a is not equal to b"<< endl ;

2 - a is less than b"<< endl ;


2 - a is not less than b"<< endl ;

{
cout <<"Line 3 - a is greater than b"<< endl ;
} else
{
cout <<"Line 3 - a is not greater than b"<< endl ; }
/* Let's change the values of a and b */
a =5;
b =20; if( a <= b )
{
cout <<"Line 4 - a is either less than \
or equal to b"<< endl ;
}
if( b >= a )
{
cout <<"Line 5 - b is either greater than \
or equal to b"<< endl ;
} return0;
}

When the above code is compiled and executed, it produces the following result:
Line1- a is not equal to b
Line2- a is not less than b
Line3- a is greater than b

Lecturer: ARSHAD FARHAD, CS Department | email: arshadfarhad@ciitsahiwal.edu.pk

Introduction to Computer Programming (ICP)

Line4- a is either less than or equal to b


Line5- b is either greater than or equal to b

Logical Operators:
Expressions that use conditional statements and logical operators. There are following logical
operators supported by C++ language.
Assume variable A holds 1 and variable B holds 0, then:
Operator Description

Example

Called Logical AND operator. A && B is true if and only if both A and B are true
&&

(A && B) is false.
Called Logical OR Operator. A || B is true if either A or B are true

||

(A || B) is true.

!(condition) is true if condition is false, and false if condition is true


!

!(A && B) is true.

Example
#include<iostream>
Using namespace std;
int main()
{
int a =5;
int b =20;
int c ;
if( a && b )
{
cout <<"Line 1 - Condition
}
if( a || b )
{
cout <<"Line 2 - Condition
}
/* Let's change the values of a
a =0;
b =10; if( a && b )
{
cout <<"Line 3 - Condition
} else
{
cout <<"Line 4 - Condition
}
if(!(a && b))
{
cout <<"Line 5 - Condition
} return0;
}

is true"<< endl ;

is true"<< endl ;
and b */
is true"<< endl ;
is not true"<< endl ;

is true"<< endl ;

Lecturer: ARSHAD FARHAD, CS Department | email: arshadfarhad@ciitsahiwal.edu.pk

Introduction to Computer Programming (ICP)

When the above code is compiled and executed, it produces the following result:
Line1-Condition
Line2-Condition
Line4-Condition
Line5-Condition

is
is
is
is

true
true
not true
true

Compound assignment operators:


There are following assignment operators supported by C++ language:
Operator

Description

Example

Simple assignment operator, Assigns values from


right side operands to left side operand

C = A + B will assign value of A + B into C

+=

Add AND assignment operator, It adds right


operand to the left operand and assign the result
to left operand

C += A is equivalent to C = C + A

-=

Subtract AND assignment operator, It subtracts


right operand from the left operand and assign the C -= A is equivalent to C = C - A
result to left operand

*=

Multiply AND assignment operator, It multiplies


right operand with the left operand and assign the
result to left operand

C *= A is equivalent to C = C * A

/=

Divide AND assignment operator, It divides left


operand with the right operand and assign the
result to left operand

C /= A is equivalent to C = C / A

%=

Modulus AND assignment operator, It takes


modulus using two operands and assign the result C %= A is equivalent to C = C % A
to left operand

Example
Try the following example to understand all the assignment operators available in C++.

Lecturer: ARSHAD FARHAD, CS Department | email: arshadfarhad@ciitsahiwal.edu.pk

Introduction to Computer Programming (ICP)

#include<iostream>
using namespace std;
int main()
{
int a =21;
int c;
c = a;
cout <<"Line 1 - =

Operator, Value of c = : "<<c<< endl ;

c += a;
cout <<"Line 2 - += Operator, Value of c = : "<<c<< endl ;
c -= a;
cout <<"Line 3 - -= Operator, Value of c = : "<<c<< endl ;
c *= a;
cout <<"Line 4 - *= Operator, Value of c = : "<<c<< endl ;
c /= a;
cout <<"Line 5 - /= Operator, Value of c = : "<<c<< endl ;
c =200;
c %= a;
cout <<"Line 6 - %= Operator, Value of c = : "<<c<< endl ;
c <=2;
cout <<"Line 7 - <<= Operator, Value of c = : "<<c<< endl ;
c >=2;
cout <<"Line 8 - >>= Operator, Value of c = : "<<c<< endl ;
c =2;
return0;
}
When the above code is compiled and executed, it produces the following result:
Line1-=Operator,Value of c =:21
Line2-+=Operator,Value of c =:42
Line3--=Operator,Value of c =:21
Line4-*=Operator,Value of c =:441
Line5-/=Operator,Value of c =:21
Line6-%=Operator,Value of c =:11
Line7-<<=Operator,Value of c =:44
Line8->>=Operator,Value of c =:11

Sizeof() operator:
The sizeof is a keyword, that determines the size, in bytes, of a variable or data type.
The syntax of using sizeof is as follows:
sizeof(data type)
Where data type is the desired data type including int, long, double etc.
Try the following example to understand all the sizeof operator available in C++. Copy and paste the following C++
program in test.cpp file and compile and run this program.

Lecturer: ARSHAD FARHAD, CS Department | email: arshadfarhad@ciitsahiwal.edu.pk

Introduction to Computer Programming (ICP)

#include<iostream>
usingnamespace std;
int main()
{
cout <<"Size of char : "<<sizeof(char)<< endl;
cout <<"Size of int : "<<sizeof(int)<< endl;
cout <<"Size of float : "<<sizeof(float)<< endl;
cout <<"Size of double : "<<sizeof(double)<< endl;
cout <<"Size of Long double : "<<sizeof(long double)<<
endl;
return0;
}
When the above code is compiled and executed, it produces the following result, which can vary from machine to
machine:
Size
Size
Size
Size
Size

of
of
of
of
of

char:1
int:4
float:4
double:8
Long double:10

Nevertheless, on different machines the size of any data type can vary. This is because of the difference in hardware
architecture and compiler. In below example long double data type value is 16 byte. However, on linux it is 12 byte,
while on Visual studio it is 8 byte. Though, some book claims that long double data type value is 10 byte. Therefore, it
varies from 8 to 16 byte on different platforms and machines.

Output is:

Lecturer: ARSHAD FARHAD, CS Department | email: arshadfarhad@ciitsahiwal.edu.pk

Introduction to Computer Programming (ICP)

Operators Precedence in C++:


Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated.
Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence
than the addition operator:
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first
gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom.
Within an expression, higher precedence operators will be evaluated first.

Example
Try the following example to understand operators precedence concept available in C++.
Check the simple difference with and without parenthesis. This will produce different results because (), /, * and + have
different precedence. Higher precedence operators will be evaluated first:

#include<iostream>
using namespace std;
int main()
{
int a =20;
int b =10;
int c =15;
int d =5;
int e;
e =(a + b)* c / d;// ( 30 * 15 ) / 5
cout <<"Value of (a + b) * c / d is :"<< e << endl ;
e =((a + b)* c)/ d;// (30 * 15 ) / 5
cout <<"Value of ((a + b) * c) / d is

:"<< e << endl ;

e =(a + b)*(c / d);// (30) * (15/5)


cout <<"Value of (a + b) * (c / d) is

:"<< e << endl ;

e = a +(b * c)/ d;// 20 + (150/5)


cout <<"Value of a + (b * c) / d is

:"<< e << endl ;

return0;
}
When the above code is compiled and executed, it produces the following result:
Value
Value
Value
Value

of
of
of
of

(a + b)* c / d is:90
((a + b)* c)/ d is:90
(a + b)*(c / d)is:90
a +(b * c)/ d is:50

Expressions:

A sequence of operators and operands that specifies a computation

Operators

Which performs computation

Lecturer: ARSHAD FARHAD, CS Department | email: arshadfarhad@ciitsahiwal.edu.pk

Introduction to Computer Programming (ICP)

E.g. *, +, /, - etc.

Operand

On which computation is being performed

E.g. c = a + b; //a & b are operands & + is an operator

c = 7 + 8; // c=15

Escape Sequence:

Lecturer: ARSHAD FARHAD, CS Department | email: arshadfarhad@ciitsahiwal.edu.pk

S-ar putea să vă placă și