Archived

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

This topic is 5695 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

for (; ; ) { }

It's basically the same as "while (true) {}", but this way you avoid one futile comparison.


theNestruo

Syntax error in 2410
Ok

[edited by - theNestruo on May 4, 2002 1:55:01 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by theNestruo
It''s basically the same as "while (true) {}", but this way you avoid one futile comparison.

And replace it with another. Brilliant.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
And replace it with another. Brilliant.



No, you don''t, actually.

for(;, even in debug builds with popular comparisons, is implemented as a straight jump. It doesn''t have *anything* to compare to, so it doesn''t bother with any comparison. while(true) will laboriously compare true to true every iteration. Optmized builds will typically remove the comparison, but for(; -- by virtue of having nothing to compare -- won''t make it an issue. If you said for(;true, perhaps it would put the test back in, but I''m not sure that anyone would bother.

Share this post


Link to post
Share on other sites
All a bit of a moot point since any half-way decent compiler will produce the same code, and more to the point, you wouldnt want to do either of them anyway.

Or would you...? Maybe theres a couple of things you could do in certain situations with ''break'' to exit the loop but not exactly clear coding i''ll bet.

Share this post


Link to post
Share on other sites
You know, after posting that I thought about the possibility that for( ; ; ) would evaluate to a straight jump, but decided to leave my comments in and take the flak.

Thanks for clearing that up.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
hi

thx but the methods with while(true) or for(; i know already

but it doesn´t work in my ogl program

when i compile i get an acces violation but i don´t know why


ok more details:

i have 4 forms with ogl windows and i need in every form a main loop because at the moment i have only timers for testing ( i know that is shit but i didn´t want to use the app idle time)

thx
--=[[TheMatrixXXX]]=--

Share this post


Link to post
Share on other sites
It''s a strange question to begin with. "Can anyone tell me how to create a main loop" ...? A main loop is no different from any other loop. It''s a loop. You iterate things. As for how to construct it, you just have to figure out what you want to iterate (respond to input, execute AI and game physics, update graphics and so forth) and put it all in there. (As for why you would bother to specify which compiler you use, I have no idea ...)

Share this post


Link to post
Share on other sites
hi all

i used the loops you all told me but every time the program hangs.
can you tell me why?
and i have the problem that i need this kind of loop in 4 windows to render in ogl


thx
--=[[TheMatrixXXX]]=--

Share this post


Link to post
Share on other sites
Okay, hint: you can''t have 4 main loops. A main loop is an infinite loop: you enter it without the plan of leaving it (well, okay, you might leave it at at some point off course).

I think what you should do, is create per window a function that simply checks&executes all the commands for that window, then create a *single* main loop in your code that calls the first-window-function, then the second-window-function... ... fourth-window-function, and then loops again.

-Maarten Leeuwrik, www.sherman3d.com


Follower of the NLS
(New Lounge Standards)

Share this post


Link to post
Share on other sites
If this isn''t some kind of prank question, there''s a generic windows app sample all over the www, and in the MSDN.

There''s a simple message handling loop in there, and you can investigate the PeekMessage function so that your app doesn''t have to wait for a message before it runs an iteration of its main loop.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites