Game Development Community

strange "for" statement.

by Chris "Dark" Evans · in Torque Game Engine · 03/07/2002 (6:10 pm) · 5 replies

In terrCollision.cc under TerrainBlock::castRayI there is a for statement:

for(;;)
   {
      F32 nextXInt = calcInterceptX(pStart.x, invDeltaX, blockX + (dx == 1));
      F32 nextYInt = calcInterceptY(pStart.y, invDeltaY, blockY + (dy == 1));
   //...

for(;;) = very strange.

What does that mean? I've never seen anything like that. What's the point of having a "for" if it doesn't really do anything?

I did a search through the code and found a bunch more like that. Can someone refresh my memory as to why one would use that?

#1
03/07/2002 (6:17 pm)
bleh, i don't have a clue
#2
03/07/2002 (6:25 pm)
Nothing strange about it at all, it's one way of implementing an infinite loop. Somewhere in the loop there will be code that jumps from it, somehow.

Old-time C programmers use for(;;) {...}

Converted Pascal programmers often get caught using while(1) {...} or while(TRUE) {...}

or

do {...} while (TRUE);

etc. etc.
#3
03/07/2002 (6:58 pm)
For a more in depth information on what it means, as you know a for statement has 3 parameters separated by a ;. Well what that's saying is that it doesn't have any parameters in it, which in effect creates an infinite loop.
#4
03/07/2002 (7:11 pm)
Thank you :)

That helped me fix the terrain raycast problem I was having.
#5
03/07/2002 (8:18 pm)
some people call for (;;) "forever"