• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# What is wrong with this simple FOR loop?

## 8 posts in this topic

I''m having a rough time here. As you can see, I am an extreme newbie to C++ (although done some Basic and Pascal before), and the sybtax is a little different I''ve noticed. Even so, I don''t see what''s wrong with this code:
#pragma hdrstop
#include <condefs.h>
#include <conio.h>
#include <iostream.h>

//---------------------------------------------------------------------------
#pragma argsused

int main(int argc, char* argv[])
{

for (int i=200;i>300;i++)
{
cout << "# " << i << "." << endl;
}

getch();
return 0;
}

All I see when I compile and run this program is a blank screen, waiting for input from the keyboard (getch()). Any help would be greatly appreciated. Also, no errors or warnings are brought up by the compiler.
0

##### Share on other sites
LOL - replace the > with <.

---------------

I finally got it all together...
...and then forgot where I put it.
0

##### Share on other sites
I think this line is your problem

  for (int i=200;i>300;i++)

it should say

  for (int i=200;i<300;i++)

0

##### Share on other sites
your for loop should look like this:

for(int i=200; i < 300; i++)

then the rest of your code. Think of for statements as kind of a while loop that does the initialization, control, and iteration in one easy statement,even though when you get into the particulars of implementation the two..blah nevermind. Just think of the control statement in the for loop (the i < 300 part) as meaning the same as while(i < 300).
0

##### Share on other sites
all he needs to know is to remember that the > points to the smaller number.
0

##### Share on other sites
Doh, I figured it would be something stupid. Thank you everyone, and eotvos your explanation helped clear it up. Thanks!

(Oh BTW: How do you do that cool thing where the code shows up all nice and pretty? I just used

.. )
0

##### Share on other sites
Heh, it''s not that, it''s just that for some reason I was thinking with the logic "Repeat until i is greater than 300" instead of "While i is less than 300 do this".
0

##### Share on other sites
For the code stuff look here...

<a HREF="http://www.gamedev.net/community/forums/faq.asp">Forum FAQ</a>

0

Thanks.
0