I could give you example of HOW to use each of the two constructs, but the nice people before me have already done that, Instead, I'll try to explain to you WHY/WHEN you could use these constructs.
First, the above examples might make you think: "Apparently, anything I can do using 'while', I can also do using 'for', and vice versa. Seeing that computer languages are typically constructed by computer scientists, why did they not choose to keep the language as small (and by that: easy to learn! See source list.) as possible?" That's a perfectly legitimate point, and I must admit I'm not entirely sure why this is done. In recent languages it's probably because most programmers have been raised with the ability to use both constructs, and they might get annoyed if their 'preferred way of doing things' was removed from a new language --so to please them, language designers leave it in.
But why did the first language designers put two different constructs to do the same thing in their programming languages? Well, if I were to venture a guess, I'd say: internal documentation. You see, any GOOD programmer will take into account that his/her code will at some point be read and maintained by another programmer. And a good programmer will try to convey as much of his/her intentions as possible to this next programmer. And that brings me to the answer to the question: when should you use each construct?
My advice is: use 'for' when you know in advance exactly how often you want to repeat the contents of the loop, and use 'while' when you don't know that. For example: if you have an array of integers and you want to calculate the sum of the values in that array (the size of which you 'of course' know when you start!), use 'for'. If the condition that terminates the loop is dependent on a calculation that is done inside the loop, use 'while'.
To make this explanation more or less complete: C++ also supports the do-while loop. It's roughly the same as the while loop, only the body of the loop is always executed at least once. So you can use the do-while loop if the body of the loop not only CONTAINS the calculation of the condition that terminates the loop, but also ASSIGNS the proper values to the variables that are used in that calculation. In other words, if you find yourself writing code like this:
some_var = some_calculation();
while(some_var == true)
{
some_var = some_calculation();
}
--and note that some_var is assigned a value the same way, twice!!--, you should consider rewriting your code like this:
do
{
some_var = some_calculation();
} while(some_var == true);
Finally, some languages (such as C#, but not C++) also support the 'foreach' loop. It looks like this:
foreach(value in some_set_of_values)
{
do_something_to(value);
}
When you use a construct like this, you're basically saying: "I want to perform a calculation on each value in some_set_of_values, but the ORDER in which I address those values does not affect the outcome." Sometimes, knowing this makes the job a bit easier for the person who has to maintain your code.
I hope this explanation helps. Good luck learning C++!
2007-01-27 22:27:14
·
answer #1
·
answered by Anonymous
·
0⤊
0⤋
#include
using namespace std;
int main(){
for(int i=0;i=10;++i)
{
count<
}
int j=0;
while(j<10)
{
cout<
j++;
}
}
2007-01-28 04:56:53
·
answer #2
·
answered by legolas 1
·
1⤊
0⤋
to print
*
**
***
****
the code using for is:
for(int i=0;i<5;i++)
{cout<<"\n";
for(int j=0;j<=i;j++)
cout<<"*";
}
the code using while:
int i=0,,j=0;
while(i<5)
{j=0;
cout<<"\n";
while(j<=i)
{cout<<"*";
++j;}
++i;
}
2007-01-28 04:57:32
·
answer #3
·
answered by Anonymous
·
0⤊
1⤋
For loop:
for (int i=0; i<100; i++) {
//...do your stuff
//...do your stuff
} //end loop
While loop:
int i=0;
while (i<100) {
//...do your stuff
//...do your stuff
i++;
} //end loop
2007-01-28 04:34:46
·
answer #4
·
answered by Metlin 3
·
0⤊
0⤋