answersLogoWhite

0


Best Answer

You use a while() loop when you want to test a condition before entering a loop for the first time, which may bypass the loop completely. The condition is also tested before beginning each iteration.

A do..while() loop always executes the loop at least once, and tests the condition at the end of each iteration before beginning a new iteration.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

There no merits or demerits as such, you simply use one or the other depending on what type of loop you want, pre-supposing that a for() loop would be unsuitable. However all three can be used to produce similar loops, it's really just a case of which makes most sense in terms of readability.

The for() loop is useful in that the initial condition can be used to declare one or more local variables that are used by the conditional expression. These variables fall from scope when the loop finishes. The for() loop also allows you to specify one or more statements that must be executed before evaluating the condition at the end of each iteration. All statements are optional, but if the only statement required is the conditional expression then a while() loop would be a better option. A do..while() loop is beneficial if you want the loop to execute at least once before evaluating the conditional expression. The only real demerit to while() and do..while() loops is that all variables used by the conditional expression must be declared outside the loop.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the merits and demerits of while loop and do while loop in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are the Merits and demerits of exception handling in c plus plus?

Are mala cha pahije disadvantages.. ani tumhi mala vichartat.. ask to scientist....


Is for loop is faster than while loop in turbo c plus plus compiler?

No, why did you think so?


What is a while statement in turbo c plus plus?

A while statement is one type of looping statement. by which we can start a loop in our programs. while loop is precondition checking statement, because it first check its condition then loop will go to its body part. EX. while(i>0) { //body part } here when i will >0 then it will check it body part and execute it and display result.


Which loop does not have an entry condition in C plus plus?

The only loop that does not require an entry condition is the procedural goto loop: again: /*...*/ goto again; Although a do-while loop has no entry condition per-se, it still requires a mandatory entry condition into the second and all subsequent iterations. do { /*...*/} while (true); // mandatory entry condition into all but the 1st iteration And although a for loop's condition is optional, it is implicit: for (;;) {/*..*/} // implicit for ever loop for (;true;) {/*...*/} // explicit for ever loop


Explain the difference between for and while loop and give the suitable examples in c plus plus?

There is no actual difference; a for loop is just syntactic sugar for a while loop. Which you use depends largely upon which makes the most sense within the context of your source code. The for loop is clearly more flexible, but you will generally use a for loop whenever the number of iterations is known in advance, such as when counting iterations, whereas while loops are generally used whenever the number of iterations is unknown or infinite. Regardless, this has no effect on the efficiency of your code (the machine code maps almost directly, 1-to-1, with a while loop), it's just a question of which makes your code easier to read. One useful property of a for loop is that you can declare and initialise a control variable in the initial expression. This renders the control variable local to the loop, which is something you cannot achieve with a while loop. This has no effect on the resultant machine code, but by scoping variables within a for loop you automatically enlist the help of the compiler to eliminate bugs. It should be noted that the do-while loop is similar to a while loop, except that a do-while loop always executes its statements at least once, because the conditional expression is evaluated at the end of each iteration, rather than before each iteration as it is with a while loop. Again, a for loop can be used to achieve a do-while loop, however the do-while loop maps closely with the resultant machine code, and is generally much easier to read.

Related questions

What are the Merits and demerits of exception handling in c plus plus?

Are mala cha pahije disadvantages.. ani tumhi mala vichartat.. ask to scientist....


Is for loop is faster than while loop in turbo c plus plus compiler?

No, why did you think so?


Example of flowchart of while loop in c plus plus?

kk


How to complete this C plus plus End Of File controlled while loop program?

Add the missing parts.


What is an infinite loop in c plus plus?

An infinite loop is one sequence of commands that just repeats over and over again forever. When it comes to creating an infinite loop you can use the: for do while and do statements. using the keywords 'true'


What is a while statement in turbo c plus plus?

A while statement is one type of looping statement. by which we can start a loop in our programs. while loop is precondition checking statement, because it first check its condition then loop will go to its body part. EX. while(i>0) { //body part } here when i will >0 then it will check it body part and execute it and display result.


Which loop does not have an entry condition in C plus plus?

The only loop that does not require an entry condition is the procedural goto loop: again: /*...*/ goto again; Although a do-while loop has no entry condition per-se, it still requires a mandatory entry condition into the second and all subsequent iterations. do { /*...*/} while (true); // mandatory entry condition into all but the 1st iteration And although a for loop's condition is optional, it is implicit: for (;;) {/*..*/} // implicit for ever loop for (;true;) {/*...*/} // explicit for ever loop


C plus plus for while loop example code?

The syntax for a for loop is:for (initialization; condition; increase) {statement;}for example:int arr[5] = {1, 2, 3, 4, 5};for (int i = 0; i < 5; i++) {cout


Explain the difference between for and while loop and give the suitable examples in c plus plus?

There is no actual difference; a for loop is just syntactic sugar for a while loop. Which you use depends largely upon which makes the most sense within the context of your source code. The for loop is clearly more flexible, but you will generally use a for loop whenever the number of iterations is known in advance, such as when counting iterations, whereas while loops are generally used whenever the number of iterations is unknown or infinite. Regardless, this has no effect on the efficiency of your code (the machine code maps almost directly, 1-to-1, with a while loop), it's just a question of which makes your code easier to read. One useful property of a for loop is that you can declare and initialise a control variable in the initial expression. This renders the control variable local to the loop, which is something you cannot achieve with a while loop. This has no effect on the resultant machine code, but by scoping variables within a for loop you automatically enlist the help of the compiler to eliminate bugs. It should be noted that the do-while loop is similar to a while loop, except that a do-while loop always executes its statements at least once, because the conditional expression is evaluated at the end of each iteration, rather than before each iteration as it is with a while loop. Again, a for loop can be used to achieve a do-while loop, however the do-while loop maps closely with the resultant machine code, and is generally much easier to read.


C program to print numbers 1 to n?

how do we use loops in c plus plus programing and what are basic differences between do,for and while loop


What are some examples of loops in C plus plus?

The following example demonstrates all 4 loop structures in C++. #include&lt;iostream&gt; int main() { int i; std::cout&lt;&lt;"For loop...\n"&lt;&lt;std::endl; for(i=0; i&lt;10; ++i) std::cout&lt;&lt;i; std::cout&lt;&lt;'\n'&lt;&lt;std::endl; std::cout&lt;&lt;"While loop...\n"&lt;&lt;std::endl; i=0; while(i&lt;10) std::cout&lt;&lt;i++; std::cout&lt;&lt;'\n'&lt;&lt;std::endl; std::cout&lt;&lt;"Do-while loop...\n"&lt;&lt;std::endl; i=0; do { std::cout&lt;&lt;i; }while( ++i&lt;10 ); std::cout&lt;&lt;'\n'&lt;&lt;std::endl; std::cout&lt;&lt;"Goto loop...\n"&lt;&lt;std::endl; i=0; again: std::cout&lt;&lt;i; if(++i&lt;10) goto again; std::cout&lt;&lt;'\n'&lt;&lt;std::endl; } Output: For loop... 0123456789 While loop... 0123456789 Do-while loop... 0123456789 Goto loop... 0123456789


How does C plus plus endeavor to represent the loop paradigm?

Iterative loops in C/C++ are represented by for(), while() and do...while() code blocks. Recursive loops are represented by functions calling themselves.