In this tutorial section we will learn what is Nested while loop in C Language?.
What is Nested while loop in C?
Nested while loop: Nested while loop in C Programming is refers to a while loop within a another while loop.There can be any number of loops. To create nested loops, we can nest multiple types of loops within one other. Nested Loops are supported by the C programming language.
In general, first iteration of nested outer while loop is executed after that inner nested while loop is exeuted. The execution of inner nested while loop continues till the condition in inner while loop is true.Once the inner while loop finish the execution the program control moves to the next iteration of outer while loop.
Following are some real examples/use case of Nested while loop
e.g.
1. Unlock the mobile phone with 3 password attempts.
2.Spelling checking in microsoft word.
3. Username and password validation: validate username and password till both are not correct.
4.One time password (OTP) validation.
Nested while loop in C Language
Advantage using Nested while loop in C programming
Following are some of advantage using Nested while loop in C programming.
1. Nested while loop improve the CPU performance.
2.Nested loop can be used to process multi-dimensional data structure.
Disadvantage using Nested while loop in C programming
Following are some of Disadvantage using Nested while loop in C programming.
1. Nested while loop process data if the nested data is large they can consume more memory.
2.Nested loop is more difficult to understand and debug.
3. Nested while loop is not more efficient when data is not well organized.
Given Diagram is the flowchart how while loop works in c programming.
Initially, Outer while loop executes only once. outer while loop evaluates the test expression. When the condition is false, The flow of control skips the execution and control come out the outer loop.When the condition is true, the flow of control jumps to the inner while loop.
Then, test expression evaluates the condition of the inner loop. if the test expression is true then the block of statements inside the inner while loop executes, If the test expression is false then skips the statement execution and control jumps out of the inner loop and start executing outer loop statement
"update expression outer loop". after successful execution of the statement control goes back to while condition outer loop and test the condtion.The process continues till condition is true.
Let us study real-world syntax of Nested while loop and then we will look detail into all parts of the Nested while loop.
/* statements outside loop.
initilization */
while(Outer Condition)
{
/*Outer while statements */
while(Inner Condition)
{
/*Inner while Statements; */
}
/*Outer While Statements */
}
/* statement outside loop */
Syntax Explanation :
all statements ouside the loops executes i.e /*Statements outside loop */
1. Control enters into the outer while loop.
2.The control jumps to the Outer Condition inside outer while loop.
3. The condition is tested. Here condition tested either be true or false.
3.a. if the condition tested is false, then control do not enters in to loops. it transfer to outside loop and start executing /* Statements outside the loop.*/
and program stops.
3.b. if the condition tested is true, then the flow goes inside the loop and executes the statements /* Outer while statements. */
4.after executing these statements control enters inner while loop.
Then The inner condition tested inside inner while loop.
5.a if tested condition is true then statements inside inner while loop i.e/* Inner while statements */ executes .
6.after this control jumps back to test inner condition. that means execution repeats from step no 4.
5.b if tested condition is false control jumps outside the inner while loop and execute /*Outer while statements.*/ control jumps to step 7.
7.Control jumps back to outer while loop again AND test the outer condition.
In short we can say it again repeat continuation from step no 2.
C program to illustrate use of Nested while loop
/* C program given below displays Table from 1 to 10 Number */
#include <stdio.h>
int main()
{
int n=1,i=1;
printf("\n Table from 1...10 Using Nested While loop");
while(n<=10)
{
i=1;
while(i<=10)
{
printf("\t %d",n*i);
i++;
}
n++;
printf("\n");
}
return(0);
}
Output:
Table from 1...10 Using Nested While loop
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 26 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Explanation : Variable int n=1,i=1; declared and initilized
printf("\n Table from 1...10 Using Nested While loop");
Shows the message on console
Table from 1...10 Using Nested While loop
Next control enters into the outer while(n<=10) loop and test the condition
i.e. while (1<10) which is true control enters into the body of outer loop .
after that i=1 is initilized, next control enters into the inner loop and test the condition while(i<=10)
which is true and control enters into the inner loop body
and executes printf("\t %d",n*i); displays output 1 .
Next i++ increase value of i by 1 then i becomes i=2 .
The control enters into the inner while loop and test the condition while(i<=10) which is true and execute body of loop and shows output 2.
The inner loop continues till value of i <= 10 i.e. while(i<=10).
If while(i<=10 ) becomes false control goes to outer loop and increase the value of n++ by one.
then n becomes n=2 and the rest of loop iteration continues till value of n<=10.
finaly we get the table output from 1 2 3........10.
Previous Topic:-->> while loop in C || Next topic:-->>Difference Between while and do while loop in C.