Pop Module2 Notes
Pop Module2 Notes
Pop Module2 Notes
MODULE 2
Operators and Expressions
Operator: Operator is a symbol (or token) that specifies the operation to be performed
on various types of data.
Operand: A variable or a value on which the operation is performed is an operand.
Expression: A sequence of operands and operators that reduces to a single value is an
expression.
Operands
a + b Expression
a _ b
Operators
Classification of operators
The operators in C can be classified based on:
The number of operands an operator has.
The type of operation being performed.
i) Unary operator: An operator which acts on only one operand to produce the result is
called Unary operator.
Ex: -10,-a,*b,++a,a++,b-- etc.
ii) Binary operator: An operator which acts on two operands to produce the result is
called Binary operator.
Ex: a+b, a*b, 10/5 etc
iii) Ternary operator: An operator which acts on three operands to produce the result is
called Ternary operator.
Ex: a ? b : c;
i) Arithmetic Operators
The operators that are used to perform arithmetic operations such as addition,
subtraction, multiplication, division and modulus are called arithmetic operators.
These operators perform operations on two operands and hence they are called binary
operators.
Subtraction - 4-2 2
Division / 4/2 2
Modulus % 4%2 0
(Remainder)
% (modulus operator) divides the first operand by second and returns the remainder.
% (modulus operator) cannot be applied to floating or double.
% operator returns remaining value (remainder) of an integer division.
ii) Assignment Operators
An operator which is used to assign the data or result of an expression into a variable
(also called memory location) is called an assignment operator.
Assignment operator is denoted by ‘=’ sign.
Ex: a=b; //value of b is copied into variable a
variable= expression;
Ex: a=10;
a=b;
a=a+b;
Area= l*b; //Result of Expression l*b is copied into variable area.
1. Post Increment
It increments the value after (post) the operand value is used. i.e., operand value is used
first and then the operand value is incremented by 1.
Ex: void main()
{
int a=20,b; // a=20, b?
b=a++; // b=a=20, a=a+1= 20+1
printf(“%d”,a); // a=21
printf(“%d”,b); // b=20
}
2. Pre Increment
It increments before (pre) the operand value is used. The operand value is incremented
by 1 and this incremented value is used.
Ex: void main()
{
int a=20,b; // a=20, b?
b=++a; // a=++a=a+1= 20+1, b=a=21
printf(“%d”,a); // a=21
printf(“%d”,b); // b=21
}
Decrement Operator
'--' is a decrement operator. This is a unary operator. It decrements the value of a
variable by one.
Post Decrement Ex: a--
Decrement
Operator Pre Decrement Ex: --a
1. Post Decrement
It decrements the value after (post) the operand value is used. i.e., operand value is used
first and then the operand value is decremented by 1.
Ex: void main()
{
int a=20,b; // a=20, b?
b=a--; // b=a=20, a=a-1= 20-1
printf(“%d”,a); // a=19
printf(“%d”,b); // b=20
}
2. Pre Decrement
It decrements before (pre) the operand value is used. The operand value is decremented
by 1 and this decremented value is used.
Ex: void main()
{
int a=20,b; // a=20, b?
b=--a; // a=--a=a-1= 20-1, b=a=19
printf(“%d”,a); // a=19
printf(“%d”,b); // b=19
}
Eg:
Initially a=10, b=5, c=20 simplify the following expression
i. W = a++ + ++b + c++ - ++a
= 10 + 6 + 20 - 12
a=11 b=6 c=21 a=12
w = 24
a=12 , b=6, c=21, w=24
All the relational operators are having a same priority and left to right associativity.
The relational operators have lower precedence than arithmetic operators.
Equality operators
C supports two kinds of equality operators to compare their operands for strict equality or
inequality.
equal ==
not equal ! =
Equality operators have lower precedence than the relational operators.
Eg: a=10, b=6
i. C=a>b
C=10>6 10 is greater than 6 so the operator returns value 1, indicating true
C=1
ii. C=a<b
C=10<6 10 is not less than 6, so the operator returns value 0, indicating false
C=0
iii. C=a>=b
C=10>=6
C=1
iv. C=a<=b
C=10<=6
C=0
v. C=a==b
C=10==6 ==checks whether 10 is equal to 6, if equal return 1, else returns 0
C=0
vi. C=a!=b
C=10!=6 != checks whether 10 is not equal to 6, if not equal returns 1, else returns
0
C=1
v) Logical operators
The operators that are used to combine two or more relational expressions are called
logical operators.
The output of relational expression is true or false, the output of logical expression is also
true or false.
Operators
not !
and &&
or ||
Logical NOT: The logical NOT operator is denoted by ‘!’. The output of not operator can be true
or false. The result is true if the operand value is false and the result is false if the operand is true.
Logical AND: The logical AND operator is denoted by ‘&&’. The output of and operator is true if
both the operands are evaluated to true. If one of the operand is evaluated false, the result is false.
Logical OR: The logical OR operator is denoted by ‘||’. The output of or operator is true if and
only if at least one of the operands is evaluated to true. If both the operands are evaluated to false,
the result is false.
Ex:
i. Given A=10, Evaluate C=!A
A=10 other than value 0, any value is treated as 1. So, A=1
C = !A
C = !1
C=0
ii. Given A=10, B=15, Evaluate C = A&&B
A=10 treated as A=1 (Because other than value 0, any value is treated as 1)
B=15 treated as B=1 (Because other than value 0, any value is treated as 1)
C = A&&B
= 1 && 1
C= 1
iii. Given A=20, B=25, Evaluate C = A || B
A = 20 treated as A=1 (Because other than value 0, any value is treated as 1)
B = 25 treated as B=1 (Because other than value 0, any value is treated as 1)
C = A || B
= 1 || 1
C=1
Where,
- expr1 is evaluated first.
- If expr1 is evaluated to true, then expr2 is evaluated.
- If expr1 is evaluated to false, then expr3 is evaluated.
Example:
1. Write a C program to find the biggest of two numbers using conditional operator.
#include<stdio.h>
void main()
{
int a,b,big;
printf(“Enter the values of a and b:”);
scanf(“%d%d”,&a,&b);
big=(a>b)?a:b;
printf(“Big=%d”,big);
getch();
}
2. Write a C program to find the smallest of two numbers using conditional operator.
#include<stdio.h>
void main()
{
int a,b,small;
printf(“Enter the values of a and b:”);
scanf(“%d%d”,&a,&b);
small=(a<b)?a:b;
printf(“Small=%d”,big);
getch();
}
Bit-wise XOR ^
Bit-wise OR |
These may only be applied to integral operand. i.e., char, short, int and long whether signed or
unsigned.
Op1 ~Op1
0 1
1 0
Ex:
i. Let A= 15, Evaluate B = ~A
Convert A = 15 to binary representation and apply Bitwise Negate to binary values
240 = 1 1 1 1 0 0 0 0
Output: ~10=245
Binary Representation(8 bit representation):
10 = 0 0 0 0 1 0 1 0
245= 1 1 1 1 0 1 0 1
Ex:
i. Let a=5, Evaluate b = a<<1
MSB is discarded
0 0 0 0 0 1 0 1 a=5
b=10
0 0 0 0 1 0 1 0
0 is appended at LSB
b = 10
Ex:
i. Let a=10, Evaluate b = a>>1
LSB is discarded
0 0 0 0 1 0 1 0 a=10
0 0 0 0 0 1 0 1 b=5
0 is appended at MSB
b=5
Write a C program to show the usage of Right Shift operator.
#include<stdio.h>
void main()
{
int a=10,b;
b=a>>1;
printf(“%d>>1=%d”,a,b);
}
Output: 10>>1=5
Ex:
C =2
c=a&b;
printf(“%d&%d=%d”,a,b,c);
}
Output: 10&6=2
e. Bit-wise OR (|)
If the corresponding bit positions in both the operands are 0, then OR operation results
in 0, otherwise OR operation results in 1.
Ex:
Let a=10, b=6. Evaluate c = a | b
Binary Representation: 10 = 0 0 0 0 1 0 1 0
06 = 0 0 0 0 0 1 1 0
14 = 0 0 0 0 1 1 1 0
C=14
Output: 10|6=14
12 = 0 0 0 0 1 1 0 0
C= 12
Output: 10^6=12
i) Comma operator
Comma Operator has the least precedence among all the operators and it is left associative
operator.
Comma Operator is used in the declaration to separate the variables.
Ex: int a,b,c;
It can be used to separate the items in the list.
Ex: a=12,345,678;
It can be used to combine two or more statements into a single statement.
Ex: sum=a+b,sub=a-b,mul=a*b,div=a/b,mod=a%b;
ii) sizeof()
‘sizeof()’ operator is used to determine the number of bytes occupied by a variable or a
constant in the memory.
Ex: sizeof(char) 1 byte
sizeof(int) 2 bytes
sizeof(float) 4 bytes
Arithmetic Expressions
The expression consisting of only arithmetic operators such as +, -, *, / and % are called
arithmetic expressions.
𝑥 = √2𝜋𝑛 x=sqrt(2*3.142*n)
a a/b
b
𝑏 x=-b/(2*a)
𝑥=−
2𝑎
𝑎𝑥 2 + bx + c a*x*x+b*x+c
Type Conversion
The process of converting the data from one data type to another data type is called Type
Conversion.
Implicit Conversion 1/2.0 1.0/2.0=0.5000
Type
Conversion Explicit Conversion 1/(float)2 1.0/2.0=0.5000
C compiler converts the data type with lower rank to the data type with higher rank.
This process of conversion of data from lower rank to higher rank automatically by the
C compiler is called “Implicit type Conversion”.
char short int int unsigned int long int float double Lower
Rank Higher Rank
Data types Data types
If one operand type is same as that of other operand type, no conversion takes place.
Ex: int + int = int, float + float = float
If one operand type is ‘int’ and other operand type is ‘float’, then the operand with type int is
promoted to ‘float’ (because float is up in ladder compared with int).
The programmer can instruct the compiler to change the type of the operand from one
data type to another data type. This forcible conversion from one data type to another
data type is called “Explicit type Conversion” (Type Casting).
Syntax:
(type) Expression
goto
break
Unconditional
Branch statements continue
return
Conditional branching
if Statement
The ‘if’ statement is the simplest form of decision control statement.
When a set of statements have to be executed when an expression (condition) is evaluated
to true or skipped when an expression (condition) is evaluated to false, then if statement is
used.
It is used whenever there is only one choice (alternative). Hence it is also called as “One-
way decision or selection statement”.
Syntax of if statement
if(test Expression)
{ test
Expression False
Statement 1;
Statement 2; True
... Statement 1
Statement 2
…
…
… …
…
Statement n; Statement n
}
Statement x; Statement x
The ‘if’ structure may include one statement or ‘n’ statements enclosed within curly
brackets.
Working Principle
First the test expression is evaluated.
If the test expression is true, then the statements of ‘if’ block (statement 1 to n) are
executed.
Otherwise these statements will be skipped and the execution will jump to statement x.
2 if-else statement
If one set of activities have to be performed when an expression is evaluated to true and
another set of activities have to be performed when an expression is evaluated to false,
then if-else statement is used.
else
Statement block 1 Statement block 2
{
Statement block 2;
}
Statement x
Statement x;
The is-else statement is used when we must choose between two choices (alternatives).
Hence is also called as “Two-way Decision or Selection Statement”.
Working Principle
According to the if-else construct, first the ‘test expression’ is evaluated.
If the expression is true then Statement block 1 is executed and Statement block 2 is
skipped.
If the expression is false the Statement block 2 is executed and Statement block 1 is
ignored.
Now in any case after the Statement block 1 or 2 gets executed the control will pass to
Statement x. It is executed in every case.
2. Write a C program to check whether the number is even or odd and print the
appropriate message.
#include<stdio.h>
void main()
{
int n,rem;
printf(“Enter a number:”);
scanf(“%d”,&n);
if(n%2==0)
printf(“Number is even”);
else
printf(“Number is odd”);
}
When an action has to be performed based on many decisions involving various types of
expressions and variables, then this statement is used. So it is called as “Multi-way
decision statement”.
Syntax for nested if-else statement:
if(test Expression1)
{
if(test Expression2) True Test False
{ Expression1
Statement block 1;
}
else Statement
Test
{ True Expression2 False block 3
Statement block 2;
} Statement Statement
} block 1 block 2
else
{
Statement block 3;
Statement x
}
Statement x;
Working Principle
If the test expression 1 is evaluated to true, then the test expression 2 is checked for true
or false. If the test Expression2 is evaluated to true, then the statements in block 1 are
executed, otherwise the statements in block 2 are executed. After executing the inner if-
else the control comes out and the statement x is executed.
If the test expression1 itself is false, then the statements in block 3 are executed. After
executing these statements, the statement x is executed.
Example Programs for ‘nested if-else’ statement
1. Write a C program to find the largest of three numbers.
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter the three numbers:”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
printf(“Max=%d”,a);
else
printf(“Max=%d”,c);
}
else
{
if(b>c)
printf(“Max=%d”,b);
else
printf(“Max=%d”,c);
}
}
} block 1
…. Statement
block 2
else
Statement
{ block n
Statement block n;
}
Statement x
Statement x;
Working Principle
The ‘Expressions’ are evaluated in order. If any expression is true then the statement
associated with it is executed and this terminates the whole chain and statement x is
executed.
The last ‘else’ part is executed when all the test expression are false.
scanf(“%d%d”,&x,&y);
if(x==y)
printf(“\n The two numbers are equal”);
else if(x>y)
printf(“\n %d is greater than %d”,x,y);
else
printf(“\n %d is less than %d”,x,y);
}
3. Write a C program to input 3 numbers and then find the largest of them using ‘&&’
operator.
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter the three numbers:”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b && a>c)
printf(“\n %d is the largest number”,a);
else if(b>a && b>c)
printf(“\n %d is the largest number”,b);
else
printf(“\n %d is the largest number”,c);
}
4 switch statement
The ‘switch’ statement is a control statement used to make a select one alternative among
several alternatives.
It is a “multi-way decision statement” that tests whether an expression matches one of
the case values and branches accordingly.
Working Principle
First the expression within switch is evaluated.
The value of an expression is compared with all the case values.
The value of an expression within switch is compared with the case value-1. If it matches
then the statements associated with that case are executed. If not then the case value-2 is
compared, if it matches then the associated statements are executed and so on.
The “default” statements are executed when no match is found.
A default is optional.
The ‘break’ statement causes an exit from the switch. ‘break’ indicates end of a particular
case and causes the control to come out of the switch. [Significance of break within
switch statement]
break;
case ‘C’: printf(“Fair”);
break;
case ‘F’: printf(“Fail”);
break;
default : printf(“Invalid grade”);
}
}
2. Write a C program to enter a number from 1 to 7 and display the corresponding day
of the week using switch.
#include<stdio.h>
void main()
{ int day;
printf(“Enter any number:”);
scanf(“%d”,&day);
switch(day)
{
case 1: printf(“Sunday”);
break;
case 2: printf(“Monday”);
break;
case 3: printf(“Tuesday”);
break;
case 4: printf(“Wednseday”);
break;
case 5: printf(“Thursday”);
break;
case 6: printf(“Friday”);
break;
case 7: printf(“Saturday”);
break;
default : printf(“invalid input”);
}
}
LOOPS
A set of statements may have to be repeatedly executed for a specified number of times or till a
condition is satisfied.
The statements that help us to execute a set of statements repeatedly for a specified number
of times or till a condition is satisfied are called as looping constructs or loop control
statements.
while loop
do-while loop
1 while Loop
It is a control statement using which the programmer can give instructions to the computer
to execute a set of statements repeatedly as long as specified condition is satisfied.
The expression is evaluated to TRUE or FALSE in the beginning of the while loop. Hence it is
called as “Entry-Controlled Loop”.
Statement x
Working principle
The test expression is evaluated first, if it is TRUE then the set of statements within the body of
the loop are executed repeatedly as long as specified test expression is TRUE.
If the test expression is false, then the control comes out of the loop by skipping the execution of
the statements within the body of the loop, by transferring the control to the Statement x.
m=a;
n=b;
while(n!=0) OUT PUT:
{ Enter the value for m and n:
rem=m%n; 6
m = n; 12
n = rem; The GCD of 6 12 numbers is= 6
} The LCM of 6 12 numbers is= 12
gcd = m;
lcm = ( a * b ) / gcd;
printf("The GCD of %d %d numbers is=%d\n", a, b, gcd );
printf("The LCM of %d %d numbers is=%d\n", a, b, lcm );
}
FALSE
for(initialization ;test expression; increment/decrement)
L
O True
O
P
Statements
Statement x
Working Principle
initialization: In this section loop variable is initialized, like i=0, n=0, i=1,n=1. (i and n are
loop variables).
test expression: The test expression may be a relational expression or logical expression or
both, which is evaluated to TRUE or FALSE. Depending on the value of the test expression,
the body of the loop is executed. If the test expression is TRUE, then the body of the loop is
executed. This process of execution of body of the loop is continued as long as the expression
is TRUE. When the test expression becomes FALSE, execution of the statements contained
in the body of the loop are skipped, thereby transferring the control to the Statement x, which
immediately follows the for loop.
increment/decrement: This section increments or decrements the loop variables after
executing the body of the loop.
Infinite Loop
A for loop without a test expression is an Infinite loop.
Ex: for(i=0; ;i++)
{
………
} is an “infinite” loop.
}
4. Write a C program to generate ‘n’ Fibonacci numbers.
#include<stdio.h>
void main()
{
int n,f1,f2,f3,i;
for(ch=’A’;ch<=’Z’;ch++)
{
printf(“%c\t”,ch);
}
}
Output: A B C D E F ----------------------------------------Z
6. Write a C program to find the factorial of a given number using for statement.
#include<stdio.h>
void main()
{
int fact=1,i,n;
printf(“Enter an integer:\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“Factorial of a given number is=%d”,n,fact);
}
Nested Loops
The loops that can be placed inside other loops.
3 do-while Loop
do- while loop is used when a set of statements have to be repeatedly executed at
least once.
Since the test expression is evaluated to TRUE or FALSE at the end of do-while loop, the
do-while loop is called as exit-controlled loop.
The while and for loop test the expression at the top.
The do-while tests the bottom after making each passes through the loop body.
Statement x
Working Principle
Statements
False
while(test expression)
True True
while(test expression);
Statements
False
Statement x
Statement x
2. Write a C program to calculate the sum and average of first ‘n’ numbers.
#include<stdio.h>
void main()
{
int n,i=0,sum=0;
float avg=0.0;
printf(“Enter the value of n:”);
scanf(“%d”,&n);
do
{
sum=sum+i;
i++;
}while(i<=n);
avg=sum/n;
printf(“The sum of numbers=%d”,sum);
printf(“The average of numbers=%f”,avg);
}
If break is executed in a loop (for/while/do-while) then the control comes out of the
loop and the statement following the loop will be executed.
Syntax
1. while(…)
{
……
if(condition)
break;
……
} Transfers the control out of the while loop
……
2. do
{
……
if(condition)
break;
…… Transfers the control out of the do-while loop
}while(condition);
…
3. for(…)
{
……
if(condition)
break;
……
} Transfers the control out of the for loop
……
Syntax:
1. while(… )
{
if(condition) Transfers the control to the expression of the while loop
continue;
……….
}
……….
2. do
{
……
if(condition)
continue;
…… Transfers the control to the expression of the do-while
loop
}while(condition);
……
3. for(…)
{
……
if(condition) Transfers the control to the expression of the for loop
continue;
……
}
……
}
Output: 1 3 4 5 [if i==2 then the continue statement is executed and the statements
following continue are skipped]
3 goto statement
The goto statement is a ‘jump’ statement that transfers the control to the specified
statement (Label) in a program unconditionally.
The specified statement is identified by ‘label’( symbolic name ). Label can be any valid
variable name that is followed by a colon (:).
Syntax
The sum of all the elements of a row is twice the sum of all the elements of its preceding row.
For example, sum of second row is 1+1= 2, and that of first is 1. Again, the sum of third
row is 1+2+1 =4, and that of second row is 1+1 =2, and so on. This major property is utilized
to write the code in C program for Pascal’s triangle.
The sequence of the product of each element is related to the base of the natural logarithm, e.
The left and the right edges of Pascal’s triangle are filled with “1”s only.
All the numbers outside the triangle are “0”s.
The diagonals next to the edge diagonals contain natural numbers (1, 2, 3, 4, ….) in order.
The sum of the squares of the numbers of row “n” equals the middle number of row “2n”.
(ax2+bx+c=0) as input and compute all possible roots for a given set of coefficients with
appropriate messages.
11. List the Difference between while loop and do-while loop.
12. Write a c program to find GCD and LCM of two numbers.
13. Write a C Program to compute Sin(x) using Taylor series approximation given by Sin(x)
= x - (x3/3!) + (x5/5!) - (x7/7!) + …….Compare your result with the built- in Library
function. Print both the results with appropriate messages.
14. Write a C Program to print Pascal Triangle.
15. Write a C program to find binomial coefficient.