Home C Programming Language / Programs / Nested while Loops in C Programming Language
🚀 Programming Example

Nested while Loops in C Programming Language

👁 1,938 Views
💻 Practical Program
📘 Step Learning

The way if statements can be nested, similarly while and for can also be nested. To understand how nested loops work, look at the program given below:

💻 Program Code

#include <stdio.h>
void main()
{
int i = 1;
int j = 1;

while(i<=3){
  while(j<=3){
    printf("Welcome ");
    j++;
   }
    printf("\n");
    j = 1;
i++; 
}
  
}
                        

🖥 Program Output

Welcome Welcome Welcome
Welcome Welcome Welcome
Welcome Welcome Welcome
Press any key to continue . . .
                            

📘 Explanation

Explanation of Nested While Loop Program in C

This C program uses nested while loops to print the word "Welcome" multiple times in a row and column pattern.

Program


#include 

void main()
{
    int i = 1;
    int j = 1;

    while(i <= 3)
    {
        while(j <= 3)
        {
            printf("Welcome ");
            j++;
        }

        printf("\n");
        j = 1;
        i++;
    }
}

Step 1: Variable Initialization


int i = 1;
int j = 1;
  • i is used to control the outer loop (rows).
  • j is used to control the inner loop (columns).

Step 2: Outer While Loop


while(i <= 3)

The outer loop executes 3 times because the value of i starts from 1 and continues until 3. Each iteration of the outer loop represents one row.

Step 3: Inner While Loop


while(j <= 3)
{
    printf("Welcome ");
    j++;
}

The inner loop prints the word "Welcome" three times in a single row. After each print operation, the value of j is increased by 1.

Value of j Output
1 Welcome
2 Welcome
3 Welcome

When j becomes 4, the condition j <= 3 becomes false and the inner loop terminates.

Step 4: Move to the Next Line


printf("\n");

This statement moves the cursor to the next line after printing three "Welcome" words.

Step 5: Reset the Inner Loop Counter


j = 1;

The value of j is reset to 1 so that the inner loop can execute again for the next row. Without this statement, the inner loop would not run again because j would already be greater than 3.

Step 6: Increment the Outer Loop Counter


i++;

The value of i is increased by 1 to move to the next row.

Execution Flow

First Iteration (i = 1)


Welcome Welcome Welcome

Second Iteration (i = 2)


Welcome Welcome Welcome

Third Iteration (i = 3)


Welcome Welcome Welcome

Output


Welcome Welcome Welcome
Welcome Welcome Welcome
Welcome Welcome Welcome

How Nested Loops Work Here

  • The outer loop controls the number of rows.
  • The inner loop controls how many times "Welcome" is printed in each row.
  • The inner loop runs completely for each iteration of the outer loop.
  • Resetting j to 1 allows the inner loop to execute again for the next row.

Conclusion

This program demonstrates the use of nested while loops in C. The outer loop creates three rows, while the inner loop prints the word "Welcome" three times in each row, producing a 3 × 3 pattern.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.