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.

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 was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is An Eligible Copy Assignment Operator In C++?

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

What Is A Trivial Copy Assignment Operator In C++?

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

What Is Uppercase T in C++ Syntax?

C++C++11C++14C++17Learn C++Syntax

What Is An Implicitly-defined Copy Assignment Operator In C++?

Worth reading...
Learn To Use Break And Continue In Loops With C++