answersLogoWhite

0


Best Answer

Although both can achieve very similar loops, the one you use for a specific loop usually comes down to whichever seems more intuitive to you. In terms of performance, one method may be slightly more efficient than the other, however there are no general rules that can be applied; you must test each case by comparing the machine code or by physically timing the loops with a high performance event timer (HPET). In my experience, the performance difference is so negligible it is not worth the effort. Background activity has a far greater impact on performance.

Describing the differences between every possible loop you might create would take me a lifetime, but there are a couple of subtle differences that are worth pointing out.

Infinite Loops

for(;;){

} // infinite for loops do not require a condition.

while( true ){

} // infinite while loops MUST have a condition.

To break out of an infinite loop you must test for one or more conditions within the loop itself, and call break to exit the loop when a condition is met. You can also call continue to start the next iteration before reaching the end of the current iteration.

Stepping Loops

When stepping through a series of values, the for() loop is usually the better choice, especially if the step must occur AFTER each iteration. Consider the following similar loops:

for( int x=0; x<10; ++x ){

// if we place continue here, ++x will execute before the next iteration begins.

}

int x=0;

while( x<10 ){

// if we use a continue here, ++x will not execute. We could be here forever!

++x;

}

Another point to bear in mind here is that with the for()loop, x will fall from scope when the loop finishes. If x must remain in scope, initialise x before entering the for() loop. With the while() loop, x is still in scope when the loop finishes so if x must fall from scope, do not use the while() loop.

do...while Loops

The do...while loop is quite different from for()and while() loops, so it is worth mentioning here. Both for() and while() loops test a condition BEFORE the loop begins execution. If the condition is false, the loop may not execute at all. But a do...while loop is guaranteed to enter the loop AT LEAST once, because the condition is tested AFTER the loop begins, at the end of the loop just before it begins to iterate.

int x = 0;

do{

// Whatever you place here will execute at least once.

// If we place continue here, execution jumps to the while(condition) first.

}while(++x != 10)

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is different between while and for loop in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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


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


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&gt;0) { //body part } here when i will &gt;0 then it will check it body part and execute it and display result.


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 is the relationship between database and c plus plus?

There is none. While you can access databases from C++, the two concepts are fundamentally different.


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


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


What is an example of a do-while loop in C plus plus?

A do-while loop is best suited to loops that must execute at least once: srand(( unsigned)time(NULL)); int i; do { // pick a random number between 1 and 10 i = 1 + (int) (10.0*(rand()/(RAND_MAX+1.0))); std::cout &lt;&lt; i &lt;&lt; std::endl; } while( i!=10 ); In this example, the loop executes at least once and prints a random number. While the random number is less than 10, the loop reiterates, selecting another random number until the number 10 is selected. The exact same effect can be achieved with for and while loops but the code is much less readable as a result: srand(( unsigned)time(NULL)); int i=0; while( i!=10 ) { // pick a random number between 1 and 10 i = 1 + (int) (10.0*(rand()/(RAND_MAX+1.0))); std::cout &lt;&lt; i &lt;&lt; std::endl; } In the above example, we must initialise i before entering the loop for the first time. srand(( unsigned)time(NULL)); for(;;) { // pick a random number between 1 and 10 int i = 1 + (int) (10.0*(rand()/(RAND_MAX+1.0))); std::cout &lt;&lt; i &lt;&lt; std::endl; if( i==10 ) break; } In the above example we use an infinite loop and instantiate i within the loop. We can also replace for(;;) with while(1) to achieve the same result. Ultimately the do-while loop makes the most sense in this case. The infinite loop versions would only make sense if you wanted i to fall from scope at the end of the loop (which can't be done with a do-while loop since the conditional expression is outside of the loop). But the while loop makes the least sense because i must be initialised before entering the loop for the first time.