Do-While loop in C

In this article, we will learn about a loop special loop called a do-while loop in C programming. This is special in that till now you have been told that the loop body will get executed only if the condition is TRUE. But this loop contradicts and executes once even if the condition is false.

  • A do-while loop in C programming is similar to a while loop.
  • In this loop, the condition is always checked after the body of a loop.
  • It is also called an exit-controlled loop.
flowchart do while loop
flowchart_do-while_loop

Syntax of the do-while loop in C programming language is as follows:

Syntax of Do While Loop in C programming:

do
{
  statements
}
  • In some cases, we have to execute a body of the loop at least once even if the condition is false.
  • do-while loop in c programming is used to achive the task of executing the loop body once even if the condition is false.
  • After the loop body gets executed, condition is checked and if the condition is true, then control is transferred to loop body and it gets executed again otherwise transferred out of the loop.
  • As just like the while loop in C programming, once the control transferred out of the loop the immediate statements gets executed.

So the main difference between while loop and do-while loop is that, while loop condition is checked at entry-level whereas, in the do-while loop condition is checked at the end of the loop body so also called exit controlled loop.

The following loop program in C illustrates the working of a do-while loop in which we are going to print the table of 2.

#include<stdio.h>
#include<conio.h>
int main()
{
	int num=1;	
	do	             //begining of do-while loop 
	{
		printf("%d\n",2*num);
		num++;		//incrementing operation
	}while(num<=10);
	return 0;
}

Output:

2
4
6
8
10
12
14
16
18
20

Explanation of the above code (Do-While Loop in C Programming)

  1. First, we have initialized a variable ‘num’ with the value 1.
  2. Then we have written a do-while loop.
  3. In a loop, we have a print function that will print the series by multiplying the value of num with 2.
  4. After each increment, the value of num will increase by 1 and in the next loop it will be multiplied by 2 and printed on the screen.
  5. This will go on until the value of num becomes 10.
  6. As soon as the value of num becomes 10, due to the increment operator it will be increased to 11 and after that loop will be terminated and a statement which is immediately after the loop will be executed.

If you have any compilation errors or doubts about Do while loop in C programming, let us discuss them in the comment section below.

Nested Do While Loop In C Language

  • One loop inside another loop is known as a nested loop.
  • Similarly, one do-while loop inside another do-while loop is called a nested do-while loop.
flowchart_nested_do-while_loop
flowchart_nested_do-while_loop

Syntax

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

Examples for nested do-while loop

Program 1: This program displays a square number pattern in the C language

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

Output

Pattern Printing

12345678910
12345678910
12345678910
12345678910
12345678910
12345678910 
12345678910 
12345678910 
12345678910 
12345678910

Program 3: displays 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");
    
 do{
    j=1;
 do{
    printf("%d",j);
    j++;
 }while(j<=i);
    printf("\n");
    i++;
    }while(i<=9);
    getch();
    return 0;

    }

Output

Triangle number pattern


1
12
123
1234
12345
123456
1234567
12345678
123456789

You can execute your code’s here with different values so see the different outputs.

You Might Like:

1 thought on “Do-While loop in C”

Leave a Comment