NESTING OF LOOPS
When a loop inside another loop is used, it is called
nesting of loops. The nesting can be for any number of levels. For certain
problems, nesting of loops are needed. When nesting is done, some care is
required. The outer loop should not end between the starting of inner loop and
ending of inner loop.
·
Nesting
For Loop
For ( initialization-1; condition-1;
increment/decrement )
{
.
.
.
For( initialization-2;
condition-2; increment/decrement )
{
.
.
.
Statement(s);
}
}
Example :-
for(i=0;i<=4;i++)
{
for(j=0;j<=4:j++);
{
printf("* ");
}
printf("\n);
}
{
for(j=0;j<=4:j++);
{
printf("* ");
}
printf("\n);
}
In above example, First for loop is called as outer
for loop, we set the control statement as ‘I’. and second for loop is called as
inner for loop. And we set control statement for inner loop is ‘j’. nested loop
is used in patterns that are shown in examples.
·
Nesting
while loop
Nested while loop is proceed as for loop.
While ( condition-1 )
{
.
.
.
While (
condition-2 )
{
.
.
.
Statement
(s);
}
}
Example :-
while(i<=4)
{
while(j<=4)
{
printf("* ");
j++;
}
printf("\n);
i++;
}
{
while(j<=4)
{
printf("* ");
j++;
}
printf("\n);
i++;
}
·
Nested
do…while Loop
do
{
do
{
Statement(s);
} while( condition );
} (condition );
0 Comments
If You have any query, Please let me know..