Structured Programming Language
Nested Loop
Mohammad Imam Hossain,
Lecturer, CSE, UIU
Nested Loop
• A nested loop is a loop within a loop i.e.
an inner loop within the body of an outer
one.
• The first pass of the outer loop triggers
the inner loop, which executes to
completion.
• Then the second pass of the outer loop
triggers the inner loop again.
• This repeats until the outer loop finishes.
Nested for Loop
#include <stdio.h>
int main()
{
int n=3, m=5;
int cnt=0,i,j;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
++cnt;
}
}
printf("inner loop iterates in total %d timesn",cnt);
return 0;
}
Trace
it……
Total no of Iterations
• When i=1, the inner loop executes m times
• When i=2, the inner loop executes m times
• .
• .
• .
• When i=n, the inner loop executes m times.
So total no of iterations = m + m + m+ … … … +m = n *m
Nested for Loop
Trace
it……
#include <stdio.h>
int main()
{
int n=10;
int cnt=0,i,j;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
++cnt;
}
}
printf("inner loop iterates in total %d timesn",cnt);
return 0;
}
Total no of Iterations
• When i=1, the inner loop executes 1 times
• When i=2, the inner loop executes 2 times
• When i=3, the inner loop executes 3 times
• .
• .
• When i=n, the inner loop executes n times.
So total no of iterations = 1 + 2 + 3+ … … … +n
= n(n+1) / 2
Flow Chart of Nested Loop
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * *
* *
* *
* *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * *
* *
* *
* *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
*
* *
* * *
* * * *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
*
* *
* * *
* * * *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
*
* *
* *
* *
* * * * *
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * *
* * * *
* * *
* *
*
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * *
* * * *
* * *
* *
*
Problem
• Input: n (integer)
• Output: the following shape when n=5
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Problem
• Input: n (integer)
• Output: the following shape when n=5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
Problem
• Input: n (integer)
• Output: the following shape when n=5
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Problem
• Input: n (integer)
• Output: the following shape when n=5
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Problem
• Input: n (integer)
• Output: the following shape when n=4
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
References
• https://www.programtopia.net/cprogramming/docs/
nested-loop
• Number pyramids:
http://www.codeforwin.in/2016/06/number-
pattern-programs-in-c.html
• Star pyramids:
http://www.codeforwin.in/2015/07/star-patterns-
program-in-c.html

SPL 11 | Nested Loops in C

  • 1.
    Structured Programming Language NestedLoop Mohammad Imam Hossain, Lecturer, CSE, UIU
  • 2.
    Nested Loop • Anested loop is a loop within a loop i.e. an inner loop within the body of an outer one. • The first pass of the outer loop triggers the inner loop, which executes to completion. • Then the second pass of the outer loop triggers the inner loop again. • This repeats until the outer loop finishes.
  • 3.
    Nested for Loop #include<stdio.h> int main() { int n=3, m=5; int cnt=0,i,j; for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ ++cnt; } } printf("inner loop iterates in total %d timesn",cnt); return 0; } Trace it……
  • 4.
    Total no ofIterations • When i=1, the inner loop executes m times • When i=2, the inner loop executes m times • . • . • . • When i=n, the inner loop executes m times. So total no of iterations = m + m + m+ … … … +m = n *m
  • 5.
    Nested for Loop Trace it…… #include<stdio.h> int main() { int n=10; int cnt=0,i,j; for(i=1;i<=n;i++){ for(j=1;j<=i;j++){ ++cnt; } } printf("inner loop iterates in total %d timesn",cnt); return 0; }
  • 6.
    Total no ofIterations • When i=1, the inner loop executes 1 times • When i=2, the inner loop executes 2 times • When i=3, the inner loop executes 3 times • . • . • When i=n, the inner loop executes n times. So total no of iterations = 1 + 2 + 3+ … … … +n = n(n+1) / 2
  • 7.
    Flow Chart ofNested Loop
  • 8.
    Problem • Input: n(integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * * * * * * * * * * * *
  • 10.
    Problem • Input: n(integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * * *
  • 11.
    Problem • Input: n(integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * * * * * * * * * * * *
  • 12.
    Problem • Input: n(integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * * *
  • 13.
    Problem • Input: n(integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * *
  • 15.
    Problem • Input: n(integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * *
  • 16.
    Problem • Input: n(integer) • Output: the following shape when n=5 * * * * * * * * * * * *
  • 17.
    Problem • Input: n(integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * *
  • 18.
    Problem • Input: n(integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * *
  • 19.
    Problem • Input: n(integer) • Output: the following shape when n=5 * * * * * * * * * * * * * * * * * * * * * * * * *
  • 20.
    Problem • Input: n(integer) • Output: the following shape when n=5 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
  • 21.
    Problem • Input: n(integer) • Output: the following shape when n=5 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1
  • 22.
    Problem • Input: n(integer) • Output: the following shape when n=5 5 4 3 2 1 5 4 3 2 5 4 3 5 4 5
  • 23.
    Problem • Input: n(integer) • Output: the following shape when n=4 1 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7
  • 25.
    References • https://www.programtopia.net/cprogramming/docs/ nested-loop • Numberpyramids: http://www.codeforwin.in/2016/06/number- pattern-programs-in-c.html • Star pyramids: http://www.codeforwin.in/2015/07/star-patterns- program-in-c.html