T7 Jump Nested Loops
T7 Jump Nested Loops
C Programming
22
Nested Loops
How many times will the string "Here" be printed?
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 <= 20)
{
printf ("Here");
count2++;
}
count1++;
}
10 * 20 = 200 23
Looping- nested for
statements
What are nested for loops?
For example:
25
Looping- nested for statement
working
for(int i=1; i<=2;i++) OUTER FOR
{
int j;
for( j=1 ;i<=3 ; j++) INNER FOR
{
printf(“%d %d”,i,j);
} How many
statements
printf(“\n”); inside inner
How many
} for?
statements
inside outer
for?
Looping- nested for statement
working
i=1 2( for true)
for(int i=1; i<=2;i++)
{
int j;
j= 1 2 3 (for true)
for( j=1 ;i<=3 ; j++)
{ i j printf
printf(“%d %d”,i,j); 1 1 11
} 1 2 12
printf(“\n”); 1 3 13
} How many 2 1 21
times i and j 2 2 22
are printed? 2 3 23
Looping- nested for statement
working
How many times outer for
loop got executed ?
for(int i=1; i<=2;i++)
{ 2
int j;
for( j=1 ;i<=3 ; j++)
{ How many times inner for
loop got executed ?
printf(“%d %d”,i,j);
} 6
printf(“\n”); For each execution of the
} outer loop the inner loop
is executed 3 times, hence
2*3=6
MCQS
Answer : C
Answer : B
Programs
33
Programs
1 2 1
2 1 2
1 2 1
56
Programs