Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to do loop statements in C++?
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HazySmoke)345




PostPosted: 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++...
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Mar 15, 2006 1:03 pm   Post subject: (No subject)

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




PostPosted: Wed Mar 15, 2006 1:55 pm   Post subject: (No 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
Justin_




PostPosted: Wed Mar 15, 2006 2:03 pm   Post subject: (No 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.
wtd




PostPosted: Wed Mar 15, 2006 2:08 pm   Post subject: (No 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.
Justin_




PostPosted: Wed Mar 15, 2006 3:31 pm   Post subject: (No 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?
wtd




PostPosted: Wed Mar 15, 2006 4:00 pm   Post subject: (No subject)

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




PostPosted: Wed Mar 15, 2006 4:09 pm   Post subject: (No subject)

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




PostPosted: Wed Mar 15, 2006 4:23 pm   Post subject: (No 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.
Justin_




PostPosted: Wed Mar 15, 2006 4:44 pm   Post subject: (No 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?
MysticVegeta




PostPosted: Sat Mar 18, 2006 5:57 pm   Post subject: (No 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.
El Cid




PostPosted: Sun Apr 30, 2006 10:07 pm   Post subject: (No 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!
wtd




PostPosted: Sun Apr 30, 2006 10:21 pm   Post subject: (No 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;
}
Sanjay




PostPosted: Tue May 02, 2006 7:39 pm   Post subject: (No 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?
Sanjay




PostPosted: Tue May 02, 2006 7:40 pm   Post subject: (No 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?
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: