Computer Science Canada

How to do loop statements in C++?

Author:  HazySmoke)345 [ Wed Mar 15, 2006 12:53 pm ]
Post subject:  How to do loop statements in C++?

How to do loop statements in C++?

Either I'm really blind, or I just can't find it in the tutorial index. In VB, there are these three types of loop statements... While, Do, and For. I wonder if there are similar structures in C++...

Author:  wtd [ Wed Mar 15, 2006 1:03 pm ]
Post subject: 

http://merd.sourceforge.net/pixel/language-study/syntax-across-languages/CntrFlow.html#CntrFlowLoop

Author:  Justin_ [ Wed Mar 15, 2006 1:55 pm ]
Post subject: 

Here's a link that will actually help you. I think wtd missed the part where you said you wanted to know how to do them. http://cplus.about.com/od/beginnerctutorial/l/aa031602a.htm

Author:  Justin_ [ Wed Mar 15, 2006 2:03 pm ]
Post subject: 

By the way, all I did was type: "c++ loops" in a google search field to come up with that link. Next time try searching for an answer before asking.

Author:  wtd [ Wed Mar 15, 2006 2:08 pm ]
Post subject: 

If it's not possible to figure it out from the information provided by the site I linked to, then one would need a better grasp of more fundamental concepts anyway.

Keep in mind that when I post I assume people will learn by mastering simple concepts and building on them. This is the standard I hold people to. If declaration and initialization or expressions need to be explained, then I can only conclude that the student in question is not ready to learn about loops.

Author:  Justin_ [ Wed Mar 15, 2006 3:31 pm ]
Post subject: 

wtd honestly, you're link had a bunch of dissembodied information mostly not even pertaining to c++. It was ugly and had no explaination and poor examples.

The only thing I can say to you is "keep your audience in mind". I don't expect you to concede since you never do so what do you say we allow HazySmoke to say which link he found more useful?

Author:  wtd [ Wed Mar 15, 2006 4:00 pm ]
Post subject: 

The comparative usefulness of things is often only apparent to us long after the fact. Smile

Author:  Justin_ [ Wed Mar 15, 2006 4:09 pm ]
Post subject: 

lol, so true. But, you're just saying that cause you know he'll side with me.

Author:  wtd [ Wed Mar 15, 2006 4:23 pm ]
Post subject: 

I'm saying it because we have lots of people here ask questions like this and providing an easy answer often seems like the most useful thing, but it often does them a disservice.

Author:  Justin_ [ Wed Mar 15, 2006 4:44 pm ]
Post subject: 

You're going to call me a troll or some wierd thing for saying this, but remember when you told me java passes objects by reference? Isn't that like an easy answer?

So give me the easy answer, give him the wacked out complicated one?

Author:  MysticVegeta [ Sat Mar 18, 2006 5:57 pm ]
Post subject: 

I thought wtd's link was pretty good because it had a comparison between many languages loops (including VB which the person is used to program in) So I guess it was fairly easy for him to compare the loop structures.

Author:  El Cid [ Sun Apr 30, 2006 10:07 pm ]
Post subject: 

Let me summarize the information here.

for loop: usually used when you know how many times you want to run the loop before you start; printing repeated characters comes to mind.
Example:

for(int i=9; i > 0; i--)
cout << *;

what happens:
you initialize an integer i to 9. You check to see if i>0. As long as that's true, cout '*' and decrement i by 1 at the end of the loop; check to see if i > 0 is still true, etc...; exits loop when i>0 becomes false.


while loop: usually used when you want to stop when a certain conditions is met; very useful for reading from files.

Example:

int num_words = 0;
string word;
cin >> word;

while(word != "end")
{
i++;
cin >> word;
}
cout << i << "words were inputed."

what happens:
You continuously read in words from the standard input device until you reach the word "end", at which point you exit the loop and display the number of words read. Note that if we start out with the word "end", the statement inside the loop would never execute.


[b]do[\b] loop: same as while loop, except that it executes at least once

Example:
string accident;
int i=0;
do
{
cout << "Take a step" << endl;
cin >> accident;
i++;
}
while(accident != "fall");
cout << "Took " << i << " steps";

