Home » Difference between while loop and do-while loop in C

Difference between while loop and do-while loop in C

by Online Tutorials Library

Difference between while loop and do-while loop in C?

In this article, we learn comparison between the while loop and do-while loop constructs in C language. At the beginning of this article, we understand the concept of while loop and do-while loop in C. After that, we learn the comparisons of while and do-while loop in C programming.

while loop vs do-while loop in C

While loop statement:

While loop statement is one of the easiest and most widely used looping constructs in the C programming language.

In this statement, while it is a reserved word or keyword, the condition can be any constant, variable, expression, and statement can be a single statement or a group of statements. In the while loop, we have to perform three steps:

  • Initialization
  • Test Condition
  • Increment and decrement

Initialization Value: In a while loop, the first step is initialization is used to set the initial value of the loop counter. The loop counter may be an increment counter or decrement counter in while loop.

For Example: i = 1;

Test Condition: After the initialization step, the next step in the while loop is to check the test condition whether the loop is executed or not. If the condition is true in the while loop, the loop’s body is executed; otherwise, there is no loop execution.

For Example: while (i<=10)

Increment and decrement: After checking the test condition in the while loop, increment and decrement are used to increment and decrement the loop counter’s value.

For Example: i = i + 1;

Syntax of while loop:

In a while loop statement, while keyword is used and followed by any good condition that has to be tested enclosed in parentheses and it may contain any logical operator. Condition is followed by a pair of curly brackets that specifies the set of statements that are to be executed while the condition is true.

Example:

Flowchart of while loop statement:

while loop vs do-while loop in C

do-while loop statement:

In the C programming language, the do-while loop statement is also similar to a while loop in the C programming language. In this, first of all, the loop’s body is executed then the condition is checked. In the do-while loop, we have to perform three steps:

  • Initialization
  • Test Condition
  • Increment and decrement

Initialization value: In a do-while loop, the first step is initialization and is used to set the initial value of the loop counter. The loop counter may be an increment counter or decrement counter.

For Example: i = 1;

Test Condition: After the initialization step, the next step in the do-while loop is to check the test condition whether the loop is executed or not.

For Example: while (i<=10);

Increment and decrement: After checking the test condition in the do-while loop, the next step is to increment and decrement the loop counter’s value.

For Example: i = i + 1;

Syntax of do-while loop:

Here, two keywords are used for the do-while loop statement is do and while. If the condition is true in the do-while loop, the statement executes again, and if the condition returns false, execution stops, and control passes to the next statement.

Example:

Flowchart of do-while loop statement:

while loop vs do-while loop in C

while loop vs do-while loop in C

Let’s see some Comparison between the while loop and do-while loop in the C language

SR.NO while loop do-while loop
1. While the loop is an entry control loop because firstly, the condition is checked, then the loop’s body is executed. The do-while loop is an exit control loop because in this, first of all, the body of the loop is executed then the condition is checked true or false.
2. The statement of while loop may not be executed at all. The statement of the do-while loop must be executed at least once.
3. The while loop terminates when the condition becomes false. As long as the condition is true, the compiler keeps executing the loop in the do-while loop.
4. In a while loop, the test condition variable must be initialized first to check the test condition in the loop. In a do-while loop, the variable of test condition Initialized in the loop also.
5. In a while loop, at the end of the condition, there is no semicolon.
Syntax:
while (condition)
In this, at the end of the condition, there is a semicolon.
Syntax:
while (condition);
6. While loop is not used for creating menu-driven programs. It is mostly used for creating menu-driven programs because at least one time; the loop is executed whether the condition is true or false.
7. In a while loop, the number of executions depends on the condition defined in the while block. In a do-while loop, irrespective of the condition mentioned, a minimum of 1 execution occurs.
8. Syntax of while loop:
while (condition)  {  Block of statements;  }  Statement-x;
Syntax of do-while loop:
do  {  statements;  }  while (condition);  Statement-x;
9. Program of while loop:
Program of while loop:    #include   #include   Void main()  {  int i;  clrscr();  i = 1;  while(i<=10)  {  printf("hello");  i = i + 1;  }  getch();  }
Program of do-while loop:
#include   #include   Void main()  {  int i;  clrscr();  i = 1;  do  {  printf("hello");  i = i + 1;  }  while(i<=10);  getch();  }
10. Flowchart of while loop:
while loop vs do-while loop in C
Flowchart of do-while loop:
while loop vs do-while loop in C

Next TopicMemory Layout in C

You may also like