Nested loops

AdnanFerdousAhmed 454 views 15 slides Mar 21, 2018
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

A presentation on nested loops of c program.


Slide Content

Nested Loops
By:
Adnan Ferdous Ahmed

•What is a loop?
•What is a nested loop?
•Loops used in nested loops
•Nestedwhile loops
•Nested do…whileloops
•Nested forloops

aloopis a sequence of
instructions that is
continually repeated
until a certain condition is
reached.

•Aloopinside anotherloop
is called a nested loop.

while loops may be nested, that is we
can put a whileloop inside another
whileloop.
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>
Intmain()
{
intr=1,c=1;
while (r<=5)
{
c=1;
while(c<=r)
{
printf(“%d”,c);
c++;
}
printf(“\n”)
r++;
}
return
}

#include<stdio.h>
intmain()
{
inta;
do
{
printf(“Enteranynumber”);
scanf(“%”,&a);
if(a>=0)
printf(“Numberispositive”);
else
printf(“NumberisNegative”);
}
while(a!=0);
return0;
}
Enter any number : 1
Number is positive
To print the following:-

#include<stdio.h>
intmain()
{
intr,c,k=1;
for(r=1;r<=4;r++)
{
for(c=1;c<=r;c++)
{
printf(“%d”,k);
k++;
}
printf(“\n”);
}
return0;
}
To print the following:-
1
2 3
4 5 6
7 8 9 10

Any questions regarding this topic?