
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
do…while loop vs. while loop in C/C++
Here we will see what are the basic differences of do-while loop and the while loop in C or C++.
A while loop in C programming repeatedly executes a target statement as long as a given condition is true. The syntax is like below.
while(condition) { statement(s); }
Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.
When the condition becomes false, the program control passes to the line immediately following the loop.
Example
#include <stdio.h> int main () { int a = 10; // Local variable declaration: do { // do loop execution printf("value of a: %d\n", a); a = a + 1; } while( a < 20 ); return 0; }
Output
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
Now let us see the do-while loop.
Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
do { statement(s); } while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop execute again. This process repeats until the given condition becomes false.
Example
#include <stdio.h> int main () { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %d\n", a); a++; } return 0; }
Output
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
So the differences are summarized in the following table −
While Loop | Do-While Loop |
---|---|
This is entry controlled loop. It checks condition before entering into loop | This is exit control loop. Checks condition when coming out from loop |
The while loop may run zero or more times | Do-While may run more than one times but at least once. |
The variable of test condition must be initialized prior to entering into the loop | The variable for loop condition may also be initialized in the loop also. |
while(condition){ //statement } | do{ //statement }while(condition); |
- Related Articles
- Do-while loop in Arduino
- Difference Between while and do-while Loop
- What are the differences between while loop and do-while loop in Java?
- Java do-while loop example
- Java infinite do-while loop
- How to use C# do while loop?
- The do…while loop in Javascript
- Java while loop
- For Versus While Loop in C
- How to use ‘do while loop’ in Java?
- What is do...while loop statement in JavaScript?
- Infinite while loop in Java
- The while loop in Javascript
- While loop in Lua Programming
- Difference between for loop and while loop in Python
