nested while loop

Nested do while loop in C language

Nested do while loop in C language

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

In C programming language, one do-while loop inside another do-while loop is known as nested do -while loop

Nested do while loop in C

Declaration

Syntax

statements
do{
statements
do{
statements
}while(condition);
statements
}while(condition);

 

Flow diagram of the Nested Do-while loop in C

Flow diagram – Nested do wile loop

How to work Nested do while loop

initially, the initialization statement is executed only once and statements(do part) execute only one. Then, the flow of control evaluates the test expression.

When the test expression is true, the flow of control enter the inner loop and codes inside the body of the inner loop is executed and updating statements are updated. This process repeats until the test expression is false(inner while loop)

Then, when the test expression is false, loop exit from the inner loop and flow of control comes to the outer loop. Again, the flow of control evaluates test condition.

If test condition for outer loop returns as true, the flow of control executes the body of while loop statements and return as false. The flow of control stops execution and goes to rest.

Examples for nested do-while loop

program 1

This program displays a square number pattern in C language

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

int main()
{
    int i,j;
    i=1;
    printf("Square number pattern\n\n");
    printf("Here your pattern\n\n");
 do{
    j=1;
 do{
    printf("%d",j);
    j++;
 }while(j<=10);
    printf("\n");
    i++;
    }while(i<=10);
    getch();
    return 0;

    }

When the above code is executed, it produces the following results:

Square number pattern

Here your pattern

12345678910
12345678910
12345678910
12345678910
12345678910
12345678910 
12345678910 
12345678910 
12345678910 
12345678910

 

Program 2

This program displays a square star pattern in C language

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

int main()
{
    int i,j;
    i=1;
    printf("Asterisk number pattern\n\n");
    printf("Here your pattern\n\n");
 do{
    j=1;
 do{
    printf("*");
    j++;
 }while(j<=9);
    printf("\n");
    i++;
    }while(i<=9);
    getch();
    return 0;

    }

When the above code is executed, it produces the following results:

Asterisk number pattern

Here your pattern

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

The above program displays a square star pattern in C language using nested do-while loop

Program 3

This program displays a floyd’s triangle number pattern in C language using nested do-while loop

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

int main()
{
    int i,j;
    i=1;
    printf("Triangle number pattern\n\n");
    printf("Here your pattern\n\n");
 do{
    j=1;
 do{
    printf("%d",j);
    j++;
 }while(j<=i);
    printf("\n");
    i++;
    }while(i<=9);
    getch();
    return 0;

    }

When the above code is executed, it produces the following results:

Triangle number pattern

Here your pattern

1
12
123
1234
12345
123456
1234567
12345678
123456789

This program displays a Floyd triangle number pattern using nested do-while loop in C language

Program 4

Print multiplication table using nested do-while loop in C

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

int main()
{
    int i,j;
    i=1;
    printf("Multiplication table\n\n");
    printf("Here multiplication table\n\n");
 do{
    j=1;
 do{
    printf("%d\t",i*j);
    j++;
 }while(j<=10);
    printf("\n");
    i++;
    }while(i<=10);
    getch();
    return 0;

    }

 

Multiplication table

Sugested for you

While loop in Java language

While loop in C language

While loop in C++ language

While loop in Python language

Do-while loop in Java language

Do-while loop in C language

Do-while loop in C++ language

 

Nested while loop in Python programming language
Loops in C 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.

Share
Published by
Karmehavannan

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago