Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

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.

[crayon-6769aa06bb051983802777/]

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,

[crayon-6769aa06bb05b059605769/]

Here is a full example of the do-while loop

[crayon-6769aa06bb05d468480604/]

and the output will be as follows,

[crayon-6769aa06bb05f131814769/]

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.

[crayon-6769aa06bb061621154801/]

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:

[crayon-6769aa06bb063718036229/]

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;

[crayon-6769aa06bb064890051937/]
[crayon-6769aa06bb065524431410/]

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

[crayon-6769aa06bb066184600297/]

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: 

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.

Exit mobile version