What is wrong with these loops

I need to know what is wrong with each of the following loops, I am pretty sure they all have the same problem.

do
{
cout << "true" << endl;
}while(true);

while(true)
{
cout << "true" << endl;
}

for(;;;)
{
cout << "true" << endl;
}
Work out when each loop stops looping.
???
Okay, I'll go slower. When will the first loop stop?
I'll add to that.
What tells the first one that it must stop?
I don't know... endl?
endl will cause the output to go onto a new line; nothing to do with the loop. Time to read up on how loops work:

http://www.cplusplus.com/doc/tutorial/control/
closed account (zb0S216C)
A little confused are we, Mrankin?

std::endl is a stream manipulator. It has nothing to do with how the loop executes, only how the contents of the stream is printed.

A loop needs to stop sometime. In all three loops, you never actually specify when any of the loops should stop. There are two ways to stop a loop:

1) By using the break statement.
2) By testing against a condition.

This code segment demonstrates both:

1
2
3
4
5
while( true )
{
    if( /* Such and such is equal to false */ )
        break; // from the loop.
}


Wazzak
Thank you very much for the help.
And yes much more than a little confused. I pretty much don't know what I am doing, I took this course online, the book has nothing to do with C++ just problem solving, so it is no help, and I am having trouble making myself think in a way that suits programming. Sorry if I annoyed anyone by not knowing what is going on.
Last edited on
closed account (zb0S216C)
Why not have a browse through these books: http://www.freetechbooks.com/c-c-f3.html

If the book you're reading, as Americans like to say, sucks, obtain another book. And don't worry, nobody is annoyed, you're just confused as all; we've all been there.

Wazzak
Last edited on
Topic archived. No new replies allowed.