what happens:
Basically, we have a while loop; however, we don't check for exit condition until the statement within the loop has executed at least once.


Hope this helps!

Author:  wtd [ Sun Apr 30, 2006 10:21 pm ]
Post subject: 

Of course, for what it's worth, loops in C++ are often better expressed as uses of STL algorithms. Wink

code:
#include <iostream>

using namespace std;

int main()
{
   int foo[] = {1, 2, 3, 4, 5};

   for (int i = 0; i < 5; ++i)
   {
      cout << foo[i] << endl;
   }

   return 0;
}


vs.

code:
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
   int foo[] = {1, 2, 3, 4, 5};

   copy(foo, foo + 5, ostream_iterator<int>(cout, "\n"));

   return 0;
}

Author:  Sanjay [ Tue May 02, 2006 7:39 pm ]
Post subject: 

Why are people posting how to do this? He has had two perfectly adequate links.
The best lesson you can teach him is how to find and extract information. That way when he gets the error "Linker error: Undefined reference to Function() at Line 12" he isn't back asking here again and he just punches it into Google and we are done with it.

code:
   int foo[] = {1, 2, 3, 4, 5};

   copy(foo, foo + 5, ostream_iterator<int>(cout, "\n"));

That scares me. Is it an actual For loop or an imitation?

Author:  Sanjay [ Tue May 02, 2006 7:40 pm ]
Post subject: 

Why are people posting how to do this? He has had two perfectly adequate links.
The best lesson you can teach him is how to find and extract information. That way when he gets the error "Linker error: Undefined reference to Function() at Line 12" he isn't back asking here again and he just punches it into Google and we are done with it.

code:
   int foo[] = {1, 2, 3, 4, 5};

   copy(foo, foo + 5, ostream_iterator<int>(cout, "\n"));

That scares me. Is it an actual For loop or an imitation?

Author:  wtd [ Tue May 02, 2006 8:10 pm ]
Post subject: 

Sanjay wrote:
code:
   int foo[] = {1, 2, 3, 4, 5};

   copy(foo, foo + 5, ostream_iterator<int>(cout, "\n"));

That scares me. Is it an actual For loop or an imitation?


The std::copy function is in the "algorithm" header. To it we pass two iterators. The first iterator is the beginning of the sequence, and the second is the end of the sequence.

Because iterators in C++ use the same operators as array access, we can provide the name of the array (which is actually a pointer to the beginning of the array in memory) as the initial iterator.

Since foo is 5 elements long, "foo + 5" indicates the end of the array. COnsider the following equivalences to better understand this:

code:
*foo == *(foo + 0) == foo[0]
*(foo + 5) == foo[5]


For an array of length 5, the last index is 4. Therefore if we have an index of 5, we are past the end of the array.

The third argument to the std::copy function is the output iterator. For this we use the ostream_iterator class from the "iterator" header. To its constructor we pass the ostream object we want to copy to. In this case, std::cout. We also pass the separator. This is a string that gets output after each element.

Make more sense now? Smile

Author:  Sanjay [ Tue May 02, 2006 8:46 pm ]
Post subject: 

Yep, thanks mate.

This is the only real reason I would consider .NET: If I was working with a group of programmers who don't understand:
for(i=1;i<5,i++){cout << i;}

It reads like normal English Very Happy (Actually, after a night of programming till 5 in the morning, I actually said "cout hello, Alex" to a friend. Maybe reading like English isn't such a good idea..,)

Author:  wtd [ Tue May 02, 2006 9:03 pm ]
Post subject: 

Sanjay wrote:
Yep, thanks mate.

This is the only real reason I would consider .NET: If I was working with a group of programmers who don't understand:
for(i=1;i<5,i++){cout << i;}


If you're working with a group of programmers who cannot understand such simple code (once you fix your typo Wink ) then switching to another language/environment will not save you.

Author:  wtd [ Tue May 02, 2006 9:07 pm ]
Post subject: 

Another example:

Let's read a bunch of integers into a vector.

code:
vector<int> numbers;

copy(istream_iterator<int>(cin), istream_iterator<int>(),
     back_inserter(numbers));


: