nested while loop

Nested while loop in C programming language

Nested while loop in C programming language

In this tutorial, We will learn  about Nested while loop in C programming language

Nested while loop in C language

In C programming language, while loop inside another while loop is known as nested while loop. In nested while loop one or more statements can be included in the body of the loop.

Declaration

The syntax of nested while in C

while(test_expression)
    {
         statements;
      while(test_expression)
            {
             statements;
             }
      }

flow diagram of the nested while loop

Flow chart for nested while loop in C

Initially, Outer while loop executes only once. outer while loop evaluates the test expression. When the condition is falseThe flow of control skips the execution and flow of control come out the outer loop for rest 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, inside the statements are executed  If the test expression is false, the flow of control skips the execution and jumps out of the outer loop.

Example

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i=1;
    while(i<=3){
    printf("%d :inner loop is executed only once\n",i);
    int j=1;
    while(j<=4){
            printf("%d :Outer loop is executed until to completion\n",j);
            j++;
    }
    i++;
    }
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following result:

1 :inner loop is executed only once 
1 :Outer loop is executed until to completion
2 :Outer loop is executed until to completion
3: Outer loop is executed until to completion
4: Outer loop is executed until to completion

1 :inner loop is executed only once 
1 :Outer loop is executed until to completion
2 :Outer loop is executed until to completion
3: Outer loop is executed until to completion
4: Outer loop is executed until to completion

1 :inner loop is executed only once 
1 :Outer loop is executed until to completion
2 :Outer loop is executed until to completion
3: Outer loop is executed until to completion
4: Outer loop is executed until to completion

 

Program 1

Rectangular number pattern printing using nested while loop in C language

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i=1,j;
    while(i<=10){
            j=1;
          while(j<=10){
           printf("%d",j);
       j++;
            }
         printf("\n");
        i++;
        }
    getch();
    return 0;
}

 

When the above code is compiled and executed, it produces the following result:

12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910
12345678910

Program 3

Rectangular star pattern printing using nested while loop in C language

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i=1,j;
    while(i<=10){
            j=1;
          while(j<=10){
           printf("*");
       j++;
            }
         printf("\n");
        i++;
        }
    getch();
    return 0;
}

 

When the above code is compiled and executed, it produces the following result:

**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

 

Program 3

#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf("Multiplication table\n\n");
    int i=1,j;
    while(i<=10){
            j=1;
          while(j<=10){
                printf("%d\t",j*i);
        j++;
            }
            printf("\n");
         i++;
        }
    getch();
    return 0;
}

 

When the above code is compiled and executed, it produced the following result.

multiplication table printing using nested while loop in C language

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     24     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

 

Nested while loop with the array in C language

#include <stdio.h>
#include <stdlib.h>

int main()
{
    const char* names[3][4]={
    {"Nimal","Kumar","Sugu","Thibi"},
    {"Puvi","Khan","Jhon","Sumi"},
    {"Kabil","Kuna","Kuru","Puvi"},
    };
    int i=0;
    while(i<=2){
        int j=0;
        while(j<=3){
            printf("%s\n",names[i][j]);
            j++;
        }
        i++;
    }
    printf("End the program\n");
    getch();
    return 0;
}

When the above code is compiled and executed, it produced the following result.

Nimal
Kumar
Sugu
Thibi
Pavi
Khan
Jhon
Sumi
Kabil
Kuna
Puvi
End the program

Know more about Array

Array in Programming languages

Similar post

for loop in C++ language

While loop in C++ language

Nested for loop in C++ language

Nested while loop in C++ language

New keyword in Java language

For loop in Java language

While loop in Java language

Nested for loop in Java language

Nested while loop in Java language

 

 

Suggested for you

If statement in Java language

If statement in C++ language

If statement in C language

If statement in Python language

Nested if in Java language

Nested if in C language

Nested if in C++ language

Nested if in Python language

Two dim Array in C++ language

Three dim Array in C++ language

Single dim Array in Java language

Two dim Array in Java language

Three dim Array in Java language

Single dim Array in C language

Two dim Array in C language

Three dim Array in C language

Nested while loop in Java programming language
Nested while loop in Cpp programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

1 day ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

4 days ago

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

1 week ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 weeks ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

1 month ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

2 months ago