POP Module-2 (Part-B) Notes
POP Module-2 (Part-B) Notes
POP Module-2 (Part-B) Notes
if-Statement
• First the test expression is evaluated. If the test expression is true, the statement of
if block (statement 1 to n) are executed otherwise these statements will be skipped and
the execution will jump to statement x.
#include<stdio.h>
void main()
{
int age;
scanf("%d",&age);
if(age>=18)
}
}
#include<stdio.h>
void main()
char ch;
scanf("%c",&ch);
if(ch>0)
}}
if-else Statement
In the if-else construct, first the test expression is evaluated. If the expression is true,
statement block 1 is executed and statement block 2 is skipped. Otherwise, if the
expression is false, statement block 2 is executed and statement block 1 is ignored. In
any case after the statement block 1 or 2 gets executed the control will pass to
statement x. Therefore, statement x is executed in every case.
#include <stdio.h>
void main()
{
int a;
scanf("%d",&a);
if(a%2==0)
else
#include <stdio.h>
void main()
char ch = 'C';
else
#include <stdio.h>
void main()
int a=100,b=20;
if(a<b)
else
printf("a is largest of 2 numbers");
WAP to enter any character. If entered in upper case then display in lower case
and vice versa.
#include<stdio.h>
void main()
char ch;
scanf("%c",&ch);
if(ch>='A'&&ch<='Z')
else
The year number must be divisible by four – except for end-of-century years, which
must be divisible by 400.
Example: This means that the year 2000 was a leap year, although 1900 was not
#include<stdio.h>
void main()
int year;
scanf("%d",&year);
if((year%4==0)&&(year%100!=0)||(year%400==0))
printf("Leap year");
else
}
}
if-else if Statement
void main()
int a,b;
scanf("%d,%d",&a,&b);
if(a==b)
else if(a>b)
else
Example:
void main()
{
int a;
if(a=10)//The condition should e a==10. Here instead of checking the condition ,the
value is being assigned to a.
printf("Equal");
else
printf("not equal");
void main()
int a;
scanf("%d",&a);
if(a>0)
else if(a<0)
else
printf("The numer is eqaul to zero"); }
A company decides to give bonus to its employee. If the employee is male he gets
5% bonus, if female she gets 10% bonus. If salary is less than 10,000, he/she
gets extra 2% bonus on salary. WAP to calculate the bonus and display the salary
which has to be given to the employee
#include<stdio.h>
void main()
{
char ch;
float sal,bonus,amt;
scanf("%c",&ch);
scanf("%f",&sal);
if(ch=='m')
bonus=0.05*sal;
else
bonus=0.10*sal;
if(sal<10000)
bonus=bonus+0.20*sal;
amt=sal+bonus;
printf("The salary which employee will get is %f",amt);
Marks>=75=Distinction
void main()
{ int marks;
scanf("%d",&marks);
if(marks>=75)
printf("Distinction");
else if(marks>=60&&marks<75)
printf("First class");
else if(marks>=50&&marks<60)
printf("Second class");
else if(marks>=50&&marks<40)
printf("Third class");
else
printf("Fail"); }
WAP to find largest of 3 numbers.
void main(){
int a,b,c;
printf("Enter a,b,c\n");
scanf("%d,%d,%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("a is greater");
else
printf("c is greater");
else if(b>c)
printf("b is greater");
else
printf("c is greater");
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter a,b,c\n");
scanf("%d,%d,%d",&a,&b,&c);
if(a>b&&a>c)
printf("a is greater");
if(b>a&&b>c)
printf("b is greater");
if(c>a&&c>b)
printf("c is greater");
}
WAP to enter the marks of 4 subject out of 100 and then calculate total,average
and display the grade obtained.
void main() {
int m1,m2,m3,m4,total;
float avg;
scanf("%d,%d,%d,%d",&m1,&m2,&m3,&m4);
total=m1+m2+m3+m4;
avg=total/4;
if(avg>=75)
printf("Distinction");
else if(avg>=60&&avg<75)
printf("First class");
else if(avg>=50&&avg<60)
printf("Second class");
else if(avg>=50&&avg<40)
printf("Third class");
else
printf("Fail");
}
Switch case
Step 2: The evaluated value is matched against all the present cases.
Step 3A: If the matching case value is found, the associated code is executed.
Step 3B: If the matching code is not found, then the default case is executed if
present.
Step 4A: If the break keyword is present in the case, then program control breaks
out of the switch statement.
Step 4B: If the break keyword is not present, then all the cases after the
matching case are executed.
Example:
char grade=‘C’;
switch(grade)
{ case 'A':
printf("\n Excellent");
break;
case 'B':
printf("\n Good");
break;
case 'C':
printf("\n Fair");
break;
default:
break;
char ch;
scanf("%c",&ch);
switch(ch)
case 'A':
break;
case 'E':
break;
case 'I':
break;
case 'O':
break;
case 'U':
break;
default:printf("%c is not a vowel",ch);
WAP to enter a number from 1-7 and display the corresponding day of the week
using switch case.
void main() {
int day;
scanf("%d",&day);
switch(day) {
case 1:printf("\nSunday");
break;
case 2:printf("\nMonday");
break;
case 3:printf("\nTuesday");
break;
case 4:printf("\nWednesday");
break;
case 5:printf("\nThursday");
break;
case 6:printf("\nFriday");
break;
case 7:printf("\nSaturday");
break;
#include<stdio.h>
void main()
int num,rem;
scanf("%d",&num);
rem=num%2;
switch(rem)
case 0:printf("\nEven");
break;
case 1:printf("\nOdd");
break;
}}
Iterative statements
Iterative statements are used to repeat the execution of a list of statements, depending
on the value of an integer expression. In this section, we will discuss all these
statements.
• While loop
• Do-while loop
• For loop
while loop
The while loop is used to repeat one or more statements while a particular condition is
true.
In the while loop, the condition is tested before any of the statements in the statement
block is executed.
If the condition is true, only then the statements will be executed otherwise the control
will jump to the immediate statement outside the while loop block.
#include<stdio.h>
void main()
{
int i=1,sum=0;
while(i<=10)
{
sum=sum+i;
i++;
printf("\nsum=%d",sum);
#include<stdio.h>
void main()
int i=1;
while(i<=20)
printf("*");
i++;
{
int i,j,sum=0;
scanf("%d",&i);
scanf("%d",&j);
while(i<=j)
{
sum=sum+i;
i++;
}
printf("\nsum=%d",sum);
int i=1,large,num;
while(i<=5)
scanf("%d",&num);
large=num>large?num:large;
i++;
void main()
{ int num,sum,count;
float avg;
scanf("%d",&num);
while(num!=-1)
count++;
sum+=num;
scanf("%d",&num);
avg=(float)sum/count;
printf("sum=%d\n",sum);
printf("Avg=%f",avg); }
do-while loop
The do-while loop is similar to the while loop. The only difference is that in a do-while
loop, the test condition is tested at the end of the loop.
The body of the loop gets executed at least one time (even if the condition is false).
The do while loop continues to execute whilst a condition is true. There is no choice
whether to execute the loop or not. Hence, entry in the loop is automatic there is only a
choice to continue it further or not.
The major disadvantage of using a do while loop is that it always executes at least
once, so even if the user enters some invalid data, the loop will execute.
do-while loops are widely used to print a list of options for a menu driven program.
Void main()
{ int n,i=1,sum;
float avg;
scanf("%d",&n);
do
sum+=i;
i++;
}while(i<=n);
avg=(float)sum/n;
printf("Sum=%d\n",sum);
printf("Avg=%f",avg); }
scanf("%d",&n);
i=1;
do
printf("\n|\t%d\t|\t%d\t|\t%d\t|",i,i*i,i*i*i);
i++;
}while(i<=n); }
void main()
{ int m=1900,n=1920;
do
if(((m%4==0)&&(m%100!=0))||(m%400==0))
m++;
}while(m<=n); }
WAP to read a character until * is encountered. Also count the number of upper
case,lower case and numbers entered.
void main()
{ char ch;
int lowers=0,uppers=0,numbers=0;
scanf("%c",&ch);
do
{
if(ch>='A'&&ch<='Z')
uppers++;
if(ch>='a'&&ch<='z')
lowers++;
if(ch>='0'&&ch<='9')
numbers++;
scanf("%c",&ch);
}while(ch!='*');
printf("\nCount of upper case entered is %d",uppers);
for loop
statement block;
Statement Y;
• When a for loop is used, the loop variable is initialized only once. With every
iteration of the loop, the value of the loop variable is updated and the condition is
checked. If the condition is true, the statement block of the loop is executed else, the
statements comprising the statement block of the for loop are skipped and the control
jumps to the immediate statement following the for loop body. Updating the loop
variable may include incrementing the loop variable, decrementing the loop variable or
setting it to some other value like, i +=2, where i is the loop variable.
void main()
{ int i,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d\t",i);
void main()
{ int i,n;
scanf("%d",&n);
for(i=0;i<=n;i++)
printf("%d\t",i);
void main()
{ int i,n;
scanf("%d",&n);
for(i=4;i<=n;i+=4)
printf("%d\t",i);
}}
Pass 2: 1 2 3 4 5
Pass 3: 1 2 3 4 5
Pass 4: 1 2 3 4 5
Pass 5: 1 2 3 4 5
void main()
int i,j;
for(i=1;i<=5;i++)
{
printf("\nPass %d",i);
for(j=1;j<=5;j++)
{ printf("\t%d",j); }
}}
**********
**********
**********
**********
**********
void main()
int i,j;
for(i=1;i<=5;i++)
printf("\n");
for(j=1;j<=10;j++)
{ printf("* "); }
}}
**
***
****
*****
void main()
int i,j;
for(i=1;i<=5;i++)
printf("\n");
for(j=1;j<=i;j++)
printf("*");
}}}
1
12
123
1234
12345
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{ printf("%d",j); }}}
1
22
333
4444
55555
void main()
int i,j;
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{ printf("%d",i); }}}
0
12
345
6789
void main()
int i,j,count=0;
for(i=1;i<=4;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{ printf("%d",count++); }}}
AB
ABC
ABCD
ABCDE
ABCDEF
void main()
char i,j;
for(i=65;i<=70;i++)
printf("\n");
for(j=65;j<=i;j++)
{ printf("%c",j); }}}
12
123
1234
12345
void main()
int N = 5,i,j,k;
printf(" ");
printf("%d ",k+1);
printf("\n");
void main()
{ int n,i;
printf("Enter any number");
scanf("%d",&n);
for(i=0;i<=10;i++)
printf("\n%d X %d = %d",n,i,i*n); }
WAP to print all numbers from m to n, thereby classifying them as even or odd.
void main()
{ int m,n,i;
scanf("%d",&m);
scanf("%d",&n);
for(i=m;i<=n;i++)
if(i%2==0)
printf("\n%d is even",i);
else
printf("\n%d is odd",i);
}
break statement
• The break statement is used to terminate the execution of the nearest enclosing
loop in which it appears.
• When compiler encounters a break statement, the control passes to the statement
that follows the loop in which the break statement appears. Its syntax is quite simple,
just type keyword break followed with a semi-colon.
break;
• In switch statement if the break statement is missing then every case from the
matched case label to the end of the switch, including the default, is executed.
while(…) do
{ {
….. …..
if(condition) if(condition)
break; break;
….. …..
} }while(…);
for(…) for(…)
{ {
….. …..
if(condition) for(…)
break; {
….. if(condition)
} break;
Stmnt y
continue statement
• When the compiler encounters a continue statement then the rest of the
statements in the loop are skipped and the control is unconditionally transferred
to the loop-continuation portion of the nearest enclosing loop. Its syntax is quite
simple, just type keyword continue followed with a semi-colon.
continue;
• If placed within a for loop, the continue statement causes a branch to the code
that updates the loop variable. For example,
int i;
for(i=1;i<=10;i++)
{ if(i==5)
continue;
printf("\t %d",i); }
while(…) do
{ {
….. …..
if(condition) if(condition)
continue; continue;
….. …..
} }while(…);
for(…) for(…)
{ {
….. …..
if(condition) for(…)
continue; {
….. if(condition)
} continue;
goto statement
• Here label is an identifier that specifies the place where the branch is to be
made. Label can be any valid variable name that is followed by a colon (:).
• Note that label can be placed anywhere in the program either before or after the
goto statement. Whenever the goto statement is encountered the control is
immediately transferred to the statements following the label.
• If the label is placed after the goto statement then it is called a forward jump
and in case it is located before the goto statement, it is said to be a backward
jump.
• Goto statements make the program code complicated and render the program
unreadable.
Example:
scanf("%d", &num);
if (num != 999)
if(num < 0)
sum += num;
*****End*****