Infinite Loops and C++?

Started by
6 comments, last by Oluseyi 19 years, 3 months ago
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
Advertisement
THey are good as long as you have conditions to break out of the game (i.e. when game is over for games...)
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.
[size=2]aliak.net
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.
______________________________"Man is born free, and everywhere he is in chains" - J.J. Rousseau
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?
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.
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.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
#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;

This topic is closed to new replies.

Advertisement