• Advertisement
Sign in to follow this  

Infinite Loops and C++?

This topic is 4764 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

Hey, I was just thinking, and I'm now curious..Is an inifinite loop good or bad in C++? I know in this emulated "newbie language" that I used to program in (or attempted, at least), all the programs I created an infinite loop in would crash. But, I was curious about c++ and infinite loops, because I thought there was supposed to be a "game loop" in games, where you created an infinite loop to handle all the client-side stuff? Thanks for any help in advance, --Lenox

Share this post


Link to post
Share on other sites
Advertisement
THey are good as long as you have conditions to break out of the game (i.e. when game is over for games...)

Share this post


Link to post
Share on other sites
They can be good or bad depending on the situation. I've seen game loops that do use a "while(1)" statement as its entry point, so people do use infinite loops. But as si0n said, you have to have some kind of condition to coume out of them. For instance I use inifinite loops when reading files sometimes:


while(1)
{
c = getchar()
if( c == EndOfFile )
breakl
// do stuff with c;
}


In the above situation I know for a fact that the end of file will turn up sometimes, so Im sure that Ill be able to exit my loop. Of course when reading files you need to worry about other errors as well and you'll have to exit the loop because of them as well, but this is just an example.

Share this post


Link to post
Share on other sites
They are bad if you don't mean to create one, and expect the loop to exit. That's bad. But that's also a bug.

A lot of apps need infinite loops. Anything that is designed to operate indefinitely can use some form of infinite loop.

In some cases, I've had tight, indefinite loops cause problems, but that's more of a multiprocess type problem.

Some embedded software can have infinite loops that never exit. Think about a router or something that never "exits". It will just run forever with no exit condition, other than pull the plug.

Share this post


Link to post
Share on other sites
Ah, aiight, thanks ^_^. I created a program that in void main() I create an infinite loop like so:


#include <stdio.h>

void main()
{
int I = 1;
while(I = 1)
{
cout<<"30t,frozv,sfl,4FQOW$R<@QO#$<R!@PO<O@!#$<T!@";
}
}




So, like it shows, it spams the window. Now, I was curious about exiting out of this, how would I go about making it so that if the focus is no longer on the window, then exit?

Share this post


Link to post
Share on other sites
I don't know exactly how you would do that in a console window, but if you have a regular window, then you can read messages sent to that window. One of the messages might be WM_KILLFOCUS. then you can use the break; statement and it will terminate the loop.

Also
while(I = 1)
means that I will be set to 1, and then it will be tested for non-zero, so it will always loop no matter what you set I before. If you use == (double equal) then it is a comparison test, meaning if I is equal to 1.

Share this post


Link to post
Share on other sites
A truly infinite loop is bad because the only way to end it is to kill the process itself. I usually make the distinction between a loop that can't stop running (an infinite loop) versus a loop whose exit condition has yet to be reached (a continuous loop). If an exit condition is impossible to satisfy, an infinite loop may be masquerading as a continuous loop.

Share this post


Link to post
Share on other sites

#include <windows.h>
#include <stdio.h>

int main()
{
HWND hwnd = GetConsoleWindow();
MSG msg;

while(true)
{
if(PeekMessage(&msg, hwnd, WM_ACTIVATE, WM_ACTIVATE + 1, PM_NOREMOVE))
break;
printf("Whateverthehellyou'respamming!\n");
}

return 0;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement