Nested while Loop in C Programming with Syntax and Example
In this tutorial section, we will understand what a Nested while loop is in the C Language and how it works with real-world examples.
What is a Nested while loop in C?
A Nested while loop in C means having one while loop placed inside another. In simple words, it’s a loop within a loop. You can nest any number of loops based on your logic. C Programming fully supports this structure, and it’s often used in complex tasks that need repeated checking or processing of conditions.
Usually, the outer while loop starts first. Then, the inner while loop runs completely for each iteration of the outer loop. The inner loop keeps executing as long as its condition is true. Once it finishes, control returns to the outer loop for the next round. This keeps repeating until the outer loop condition becomes false.
Where do we use Nested while loops in real life?
Here are some everyday situations where nested while loops apply:
1. Unlocking a mobile phone with up to 3 password attempts.
2. Spell checking in software like Microsoft Word.
3. Validating both username and password until both are correct.
4. One-Time Password (OTP) input and validation within a limited number of tries.
Advantage of using Nested while loop in C programming
Following are some of the key advantages of using Nested while loops in C programming:
1. Nested while loops can improve CPU efficiency when structured properly for repetitive tasks.
2. They are useful for processing multi-dimensional data structures like matrices or grids.
3. Helpful in handling complex logic where one loop depends on another, such as validating user credentials or OTP verification.
4. Great for building table-based outputs (like multiplication tables) or simulations that require row-column style execution.
5. Allow better control over layered or step-wise data processing, such as file parsing or nested menu systems.
Disadvantage of using Nested while loop in C programming
Following are some of the disadvantages of using Nested while loops in C programming:
1. If the nested data is large, nested while loops can consume more memory and CPU time.
2. Nested loops are harder to understand and debug, especially for beginners or in complex logic.
3. When the data is not well-structured, nested while loops can make the code less efficient and harder to maintain.
4. Improper nesting or missing loop conditions may lead to infinite loops or logic errors.
5. Overuse of nested loops can reduce code readability and make it harder for others to modify or extend the program later.
Given diagram shows the flowchart of how a while loop works in C programming.
Initially, the outer while loop evaluates the test expression. If the condition is false, the flow of control skips the loop and comes out of the outer loop. If the condition is true, the control enters the inner while loop.
Inside the inner loop, the test expression is again evaluated. If it’s true, the statements inside the inner loop are executed. If the condition is false, the control skips the inner loop and jumps to the outer loop’s update expression.
After executing the outer loop's statement, the control goes back to the outer while condition and rechecks it. This process continues as long as the condition remains true.
Syntax of Nested while loop in C:
Let us first look at a real-world example of the syntax for a nested while loop in C and then we will go into detail about each part of it.
/* statements outside loop.
initilization */
while(Outer Condition)
{
/*Outer while statements */
while(Inner Condition)
{
/*Inner while Statements; */
}
/*Outer While Statements */
}
/* statement outside loop */
Syntax Explanation:
Before we get into how it works, remember that any statements written outside the loops will execute first — that’s what we mean by /* Statements outside loop */.
1. The program starts by entering the outer while loop.
2. It checks the condition written in the outer while loop.
3. Now two things can happen:
3.a. If the condition is false, the program skips the loop entirely and jumps straight to the statements outside the loop — and ends there.
3.b. If the condition is true, the program enters the loop and starts executing the statements inside the outer while loop — these are your /* Outer while statements */.
4. After the outer loop’s statements run, the program now goes inside the inner while loop.
5. The inner loop condition is checked.
5.a. If true, it runs the statements inside the inner while loop — these are your /* Inner while statements */.
6. Then the control goes back to recheck the inner loop condition. If it’s still true, it keeps running those inner statements again. This process repeats until the inner condition becomes false.
5.b. If the inner condition is false, the program exits the inner while loop and continues with the remaining statements inside the outer loop (if any). Then it moves to step 7.
7. Now it goes back to the top of the outer while loop, rechecks the outer condition, and if it's still true, the whole process repeats from step 2.
In short, the inner loop keeps running until its condition becomes false. Once that happens, the outer loop picks up again — and this keeps going until the outer loop’s condition also turns false. That’s how nested while loops work in a step-by-step flow.