Professional Documents
Culture Documents
Lecture 3a
Operators and Expressions
What are operators and expressions?
Operators
01 02 03 04
We can only use the This is possible because In the statement: So if we write
addition and data ( character or char ch = ‘A’; ch=ch+1;We are
subtraction operator symbol) is stored in a the ASCII equivalent actually adding 1 to 65,
with variables of char char variable as an of ‘A’ i.e. 65 is stored which becomes 66. Thus
data type. integer value equal to in ch. ch will now store the
the ASCII code of the letter ‘B’ whose ASCII
character. code is 66
Arithmetic operators and char data
Relational
Operators
• Relational operators are used Operator Description Expression
X = 20;
In the example the variable x is assigned a value 20.
Variable Value
The variable is always on the left hand side and the value on the right
hand side.
The value can be another variable or an
expression. For eg:
a=b; the value stored in variable b is assigned to a
p=q+r; the sum of q and r is stored in the variable p
C++ Shorthands
++a; (prefix)
a++;
(postfix)
Working
code
a b 1
a=a+1
int a,b;
a=3; 2
b=a
b=++a; 4
3
cout<<“a= ”<<a<<“\tb= ”<<b; 4 2
1
Explanation
Working
code
a b 1
b=a
int a, b ; a=a+1
a=3; 2
b=a++; 3
3
cout<<“a= “<<a<<“\tb= “<<b; 4 1
2
Explanation
Relationa x>y
z!=p
Expressions l
x >y && p<=z
Logical b>9 || v!=6
Type
Conversions
• Data type conversions take place when an expression contains
variables of different numeric data types eg. int to float, float to
double etc.
• There are two types of conversions:
Type
Conversions
This will give the output as 65 which is the ASCII code of ‘A’
Here we are forcing ch which is a char variable to behave like an
integer by casting it as (int)
Type conversion: the order of
conversion
Data types
long double
double
float Order of conversion of variables:
long int Smallest to largest
int
char
Type Casting: Example Program
Dry Run
y=++x
x=98 to 98
y=98
z=++y
y=99
z=99
d=++z+1
z=100
d=100+1
d=101