Você está na página 1de 55

Operators

Operators are the symbols that are used to


perform certain operation on data
C++ provides variety of operator

Arithmetic operator
Relational operator
Logical operators
Bitwise operators

Arithmetic operators

The five arithmetical operations supported by


the C++ language are
Arithmetic operator is a symbol that perform
mathematical operation on data
Example

+ for addition e.g. a+b


- for subtraction e.g. a-b
* for multiplication e.g. a*b
/ for division e.g. a/b
%for modulo e.g. a%b

%for modulo

Modulo is the operation that gives the


remainder of a division of two values. For
example, if we write:
a = 11 % 3;
The variable a will contain the value 2, since
2 is the remainder from dividing 11 between
3.

Question

Write a program that performs all


mathematical operation on two variables

Assignment (=)

The assignment operator assigns a value to a


variable. a = 5;
This statement assigns the integer value 5 to the
variable a.
The part at the left of the assignment operator (=) is
known as the lvalue (left value) and the right one as
the rvalue (right value).
The lvalue has to be a variable whereas the rvalue
can be either a constant, a variable, the result of an
operation or any combination of these.
The most important rule when assigning is the rightto-left rule: The assignment operation always takes
place from right to left, and never the other way:

Example

// assignment operator
#include <iostream>
int main ()
{ int a, b;
a = 10;
b = 4;
A=b
b = 7;
cout << "a:";
cout << a;
cout << " b:";
cout << b;
return 0; }

Compound Assignment
statement
An assignment that assign a value to many

variables is known as compound assignment


operator.
Example

A=b=20
X=y=z=100

Compound assignment
Operator

When we want to modify the value of a


variable by performing an operation on the
value currently stored in that variable we can
use compound assignment operators:
C++ provide Compound assignment
Operator that combine assignment operator
with arithmetic operator

Syntax

Variable op=expression
Example

N+=10 = N=N+1

List of Compound assignment Operator


(+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

Example
int main ()
{ int a, b=3;
a = b; a+=2; // equivalent to a=a+2
cout << a;
return 0; }

Example

Prefix

B=3;
A=--B;
// A contains 2, B contains 2

Postfix

B=3;
A=B--;
// A contains 3, B contains 2

Relational and equality


operators
In order to evaluate a comparison between

two expressions we can use the relational


and equality operators.
The result of a relational operation is a
Boolean value that can only be true or false,
according to its Boolean result.
We may want to compare two expressions,
for example, to know if they are equal or if
one is greater than the other is.

Example

==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example

7 == 5) // evaluates to false.
(5 > 4) // evaluates to true.
(3 != 2) // evaluates to true.
(6 >= 6) // evaluates to true.
(5 < 5) // evaluates to false.

Example 2

(a == 5) // evaluates to false since a is not


equal to 5.
(a*b >= c) // evaluates to true since (2*3 >= 6)
is true.
(b+4 > a*c) // evaluates to false since (3+4 >
2*6) is false.
((b=2) == a) // evaluates to true.

Be careful

The operator = (one equal sign) is not the same as


the operator == (two equal signs)
the first one is an assignment operator (assigns the
value at its right to the variable at its left)
and the other one (==) is the equality operator that
compares whether both expressions in the two sides
of it are equal to each other.
Thus, in the last expression ((b=2) == a), we first
assigned the value 2 to b and then we compared it
to a, that also stores the value 2, so the result of the
operation is true.

Logical operators ( !, &&, || )

The Operator ! is the C++ operator to perform


the Boolean operation NOT, it has only one
operand, located at its right, and the only
thing that it does is to inverse the value of it,
producing false if its operand is true and true
if its operand is false.
Basically, it returns the opposite Boolean
value of evaluating its operand.

logical operators && and ||

The logical operators && and || are used


when evaluating two expressions to obtain a
single relational result.
The operator && corresponds with Boolean
logical operation AND.
This operation results true if both its two
operands are true, and false otherwise.

Example

The following panel shows the result of


operator && evaluating the expression a &&
b:
&& OPERATOR
a
b
a && b
true
true
true
True
false false
False true false
False false false

Example 2

The operator || corresponds with Boolean logical


operation OR.
This operation results true if either one of its two
operands is true, thus being false only when both
operands are false themselves. Here are the
possible results of a || b:
&& OPERATOR
A
b
a || b
true true
true
True false
true
False true
true
False false
false

For example:

( (5 == 5) && (3 > 6) ) // evaluates to false


( true && false ).
( (5 == 5) || (3 > 6) ) // evaluates to true
( true || false ).

Precedence of operators

When writing complex expressions with several


operands, we may have some doubts about which
operand is evaluated first and which later. For
example, in this expression:
a=5+7%2
we may doubt if it really means:
a = 5 + (7 % 2) // with a result of 6,
or a = (5 + 7) % 2 // with a result of 0
The correct answer is the first of the two
expressions, with a result of 6.

Operator Precedence (contd.)

Example:

10 * (24 / (5 - 2) ) + 13

10 * ( 24 / 3 ) + 13
10 * 8 + 13
80 + 13
93

Operator Precedence (contd.)

The order of precedence in C ++


language is as follows:

Any expression given in parenthesis is


evaluated first.
Then multiplication * and division /
operators are evaluated.
Then plus + and minus operators are
evaluated.
In case of parenthesis within parenthesis,
the expression of the inner parenthesis will
be evaluated first.

Operator Associativity

The order in which operators has same


