C is one of the most powerful programming languages and it’s suitable for a wide variety of uses. You can compile C code with several different C and C++ compilers and IDE. The RAD Studio and C++ Builder IDEs are extremely powerful development tools for those who want to develop C and C++ applications of all types. In this post, we will explain how we can use do while programming in C. For example, you may have char array data and you may want to convert a char
array to string
C++ types with a do-while()
or while()
loop.
Right now we have many great selection of free C++ IDE and professional C++ IDE. Tools like C++ Builder CE, Dev-C++, and even the BCC32 command line compiler. Although C++ Builder’s primary purpose is to be the best available C++ IDE which helps developers be at their most efficient and effective possible selves it is also quite capable of editing and compiling most C code thanks to C and C++ having a shared lineage. The latest version RAD Studio 11.2 is announced with many new features.
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.
Table of Contents
Is there a short example Do while program in C?
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.
1 2 3 4 5 6 |
do { // codes here } while(condition); |
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,
1 2 3 4 5 6 7 |
int i = 0; do { i++; } while (i < 10); |
What about a full example of a do while program in C?
Here is a C example,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> main() { int i = 0; do { i++; printf( "%d\n" , i ); } while (i < 10); } |
Can you use a while loop in C without do?
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,
1 2 3 4 5 6 |
while(condition) { // codes here } |
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,
1 2 3 4 5 |
for( int i =0; i< 10; i++) { } |
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;
1 2 3 4 5 6 7 |
int i = 0; while (i < 10) { i++; } |
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.
Is there full example of a do while program in C?
Here is a while loop example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> main() { int i = 0; while (i < 10) { i++; printf( "%d\n" , i ); } } |
What are the benefits of a do while program loop in C?
All these for() loop, while() loop and do-while() loop look similar but there are differences when you are 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.
Can I use do while program In C and C++, for example with C++ Builder ?
Yes! In C++ Builder, you can use while, and do-while loops in both C and C++ applications. C++ Builder is a grey-haired genius that supports the latest CLANG standards which means it is compatible with other C and C++ compilers that also use these standards.
Although the free C++ Builder Community Edition is extremely powerful it is intended for students, beginners, and startups. If you are a regular business or do not qualify for the free community edition then you can download a free trial of the very latest full RAD Studio C++ Builder version.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional 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.