Você está na página 1de 14

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name of the faculty : G.Padmanabhaiah
Designation : HCME
Branch : Computer Engineering
Institute : V.K.R & V.N.B Polytechnic, Gudivada
Year/Semester : III Semester
Subject : UNIX & C.
Subject Code : CM-304
Topic : Understand repetitive structures.
Duration : 50 Min
Sub Topic : Nested loops.
Teaching Aids : Diagram and animations, PPTs.
CM304.51 1
Objective

On completion of this period, the student


would be able to know.

 Nested loops.

CM304.51 2
Nested loops

 It is possible in C to have a loop within


another loop. It is called a nested loop.

 Nested loop construct has an outer loop and a


subsequent inner loop(s).

CM304.51 3
General form nested loop

for( ) Outer loop


{
statements;
for( ) Inner loop
{
statements; Inner loop Outer loop
} statements statements

CM304.51 4
Nested Loop

 The complexity of the program will increase .

 Very difficult to understand the program if the


nesting levels are more.

CM304.51 5
Simple program using nested loop

#include<stdio.h>
Outer
main() loop
{
int i, j;
for (i=1;i<=5;i++) Inner
{ loop
printf (“\n ”);
for (j=1;j<=i; j++)
{ Output :
1
printf (“%d”, j); 12
} 123
} 1234
12345

CM304.51 6
Summary

In this class, you have learnt about..

 Using and working of nested loops, with


general form and with examples.

CM304.51 7
Quiz

1. What is nested loop?


b) loop with in condition.
c) only one loop.
d) loop with in a another loop it is called nested
loop.

CM304.51 8
Quiz

1. What is nested loop?


• loop with in condition.
• only one loop.
• loop with in a another loop it is called nested
loop.

CM304.51 9
Quiz
2. In the following which one is nested loop?

(a) for (i=1;i<=5;i++) (b) for


{ (i=0;i>5;i++)
printf (“\n ”); for (j=1;j<=i; j++) {
{ printf (“%d \n”, i);
printf (“%d”, j); }
}
}
c) None

CM304.51 10
Quiz
2. In the following which one is nested loop?

(a) for (i=1;i<=5;i++) (b) for


{ (i=0;i>5;i++)
printf (“\n ”); for (j=1;j<=i; j++) {
{ printf (“%d \n”, i);
printf (“%d”, j); }
}
}
c) None

CM304.51 11
3. What is the output of the following input?
main()
{
int i, j, k;
for (i=1;i<=4;i++)
{
printf (“\n ”);
for (j=1;j<=i; j++)
{
printf (“%d”, j);
} a)1 b) * a) 1
} ** 12
12
123 *** 123

1234 **** 1234

CM304.51 12
3. What is the output of the following input?
main()
{
int i, j, k;
for (i=1;i<=4;i++)
{
printf (“\n ”);
for (j=1;j<=i; j++)
{
printf (“%d”, j);
} a)1 b) * a) 1
} ** 12
12
123 *** 123

1234 **** 1234

CM304.51 13
Frequently Asked Questions

1. Write a program to generate multiplication


tables from 1 to 10 using nested for loops
2. Explain briefly about nested loops with
syntax, flowchart and suitable example.

CM304.51 14

Você também pode gostar