Lab-05 29 March2017
Lab-05 29 March2017
CIS-104 BSME
What is Nested Loop?
A nested loop is just a normal loop except that it is inside the body of another loop. A nested loop can be
formed by placing a for, while or do‐while loop inside the body of another for, while or do‐while loop.
There is no limit on the combination of nesting i.e. any loop can be used inside any other loop.
Following are some combination of nested loops. Usually the loop inside the body of another loop is
called inner loop while the other loop is termed as outer loop.
Nested Loops (Syntax)
The syntax for a nested for loop statement in C is as follows:
for( init; condition; increment )
{
for( init; condition; increment )
{
statement(s);
}
statement(s);
}
The syntax for a nested while loop statement in C programming language is as follows:
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
The syntax for a nested do...while loop statement in C programming language is as follows:
do
{
statement(s);
do
{
statement(s);
}while( condition );
}while( condition );
The syntax of mixed types of loop as nested loops
Task‐05
Try the above patterns using number series as discussed in the class
#include <stdio.h>
#include <conio.h>
void main()
{
int a=1;
while(a%2!=0){
printf(”\nEnter Value of a = “);
scanf(“%d”,&a);
}
}
Program‐02
#include <stdio.h>
#include <conio.h>
void main()
{
int a=205,b=450;
int diff=500;
while(diff>10){
b=b-25;
diff=b;
printf(“ a = %d , b=%d”,a,b);
printf(”\nDifference = %d“,diff);
}
}
Program‐07
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j;
int limit;
for(j=2;j<5;j++){
limit=j*j;
printf(“\nNumbers between 0 and %d are “,limit);
for(i=1;i<j*j;i++){
printf(“\n %d ”,i);
}
printf(“\n************************************************”);
}
printf(“End of Program”);
}
Task-03
#include<conio.h>
#include<stdio.h>
int main ()
{
int i, j;
for(i=0;i<5;i++){
for(j=0;j<=i;j++)
printf(" %d ",j);
printf("\n");
}
getch();
return 0;
}