answersLogoWhite

0

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

13y ago

What else can I help you with?

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


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'


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

Add the missing parts.


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


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


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.


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.

Trending Questions
What is direct indexing in milling? Why is it important to evaluate possible solutions during the engineering design process? What is ampere rating of a fuse used to protect a 5.5 mm squared conductor? What is a machine that is made of two simple machines? How do you turn the heat off? Why is it important for us to remove the metal jewelry matches ring before working on electric circuit? If you are measuring the voltage of a three phase power system you take only two phase in account then why there is a need for third phase? How does ANV electric company phase controller type APR-3 works? How do you pass enum object as argument to function in c? Are fixed end beams an indeterminate structure? Who built the Itsukushima Torii gate? Where do you put a pump in an old large cement mermaid statue fountain? How does Prim's algorithm differ from Kruskal's and Dijkstra's algorithms? What will happen to frequency of current when voltage is changed using a step up or step down transformer? Have a step up tranformer. 110 volts to 220 volts. Power 550VA. Can I connect the secondary to 220 volts and get 110 volts form the Primary and use this 110 volts to run an equipment that draws 2 amps? How do I know if my waterproofing contractor does good work? Assembly language program in 8086 to check wether given string is palindrome or not? Difference between static and dynamic characteristics of transducers? WHY CAN’T WE THROW THE ELECTRONIC DEVICES IN REGULAR DUSTBINS? How much water does a steam engine use?