Reputation: 287
I'm going through "C++ Primer 5th Edition" as recommended via this site. I'm stepping through For and While loops and have come to the conclusion that they're virtually the same. At least that's what I've gathered. I think someone explained it the best when they said "For loops are for when you know how many times you want to loop and while loops are for when you want to loop until a condition is met." But essentially, the compiler doesn't differentiate between the two and that they can be used interchangeably.
The book presented this code (which doesn't to even work the way the explained it):
<iostream>
int main()
{
int sum = 0, value = 0;
// read until end-of-file, calculating a running total of all values read
while (std::cin >> value) {
sum += value; // equivalent to sum = sum + value
std::cout << "Sum is: " << sum << std::endl;
return 0;
}
}
The attached exercise said to "Write your own version of a program that prints the sum of a set of integers read from cin." I'm assuming they want me to use the For loop. So I tried doing that. I am having a hard time though.
The book said "The For loop is made up of an initstatement, condition and an expression. In the while loop, "std::cin >> value" was my condition and there was no expression. So how am I supposed to use a for loop if all I have is an initstatement and a condition?
#include <iostream>
int main()
{
int sum = 0;
for (int value = 0; std::cin >> value; "EXPRESSION GOES HERE")
sum += value;
std::cout << "The sum is: " << sum << std::endl;
return system("pause");
}
Upvotes: 3
Views: 3277
Reputation: 2942
Sometimes the code just looks neater with one rather than the other. Consider two scenarios;
std::vector<double> myVector;
typedef std::vector<double>::iterator dblIter;
dblIter curr = myVector.begin();
dblIter end = myVector.end();
while(curr != end)
{
// do stuff
++curr;
}
Or;
std::vector<double> myVector;
typedef std::vector<double>::iterator dblIter;
for(dblIter curr = myVector.begin(); curr != myVector.end(); ++curr)
{
// do stuff
}
I happen to prefer option 2, despite comments that say "use for when you know the number of iterations". I guess it applies as a general rule.
Upvotes: 0
Reputation: 179
a while loop as a for
{
int i = 0;
while( i < limit){
//do stuff
i++;
}
}
Keep the scope operators to trash out the iterating variable when the loop is done.
Upvotes: 1
Reputation: 239311
Err, you can write any while
loop as a for
loop by replacing while(...)
with for(;...;)
...
for (;std::cin >> value;) {
}
Nothing about the code you posted suggests a for
loop though. Generally you'll use a for loop when you actually want to count something, or iterate over a collection. Your intent is just to loop until a condition is met, so that pretty much screams while
, not for
.
I guess if I had to write this "correctly" with a for loop, I might use something like this:
int sum = 0;
for (int i = 0; std::cin >> i; sum += i); // empty loop body
std::cout << sum << std::endl;
Though, I'd be more likely to use a while
loop which is more semantically correct.
Upvotes: 4
Reputation: 1258
The important thing to the answer is that you can leave every part of the for expression out, e.g.
for (;;) break;
Would be valid code.
Also I suggest putting a C++ book aside and going back to C in this case, as loop semantic basics are the same for both, and C is by far simpler.
Edit: Complete solution for your problem could be something like:
#include <iostream>
int main()
{
int sum = 0;
for (int value = 0; std::cin >> value; sum+= value);
// alternative version, a bit easier to read probably:
// for (int value = 0; std::cin >> value;) sum+= value;
std::cout << "The sum is: " << sum << std::endl;
return system("pause");
}
Upvotes: 1