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,
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.
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.
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); |
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!
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition