Module 2
Module 2
(22ECS145/245)
Module 2
• Operators in C, Type conversion and typecasting
• Decision control and Looping statements: Introduction to decision
control, Conditional branching statements, iterative statements,
nested loops, break and continue statements, goto statement.
Operator Meaning
== Returns 1 if both operands are equal, 0 otherwise
!= Returns 1 if operands do not have the same value, 0 otherwise
Logical Operators
• These are used to test more than one condition and make decision.
The different logical operators are:
• Logical NOT
• Logical AND
• Logical OR
Logical NOT (!) The output is true when input is false and vice versa. It
accepts only one input.
Input Output
X !X
0 1
1 0
Logical Operators
• Logical AND (&&) The output is true only if both inputs are true. It
accepts two or more inputs.
Input Output
X Y X && Y
0 0 0
0 1 0
1 0 0
1 1 1
• Logical OR (||) The output is true only if any of its input is true. It
accepts two or more inputs.
Input Output
X Y X || Y
0 0 0
0 1 1
1 0 1
1 1 1
Unary Operators
Unary Minus Operator
Example: int a, b =100; a=-(b); Result: a=-10
Increment (++)
Pre increment:
Example: If a=8, b= ++a what will be the value of a and b?
Post increment
Example: If q=6, p= q++ what will be the value of p and q?
Decrement (--)
Pre decrement Example: If y=4, x= --y what will be the value of x and y?
Post decrement Example: If n=5, m= n-- what will be the value of m and
n?
Conditional Operator
It takes three arguments
Expression1 ? Expression2 : Expression3
Where,
Expression1→ Condition
Expression2→ Statement followed if condition is true
Expression3→ Statement followed if condition is false
Example:
large = (4 > 2) ? 4: 2 →large = 4
Bitwise Operators
These works on bits and performs bit by bit operations.
The different types of bitwise operators are:
1 1 1 1 0
X 0 1 0 0 0 1 1 0
X<<2 0 0 0 1 1 0 0 0
X 0 1 0 0 0 1 1 0
X>>2 0 0 0 1 0 0 0 1
Assignment Operators
The assignment operator is used to assign the values to the variables
on the left hand side. The symbol “=” is used as an assignment
operator.
x = 10, c = a+b
Shorthand Assignment:
x = x+2 → x+=2
Multiple Assignment
a=10,b=10,c=10 → a=b=c=10
Comma Operators & sizeof operators
• Comma Operator: It can be used as operator in expression and as
separator in declaring variables.
• sizeof() operator: It is used to determine the size of variable or value
in bytes.
Type Conversion and Typecasting
Type conversion is done when the expression has variables of different
data types.
The data type is promoted from lower to higher level where the
hierarchy of data types are
Short/char int unsigned int long int unsigned long int float
double long double
Type Conversion and Typecasting
• Typecasting is also known as forced conversion.
• Typecasting an arithmetic expression tells the compiler to represent
the value of the expression.
• Higher data type has to be converted into the value of a lower data
type.
Example:
Float salary=10000.00;
Int sal;
Sal=(int) salary;
Output:
10000