go away spammer

Author Topic: for( ; ; )  (Read 3169 times)

0 Members and 1 Guest are viewing this topic.

Offline jsmokerTopic starter

  • Full Member
  • ***
  • Posts: 118
  • Helpful? 2
for( ; ; )
« on: November 21, 2007, 09:14:19 AM »
I found this in some code I'm trying to use (in C++).  Does anyone know what it means?

for( ; ; )

I know what a "for" does in cases like (int i = 0; i < 20; i++), but what's with the "( ; ; )". 

-JSmoker


Offline JonHylands

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 562
  • Helpful? 3
  • Robot Builder/ Software Developer
    • Jon's Place
Re: for( ; ; )
« Reply #1 on: November 21, 2007, 10:10:07 AM »
In C, a for loop looks like this:

for (index = 0; index < 10; index++)

That means to loop 10 times, with index incrementing from 0 - 9.

If you leave the three fields inside the parens blank, it basically means to loop forever...

- Jon

Offline Fredrik Andersson

  • Robot Overlord
  • ****
  • Posts: 216
  • Helpful? 0
Re: for( ; ; )
« Reply #2 on: November 21, 2007, 10:15:55 AM »
Well, it does look a little weird doesn't it? I've been programming for a while and have never seen it.

The common method of making an infinite loop is something like this:

while (true) {
   //Code and so on...

Basically it means the expressions are empty, so that means they result in true (which makes the for loop infinitely), right?
Current project: Pirrh - Portable Intelligent Round Rolling Hexapod

Offline jsmokerTopic starter

  • Full Member
  • ***
  • Posts: 118
  • Helpful? 2
Re: for( ; ; )
« Reply #3 on: November 21, 2007, 11:47:22 AM »
ok, I got it, the author was just using it as a variation of a while loop:

instead of:
Code: [Select]
int x = 1;
while(x)
{
    if(case)
        x= 0;
}

he did:
Code: [Select]
int x = 1;
for( ; ; )
{
    if(case)
        break;
}

Offline fr4ncium

  • Jr. Member
  • **
  • Posts: 36
  • Helpful? 0
Re: for( ; ; )
« Reply #4 on: November 21, 2007, 05:37:23 PM »
Yeah, the reason you didn't recognize that straight off is because no recently created programming language allows for it.  Thank goodness.

http://www.xkcd.com/292/

paulstreats

  • Guest
Re: for( ; ; )
« Reply #5 on: November 23, 2007, 10:41:19 AM »
are there any advantages to it? such as is it easier to break out of without taking up a variable?

Offline HDL_CinC_Dragon

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: for( ; ; )
« Reply #6 on: November 23, 2007, 12:55:07 PM »
That method of coding is a nightmare to any experienced coder. Even a "while(true);" makes SOME (not all) programmers cringe. In my experience, readings, and observation ive found that the best thing to do is to create a simple global integer or boolean and set it to 'true' or '1' and have a while loop have that variable as its condition. Offers more control and is a little more stable... That might just be paranoia and this programming book I recently read talking :P
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline Fredrik Andersson

  • Robot Overlord
  • ****
  • Posts: 216
  • Helpful? 0
Re: for( ; ; )
« Reply #7 on: November 24, 2007, 04:02:36 PM »
That method of coding is a nightmare to any experienced coder. Even a "while(true);" makes SOME (not all) programmers cringe. In my experience, readings, and observation ive found that the best thing to do is to create a simple global integer or boolean and set it to 'true' or '1' and have a while loop have that variable as its condition. Offers more control and is a little more stable... That might just be paranoia and this programming book I recently read talking :P

Yeah, i agree with that. I often use a boolean variable called quit, and set it false and have the loop look like this: "while(!quit)". Whenever i need to jump out of that loop i just set quit to true. Thats feels like a pretty neat solution for me. Design is pretty important in programming, especially if you are studying programming and the teacher need to see how the program works...
« Last Edit: November 25, 2007, 03:11:23 AM by Fredrik Andersson »
Current project: Pirrh - Portable Intelligent Round Rolling Hexapod

 


Get Your Ad Here