C++C++11C++14C++17Introduction to C++Learn C++

Learn to Use While and Do-While Loops in C++

While loops allow you to repeat a block of code as long as your specified condition is reached. Loops are used very offend in programming because they save time, reduce errors, and they make code more readable.

1. The While Loop

In the mechanism of the while statement, the condition is evaluated and if it returns true then the code block inside the while loop will be executed, this will repeat until the condition returns false. So While() loop checks condition before the code block inside is executed. When the condition returns false, the control comes out of the loop without doing the code block and jumps to the next statement in the program lines. While statement can be simply shown as below,

Here while do looping only if condition is true, inside it, this condition can be set to false to exit from while. We can use while() loop same as for() loops too. Let’s remember counting 0 to 9 with for loop,

for loops exits when the condition is done. When using while condition generally we do comparison if this condition is true, so we can do this for loop example like this;

As you see, inside the while loop, in code blocks, we need to modify conditional parameter i. If we don’t do this while statement will loop forever, which will cause the use of CPU and stuck of your application.

2. The Do-While Loop

The do-while loop is another usage of the while loop. In this mechanism, the loop begins with do, will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

As you see, here code block of do statement listed in { } and we ended while(condition) with ‘;’. In previous While Loop usage we used { } after the while(condition); Let’s do same counting example with do-while() statement,

All these for() loop, while() loop and do-while() loop looks similar but there are differences when you coding. 

for() statement executes code block with constant increment.

while() statement checks condition and its code block doesn’t work if your condition is not true.

do-while() statement executes code block inside once and at the end while() statement checks to decide to loop again or not.

So developer should choose which is necessary for this loop, by condition, and the code block should be executed once or not. Generally, while statements are good to use with multi-task operations or with components. For example, you can have a component to turn ON and OFF things on your form and your application can run your code blocks inside your do-while loop if this component is turned ON, and your application doesn’t run your code-blocks if it is turned OFF. 

Note that, in condition statement you can also use && (AND) / || (OR) logical operands to check multiple conditions as described before.

Get started building powerful apps with C++Builder!

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?

C++C++11Introduction to C++Learn C++Syntax

Full List Of Features In C++ 11

C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

How To Use Basic Methods Of Strings In C++?

Worth reading...
Learn To Define And Use Arrays In C++