×

Difference between while and do-while loop in C

Introduction

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the same manner. But, there is a sole and significant difference between them. In the do-while loop, the execution of the body takes place at least one time before checking the termination condition. Then it executes the command according to the termination condition. Whereas, in the case of the while loop, the termination condition is checked before the execution of the body.

In other words, in the do-while loop, the body is executed first, and after that, it checks whether the termination condition results in true or false. In contrast, in the while loop, it first executes the termination condition and checks whether the results in true or false. If the result is True, then the statement is executed, otherwise not.

Difference between the syntax

Syntax of while loop:

while (termination condition) {
  // the body of the loop 
}

Syntax of the do-while loop:

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

Processing of Loop

While loop in C:

  1. Initially, we use the parentheses for storing the termination condition of the While loop.
  2. Then, there can be two outcomes.
  3. Firstly, if the result is true, the control will automatically execute the While loop.
  4. It will continue to evaluate again and again until the termination condition is false.
  5. Secondly, if the result is false, the control will come out of the While loop, and the While Loop will be dismissed.

Do-while loop in C:

  1. Initially, one-time execution of the body of Do-While takes place. After that, it executes the termination condition.
  2. 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 again and again.
  3. One more thing is that the execution will run multiple times until the condition become false.
  4. As soon as the results become false after the execution of the termination command, then the loop will be automatically dismissed.

Digrammatical Representation of loop with a flow chart

While loop in C:

Difference between while and do-while loop in C

Do while loop in C:

Difference between while and do-while loop in C

Examples of Loop

While Loop:

/* Printing integers from 1 to 10 */
#include <stdio.h>
int main () 
{
  int a = 1; // initializing the value of a to 1
  while (a <= 10) // termination condition
  {
    Printf ("the value of ‘a’ is %d\n", a); // body of the while-loop 
    ++a; // increment/ decrement statement
  }
  return 0;
}

Output:

the value of a is 1
the value of a is 2
the value of a is 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

Explanation of Code:

The first step is to initialize the value of int a.

Then, we applied a termination condition (while (a <= 10)) through which we can avoid the code from running to infinity.

After that, the main body of the while-loop is implemented just like [printf ("the value of ‘a’ is %d\n", a);]. The main body can print the desired output that we want (see output).

Then, we placed an increment statement (++a;), which increments the value of a one by one.

The code will print "the value of a is 1" as we initialized a to 1. After that, it will perform the increment command and will add 1.

It will evaluate again and checks that '2' is also less than 10. Therefore, the code will print "the value of a is 2".

This evaluation will take place several times until the value becomes 11. The code will terminate the entire process when the termination condition becomes false.

Do-while loop:

      
/* code for adding integers until the user assigns the value zero */
#include <stdio.h>
int main() 
{
  double number, total = 0;
  do 
  {
    printf("Enter a number: ");// body of do-while loop
    scanf("%lf", &number);     // it is executed only once
    total += number;
  }
  while(number != 0.0);


  printf("Sum = %.2lf",total);
  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

Explanation:

In the above program, we used the do-while loop, which processes the code in the following manner:

It lets the user enter the numbers, and the loop is executed until the user inserts 0. If the condition becomes false, the loop terminates, and the code adds all the entered numbers before zero and displays the sum of those numbers on the screen.


Related Topics

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

Types of Function in C

A function is a piece of code that accomplish a certain operation for a specific task. There are two types of functions in C: System-defined function (Library Function)User-defined function 1. System defined Function...

8 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.