precedence are evaluated is known as
operator associativity.
If an expression contains some
operators that have same precedence
level, the expression is evaluated from
left-to-right or right-to-left.

Operator Associativity
(contd.)

Operator associativity in C ++ language


is as follows:

Operators
()
++(postfix)
-(postfix)

Associativity
Left-to-right

+(unary) -(unary) ++(prefix) -(prefix)

Left-to-right

*
+
=

Left-to-right
Left-to-right
Right-to-left

+=

-=

*=

/=

Lvalue and Rvalue

Lvalue

It is an operand that can be written on the


left side of assignment operator =.

Rvalue

It is an operand that can be written on the


right side of assignment operator =.

Example

a = 5 + 7 % 2;might be written either as:


a = 5 + (7 % 2);
a = (5 + 7) % 2;

Example 2

(a == 5) // evaluates to false since a is not


equal to 5.
(a*b >= c) // evaluates to true since (2*3 >= 6)
is true.
(b+4 > a*c) // evaluates to false since (3+4 >
2*6) is false.
((b=2) == a) // evaluates to true.

Escape Sequences

These are special characters used in control string to


modify the format of output.
Different escape sequences are as follows:

Escape Sequence
\a
\b
\f
\n
\t
\
\

Purpose
Alarm
Backspace
Form feed
Carriage return
Tab
Single quote
Double quote

Type casting

The process of converting the data type of a


value during execution is known as type
casting
Two types of type casting

Implicit type casting


Explicit type casting

Implicit type casting

Implicit type casting is performed


automatically by the c++ compiler.

Implicit Type Casting

It is performed automatically by C++


compiler. e.g. char + float float
Highest data type
long double
double
float
long
int
char
Lowest data type

Explicit type casting

Explicit type casting is performed by the


programmer
It is performed by using cast operator
The cast operator tells the computer to
convert the data type of a value
Syntax

(type) expression

Example

Float a,b
Int c;
A=10.3
B=5.2
c=(int)a % (int)b;
Cout<<c;

Result

Shows 0

The sizeof operator

The sizeof operator is used to find the size of any


data value
It gives the number of types occupied by the value
Syntax

Sizeof(operand)

Example
cout<<sizeof(int)
Result

comments

Comments are line of program that are not


executed
Compiler ignore comments and does not
include them in executable program
Comments can be added anywhere in
program in two ways
Single line comments \\
Multiline comments /*
*/

Basic Input/Output

The standard C++ library includes the header


file iostream, where the standard input and
output stream objects are declared.

Standard Input

The standard input device is usually the


keyboard. Handling the standard input in C++
is done by applying the overloaded operator
of extraction (>>) on the cin stream. The
operator must be followed by the variable that
will store the data that is going to be
extracted from the stream. For example:
int age; cin >> age;

Example

include <iostream>
int main ()
{ int i; cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n"; return
0; }

Lab work

Write a program that adds two floating point


numbers and shows the sum on screen
Write a program to calculate and print the
area of a square

C++ Manipulator

C++ manipulator are used to format the


output in different styles.
The manipulators are the most common way
to control output formatting

Endl
Setw
Showpoint

Endl manipulator

The endl stands for end line


The endl manipulator is used to move cursor
to the beginning of next line

Example

Cout<<hello<<endl<<comsats

Constants

It is a quantity that cannot be changed


during program execution.
Two types of constants.

Literal constant
Symbolic constant

Literal Constant

It is a value that is typed directly in a


program.
For example

int age = 19 ;

Types of Literal Constants

Integer constant
Floating point constant
Character constant
String constant

e.g. 87
e.g. 10.22F
e.g. A
e.g. Pakistan

Symbolic Constants

It is a name given to values that cannot


be changed.
It can be declared in two ways.

const Qualifier

e.g

const data_type identifier = value ;


const
int
N
= 100 ;

Define Directive

e.g

# define identifier
value ;
# define
Pl
3.141593 ;

Expression

It is a statement that evaluates to a


value.
It consists of operators and operands.

e.g

A+B;
Operands

Operator

Program
#include <iostream.h>
void main()
{
char ch1, ch2, sum;
ch1 = 2 ;
ch2 = 6 ;
sum = ch1 + ch2 ;
cout<<Sum =<<sum;
}

Output

104
Because ASCII values of 2 and6 are
50 and 54

Program
#include <iostream.h>
#incldue<conio.h>
void main()
{
clrscr();
short var1 = 32767;
cout << var1 << endl;
var1= var1 +1 ;
cout <<var1 << endl;
var1 = var1 - 1 ;
cout << var1 << endl;
getch();
}

Output

32767
- 32768
32767
Because range of short is -32768 to
32767.

Program
#include <iostream.h>
#incldue<conio.h>
#define PI 3.141
void main()
{
float r, area;
clrscr();
cout << Enter radius:;
cin>> r;
area = 2.0 * PI * r;
cout << Area= << area;
getch();
}

Output

User will give input, then Area will


be displayed on the screen.

Program
#include <iostream.h>
#incldue<conio.h>
void main()
{
clrscr();
int a,b;
a = 10;
b = 5;
cout << a+b =<< a+b << endl;
cout << a-b =<< a-b << endl;
cout << a*b =<< a*b << endl;
cout << a/b =<< a/b << endl;
cout << a%b =<< a%b << endl;
getch();
}

Output

a+b =15
a-b =5
a*b =50
a/b =2
a%b =0

Você também pode gostar