×

Do WHILE LOOP in C Programming Examples

Before understanding the programming examples of the Do-While loop, we have to know what is Do-While loop in C. So, let's start with the definition of the Do-While loop.

What is Do-while Loop?

The Do-While loop executes the body at least one time before checking the termination condition and then executes the statements according to the termination condition.

Syntax of the do-while loop in C

do 
{
  /* main body used for the loop */
}
while (termination condition);

Initially, the body of do-while executes only one time. After that, the control executes the termination condition.

It then checks whether the termination command results in true. If yes, it will print the desired outcome on the screen, and it will continue to execute the termination condition repeatedly.

One more thing is that the execution will run multiple times until the condition become false.

As soon as the results become false after the execution of the termination command, then the loop will be automatically dismissed.

Example 01: Program to perform basic addition of various numbers until the user enters 0.

/* code for adding integers until the user assigns the value zero */
#include <stdio.h>
int main() 
{
  double number, total = 0; // variable declaration and initialization of values of                                                    variables
  do 
  {
    printf("Enter a number: "); // body of “do while” loop
    scanf("%lf", &number);     // it is executed only once
    total += number; 
  }
  while(number != 0.0); // termination condition
  printf("Sum = %.2lf",total); // command for printing output 
  return 0;
}

Output

Enter a number: 3
Enter a number: 4
Enter a number: -5
Enter a number: 6
Enter a number: 0
Sum = 8.00

Step by step explanation:

In the above code, the do-while loop is used in such a manner that it lets the user enter the numbers, and the loop will be executed as long as the user does not insert a 0 integer.

The loop in this code executes the body once without checking the termination statement, so it will execute the statement first. After that, the user entered 3, 4, -5, and then 6.

At last, the user enters 0, and the "do while" loop is terminated because the termination condition becomes false. As a result, the output (the sum of all inserted numbers) is displayed on the screen.

A few more similar examples of the "do while" loop in C are discussed below.

Example 02:  Do-While loop with increment statement:

/* this is a program to print numbers to a specific point */
#include <stdio.h>
int main() 
{
  int a;
      printf("enter the number (less than or equal to 3) to start from:\n");
      scanf("%d",&a); // assigning the value entered to a
    do //execute the task without checking the condition only once
  {
      printf("the value of a is %d\n",a);
      a++;//increment statement
  }  
    while(a<=3);//this is termination condition
  return 0;
}

Output:

enter the number (less than or equal to 3) to start from:
-3
the value of a is -3
the value of a is -2
the value of a is -1
the value of a is 0
the value of a is 1
the value of a is 2
the value of a is 3

In this example, firstly, we have declared the variable "a," and then we took the input from the user and assigned the inserted value to “a”.

The code will print the value of “a” without checking the termination condition. Then, it will increment the value of “a” and check the termination condition repeatedly.

According to the input provided by the user in the mentioned output, the value of “a” is -3, but after increment, it becomes -2, -1, 0. The value will increment until ‘a’ reaches 3. When the value of 'a' becomes 3, then the loop will be terminated because the termination condition (a<=3) becomes false.

Example 03: Do-While loop with decrement statement:

/* this is a program to print numbers to a specific point */
#include <stdio.h>
int main() 
{
  int a;
      printf("enter the number (more than or equal to 3) to start from:\n");
      scanf("%d", &a); // assigning the value entered to a
    do //execute the task without checking the condition only once
  {
      printf ("the value of a is %d\n", a);
      a--; //decrement statement
  }  
    while(a>=3); //this is termination condition
  return 0;
}

Output:

enter the number (more than or equal to 3) to start from:
9
the value of a is 9
the value of a is 8
the value of a is 7
the value of a is 6
the value of a is 5
the value of a is 4 
the value of a is 3 

In this example, firstly, we declared the variable "a", then we took the input from the user and assigned the inserted value to “a”.

The code will print the value of “a” without checking the termination condition. Then, it will decrease the value of "a" and repeatedly check the termination condition.

According to the input provided by the user in the mentioned output, the value of “a” is 9, but after decrement, it becomes 8, 7, and 6. The value will decrement until ‘a’ reaches 3. When the value of 'a' becomes 3, then the loop will be terminated because the termination condition (a>=3) becomes false.

Example 04: Program of Do-While loop:

/* this program will calculate the sum of all multiples of the table */
#include <stdio.h>
int main() 
{
  int num , sum, a=0; // Declaring variables and initialization of the value of a
      printf("enter the table you want to add:\n\n");
      scanf("%d", &num);//assigning the value of a entered by user
    do
  {
      printf("%d * %d = %d \n\n", num, a, num * a);
      a++;// increment statement
  }
    while(a<=10);// termination condition
      sum=num*55;
  {
      printf("the sum of all multiples of this table is %d\n\n", sum);
  }
    return 0;
}

Output:

enter the table you want to add:
8
8 * 0 = 0 
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
the sum of all multiples of this table is 440

the sum of all multiples of this table is 440

Example 05: Do-While loop with positive infinity:

/* this is a program to print numbers to an uncertain point */
#include <stdio.h>
int main() 
{
  int a;// declaring variable
      printf("enter the number (more than or equal to 3) to start from:\n");
      scanf("%d", &a); // assigning the value entered to a
    do //execute the task without checking the condition only once
  {
      printf ("the value of a is %d\n", a);
      a++; //increment statement
  }  
    while(a>=3); //this is termination condition
  return 0;
}

Output:

enter the number (more than or equal to 3) to start from:
3
the value of a is 4
the value of a is 5
the value of a is 6
the value of a is 7
the value of a is 8
the value of a is 9 
the value of a is 10………

Example 06: Do-While loop with negative infinity:

/* this is a program to print numbers to an unspecified point */
#include <stdio.h>
int main() 
{
  int a; //declaring variable
      printf("enter the number (less than or equal to 3) to start from:\n");
      scanf("%d", &a); // assigning the value entered to a
    do //execute the task once without checking the condition
  {
      printf ("the value of a is %d\n", a);
      a--; //decrement statement
  }  
    while(a<=3); //this is termination condition
  return 0;
}

Output:

enter the number (less than or equal to 3) to start from:
3
the value of a is 2
the value of a is 1
the value of a is 0
the value of a is -1
the value of a is -2
the value of a is -3
the value of a is -4…………. 



Related Topics

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

How to convert a string to hexadecimal in C

Converting a character array or any string to its respective hexadecimal form is simple. The only thing we have to do is to follow the below steps. Take each character from...

3 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.