answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Explain the difference between for and while loop and give the suitable examples in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is difference between define and cons Explain with suitable example?

Difference between define and cons is that, con is disadvantage, whereas define means to elaborate something fully.


How do you explain the difference between the vassals and serfs?

Explain the difference between the vassals and the serfs


What is the difference between standard sasria cover and the war exclusion?

Explain the difference between a standard sasria cover and the war exclusion under household buildings policy by making use for examples.


Explain the difference between young and mature mountains?

Explain the difference between young and mature mountains?


What is difference between explain and describe?

Describe is what it is and explain is why it is as it is


What is the difference between describe and explain?

Describe is what it is and explain is why it is as it is


What is the difference between Picasso's periods?

Click link below! You will see examples of paintings from the different periods. This will explain better than words.


Explain the difference between a cash and credit transaction for purchass and sales?

explain the difference between cash and credit transaction


Explain the difference between share of customer and customer equity.?

Explain the difference between share of customer and customer equity


Illustrate the difference between aromaticity and antiaromaticity with appropriate examples?

Illustrate the difference between aromaticity and antiaromaticity with appropriate examples?


What is the difference between a system and a subsystem?

explain the difference between systems and sub systems


Explain the difference betwwen the two types of feeding?

explain the difference between the two types of feeding?