Here is a snippet from an example on C++.com.
They use what looks like a do loop.
I tried putting it into their search, but nothing came back for "do".
As far as I can tell it forces the loop intially, otherwise the while statement would cause it drop through without it.
What is this the "do" statement intended for and when is it appropriate?
The fact is, I wrote a similar program without "do" . I just intialized the variable to 1.
code from C++:
// vector::push_back
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
int myint;
cout << "Please enter some integers (enter 0 to end):\n";
do {
cin >> myint;
myvector.push_back (myint);
} while (myint);
cout << "myvector stores " << (int) myvector.size() << " numbers.\n";
return 0;
}
Now, here is the way I did it without "do".
#include <iostream>
#include <vector>
using namespace std;
vector<int> vint;
int input;
int main()
{
cout << "Enter an integer (Enter 0 to exit) ";
while(input)
{
cin >> input;
vint.push_back(input);
}
vector<int>::iterator it;
for(it=vint.begin(); it < vint.end(); it++)
{
cout << *it << " ";
}
cout << endl;
return 0;
}