Nested Iterative Statement
(Nested Loop)
CSE 1111: Structured Programming Language
Charles Aunkan Gomes
Lecturer
Dept of CSE, UIU
1
Nested Loop
Just like nested if else C allows the looping of statements inside another loop
There is no restriction for defining any number of loops. The nesting level
can be defined at n times.
Example :
Outer_loop {
//outer loop statements…
Inner_loop {
// inner loop statements.
}
// outer loop statements.
}
Nested for loop Syntax
for(initialization; condition; update loop variable) {
// statement 1
………………………….
for(initialization; condition; update loop variable) {
// Inner loop statements
}
// statement n
}
Nested while loop Syntax
while(condition)
{
//outer loop statements.
while(condition)
{
// inner loop statements.
}
// outer loop statements.
}
Nested do…while loop Syntax
do {
// statements
do {
// Inner loop statements
}while(condition);
// statements
}while(condition);
Nested for loop Syntax
for(outVariable initialization ; condition; update outVariable) {
// statement 1
………………………….
outer
loop
for(inVariable initialization; condition; update inVariable) {
Inner
loop
// Inner loop statements
}
// statement n
}
Nested for loop Example
for(i=1 ; i<=3 ; i++){ Sample Output
printf(“*”);
***
}
Nested for loop Example
Desired Output
for(i=1 ; i<=3 ; i++){
***
for(j=1 ; j<=3; j++){ ***
printf(“*”); ***
}
Output for this code
*********
}
How can we solve?
Outer loop indicates/defines the row
Where should we put the "\n”?
Inner loop indicated/defines the column
Nested for loop Example
Sample Output
for(i=1 ; i<=3 ; i++){
***
for(j=1 ; j<=3; j++){ ***
printf(“*”); ***
}
printf(“\n”);
}
Outer loop indicates/defines the row
Inner loop indicated/defines the column
Nested for loop Example Code
for(i=1; i<=2; i++) { Outer loop Inner loop
for(j=1; j<=2; j++) { Value of i condition Execution Value of j condition Execution
printf(“i:%d, j:%d\n”,i,j);
;1 1<=2, True Execute inner loop 1 1<=2,true i:1,j:1
} 2 2<=2,true i:1,j:2
3 3<=2,false
} 2 2<=2,True Execute inner loop 1 1<=2,true i:2,j:1
2 2<=2,true i:2,j:2
3 3<=2,false
3 3<=2,False
Nested for loop Example (printing pattern)
Take input of a number n and print a pattern
for(i=1 ; i<=n ; i++){ Intput: n=3
Sample Output
for(j=1 ; j<=i; j++){ 1
12
printf(“%d”,j); 123
} Input: n=4
printf(“\n”); Sample output
1
} 12
123
1234
Note: CT 2 Syllabus is up to this.
Nested for loop Example 5(printing pattern)
Take input of a number n and print a pattern
for(i=1 ; i<=n ; i++){ Intput: n=3
for(j=0 ; j<i; j++){ Sample Output
3
printf(“%d”,n-j); 32
} 321
printf(“\n”); Input: n=4
} Sample output
4
43
432
4321
Nested for loop Example 5 (Alternate Solution)
for(i=1 ; i<=n ; i++){ Take input of a number n and print a pattern
int printvalue=n; Intput: n=3
Sample Output
for(j=0 ; j<i; j++){ 3
printf(“%d”, printvalue); 32
321
printvalue--;
} Input: n=4
printf(“\n”); Sample output
4
} 43
432
4321
Nested for loop Example 6
for(i=1;i<=n;i++){ Take input of a number n and print a pattern
for(j=1; j<=n-i;j++){ Intput: n=3
printf(" "); For printing space Sample Output
}
1
1 2
for(k=1; k<=i ;k++){
For printing 1 2 3
printf("%d",k); numbers
}
printf("\n");
}
Nested for loop Example 7
for(i=1;i<=n;i++){ Take input of a number n and print a Triangle
for(j=1; j<=n-i;j++){ Intput: n=3
printf(" "); For printing space Sample Output
}
*
* * *
for(k=1; k<=2*i-1 ;k++){
For printing stars * * * * *
printf(“*");
}
printf("\n");
}
Nested for loop Example 8
for(i=1;i<=n;i++){ Take input of a number n and print a Triangle
for(j=1; j<=n-i;j++){ Intput: n=3
printf(" "); For printing space Sample Output
}
1
1 2 3
for(k=1; k<=2*i-1 ;k++){
For printing 1 2 3 4 5
printf(“%d“,k); numbers
}
printf("\n");
}
Nested for loop Example 9
for(i=1;i<=n;i++){ Take input of a number n and print a Triangle
for(j=1; j<=n-i;j++){ For printing Intput: n=3
printf(" "); space Sample Output
}
for(k=1; k<=i ;k++){ 1
printf("%d",k); For printing first 1 2 1
} triangle
1 2 3 2 1
for(l=i ; l>1 ;l--){
printf("%d",l-1); For printing second
} triangle
printf("\n");
}
Nested for loop Sample Problems
For input n = 3
1 2 3 2 1 1
1 2 1 2 3
2 4 5 6
1
1 2 1
1 2 3 2 1