Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!

Do While Loop C++ Example Program For Beginners

Do While Loop C++ Example Program For Beginners

Some of the most important parts of programming are loops that allow your algorithms to run iterations. C and C++ have many options to perform and control loops that count or carry out operations based on a given condition. In addition to the for() loop, do-while loops are the most used loops in C++ programming. These loops can be used in all types of C++ code, including console applications, Windows applications, or apps built with a C++ cross platform framework such a FireMonkey.

In programming, ‘do while’ and ‘while’ loops allow you to repeat a block of code until a specified condition is reached. Loops are used very often in programming and understanding what they do and how to use them is a fundamental programming skill. In this post, we will explain the do-while loop and while loop.

Is there a short C++ example program of how to use a do while loop?

In the do-while loop mechanism, the loop begins with the do keyword. This will execute the code block at least once, before checking if the condition is true. If the condition is true the loop will repeat again. This will continue until the ‘while’ condition is no longer true.

As you see, the code block of the do statement listed between the braces { } and we ended while(condition) with ‘;’.

Let’s try a counting example with do-while() statement,

Here is a full example of the do-while loop

and the output will be as follows,

Is there a do while loop C++ example program using only the while condition?

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, the While() loop checks the condition before the code block inside is executed. When the condition returns false, the control comes out of the loop without performing the code block and jumps to the next statement in the program lines which follow the block.

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() loops in a similar way as for() loops too. If we look at this for loop which counts from 0 to 9:

the for loops exits when the condition evaluates as false. When using a while condition we also compare to see if this condition is true. We can replicate the for loop example like this;

and the output of this example will be same as above result with do-while,

As you see, inside the while loop, in code blocks, we need to modify the conditional parameter i. If we don’t do this the while statement will loop forever!

What are the differences between for loops, while loops and do-while loops in C++?

All these for() loops, while() loops and do-while() loops look similar but there are some differences: 

  • for() statements executes the code block with a constant increment.
  • while() statements check a condition and its code block doesn’t execute if your condition is not true.
  • do-while() statement executes code block inside once and at the end the while() statement checks to decide to loop again or not.

So, developers 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 the condition statement you can also use && operator as an AND operator, || operator as an OR operator, which are logical operands to check multiple conditions as described before.

Next, you’ll find the answers to several questions about String Operations in C++ software.


C++ Builder is the easiest and fastest C and C++ IDE for building applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

What's Coming in Delphi, C++Builder, and RAD Studio 11.2 Alexandria

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

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and other operating systems. He was born in 1974, Eskisehir-Turkey. He graduated from the Department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later, he started to work in the same university as an assistant. He received his MSc and Ph.D. degrees from the same department of the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations. He has married, and he is a father of a son.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.