Nested Loops
By:
Adnan Ferdous Ahmed
•What is a loop?
•What is a nested loop?
•Loops used in nested loops
•Nested while loops
•Nested do…while loops
•Nested for loops
a loop is a sequence of
instructions that is
continually repeated
until a certain condition is
reached.
•A loop inside another loop
is called a nested loop.
 while loops may be nested, that is we
can put a while loop inside another
while loop.
However, we must start the inner loop
after starting the outer loop and end the
inner loop before ending the outer loop for
a
logically correct nesting.
To print the following series:-
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<stdio.h>
Int main()
{
int r=1,c=1;
while (r<=5)
{
c=1;
while(c<=r)
{
printf(“%d”,c);
c++;
}
printf(“n”)
r++;
}
return
}
#include<stdio.h>
int main()
{
int a;
do
{
printf(“Enter any number”);
scanf(“%”,&a);
if (a>=0)
printf(“Number is positive”);
else
printf(“Number is Negative”);
}
while(a!=0);
return 0;
}
Enter any number : 1
Number is positive
To print the following:-
#include<stdio.h>
int main()
{
int r,c,k=1;
for (r=1;r<=4;r++)
{
for (c=1;c<=r; c++)
{
printf(“%d”,k);
k++;
}
printf(“n”);
}
return 0;
}
To print the following:-
1
2 3
4 5 6
7 8 9 10
Any questions regarding this topic?

Nested loops

  • 1.
  • 2.
    •What is aloop? •What is a nested loop? •Loops used in nested loops •Nested while loops •Nested do…while loops •Nested for loops
  • 3.
    a loop isa sequence of instructions that is continually repeated until a certain condition is reached.
  • 4.
    •A loop insideanother loop is called a nested loop.
  • 6.
     while loopsmay be nested, that is we can put a while loop inside another while loop. However, we must start the inner loop after starting the outer loop and end the inner loop before ending the outer loop for a logically correct nesting.
  • 8.
    To print thefollowing series:- 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 #include<stdio.h> Int main() { int r=1,c=1; while (r<=5) { c=1; while(c<=r) { printf(“%d”,c); c++; } printf(“n”) r++; } return }
  • 11.
    #include<stdio.h> int main() { int a; do { printf(“Enterany number”); scanf(“%”,&a); if (a>=0) printf(“Number is positive”); else printf(“Number is Negative”); } while(a!=0); return 0; } Enter any number : 1 Number is positive To print the following:-
  • 14.
    #include<stdio.h> int main() { int r,c,k=1; for(r=1;r<=4;r++) { for (c=1;c<=r; c++) { printf(“%d”,k); k++; } printf(“n”); } return 0; } To print the following:- 1 2 3 4 5 6 7 8 9 10
  • 15.