PRESENTATION
By:-
Balwinder Singh
CONTENTS
 What is a Loop?
 Nested While loops.
 Do…..While loops.
 Nested For loops.
 Question.
WHAT IS A LOOP?
 A loop is a programming structure that
contains an executable block of code that
repeats so long as a given condition is
true/not true.
 Good programmers include a way to satisfy
the condition somewhere inside the
executable block.
TYPES OF NESTED LOOPS
 Nested While Loops.
 Do…..While Loops.
 Nested For Loops.
nested
loops
NESTED WHILE LOOPS
 WHILE loops may be nested, that is you
can put a WHILE loop inside another
WHILE loop.
 However, one 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.
EXAMPLE:- NESTED WHILE LOOPS
To print the following series:-
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
EXAMPLE:- NESTED WHILE LOOPS
#include<stdio.h>
#include<conio.h>
main()
{
int r=1,c=1;
while (r<=5)
{
c=1;
while(c<=r)
{
printf(“%d”,c);
c++;
}
printf(“n”);
r++;
}
getch();
}
DO……WHILE LOOP
 Executes the loop statement once and then
repeats the loop statement until the condition
is zero (false).
 Starts with a do statement and then tests the
repetition condition in the while statement at
the end of the loop.
Syntax:- do
{
statements;
}
while(condition);
LOGIC OF A DO….WHILE LOOPS
statement
true false
condition
evaluated
The while Loop
EXAMPLE:- DO….WHILE LOOP
#include<stdio.h>
#include<conio.h>
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);
getch();
}
Accept any number from the user to print the number is
positive or negative.
NESTED FOR LOOPS
 A for loop can contain any kind of statement
in its body, including another for loop.
 The inner loop should have a different name
for its loop counter variable so that it will not
conflict with the outer loop.
EXAMPLE:- NESTED FOR LOOPS
Write a program to print the following series:-
1
2 3
4 5 6
7 8 9 10
EXAMPLE:- NESTED FOR LOOPS
#include<stdio.h>
#include<conio.h>
main()
{
int r,c,k=1;
for (r=1;r<=4;r++)
{
for (c=1;c<=r; c++)
{
printf(“%d”,k);
k++;
}
printf(“n”);
}
getch();
}
ANY QUESTIONS???

presentationonnestingofloops-110228071935-phpapp01.pdf

  • 1.
  • 2.
    CONTENTS  What isa Loop?  Nested While loops.  Do…..While loops.  Nested For loops.  Question.
  • 3.
    WHAT IS ALOOP?  A loop is a programming structure that contains an executable block of code that repeats so long as a given condition is true/not true.  Good programmers include a way to satisfy the condition somewhere inside the executable block.
  • 4.
    TYPES OF NESTEDLOOPS  Nested While Loops.  Do…..While Loops.  Nested For Loops. nested loops
  • 5.
    NESTED WHILE LOOPS WHILE loops may be nested, that is you can put a WHILE loop inside another WHILE loop.  However, one 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.
  • 6.
    EXAMPLE:- NESTED WHILELOOPS To print the following series:- 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
  • 7.
    EXAMPLE:- NESTED WHILELOOPS #include<stdio.h> #include<conio.h> main() { int r=1,c=1; while (r<=5) { c=1; while(c<=r) { printf(“%d”,c); c++; } printf(“n”); r++; } getch(); }
  • 8.
    DO……WHILE LOOP  Executesthe loop statement once and then repeats the loop statement until the condition is zero (false).  Starts with a do statement and then tests the repetition condition in the while statement at the end of the loop. Syntax:- do { statements; } while(condition);
  • 9.
    LOGIC OF ADO….WHILE LOOPS statement true false condition evaluated The while Loop
  • 10.
    EXAMPLE:- DO….WHILE LOOP #include<stdio.h> #include<conio.h> main() { inta; do { printf(“Enter any number”); scanf(“%”,&a); if (a>=0) printf(“Number is positive”); else printf(“Number is Negative”); } while(a!=0); getch(); } Accept any number from the user to print the number is positive or negative.
  • 11.
    NESTED FOR LOOPS A for loop can contain any kind of statement in its body, including another for loop.  The inner loop should have a different name for its loop counter variable so that it will not conflict with the outer loop.
  • 12.
    EXAMPLE:- NESTED FORLOOPS Write a program to print the following series:- 1 2 3 4 5 6 7 8 9 10
  • 13.
    EXAMPLE:- NESTED FORLOOPS #include<stdio.h> #include<conio.h> main() { int r,c,k=1; for (r=1;r<=4;r++) { for (c=1;c<=r; c++) { printf(“%d”,k); k++; } printf(“n”); } getch(); }
  • 14.