You are not logged in.
I have the following (loop) construction in some online C source code:
for (;;) {
/* here follows some random code */
}
I didn't, however, find any particular code within the sequence embraced by the for, that actually refers to this strange loop.
If you wanna have a look at the complete source code, please refer to http://omlc.ogi.edu/software/mc/tiny_mc.c
EDIT: Oopsie! Typing the URL I forgot what I wanted to ask! So, what does this 'for' do? What does that (;;) evaluate?
Last edited by Stalafin (2008-06-05 14:36:54)
Offline
This is exactly the same as :
while (true) {
}
ie : infinit loop
Offline
As chicha pointed out, this loop just keeps going until it hit a break statement.
Offline
Ah, okay... thanks a lot.
So, why would one use that whing instead of a simple while loop? Wouldnt a while be more efficient?
Offline
Technically it does not make a difference using the while(true) or for(;; ) : the compiler should be smart enough and make those loops as fast as each other...
Practically both loops are ugly : you should always make sure that your loop end at one time. You cannot warranty that the algorithm inside the loop will reach a break instruction at one time, in particular in complex loop.
Sometimes some developers prefer to write a short line of ugly code instead of 3 lines of safe code ...
However I hardly recommand not to use such a loop (and avoid 'while' loop as much as possible for the same reason).
Make sure your loop will always finish in any situation.
I hope it helped !
Offline
I'm not that great with programming yet but isn't that a fork bomb?
Offline
I'm not that great with programming yet but isn't that a fork bomb?
infinite loop != forkbomb. Infinite loop spins a single process forever, while a fork bomb spawns exponentially many processes. Very different things.
There's really nothing wrong with a while(true) loop - you just need to do some verification that the loop will eventually exit. While tougher than a "standard" for loop, it's doable.
Offline
for (;;) { /* here follows some random code */ }
i dunno about you guys but i've always referred to these loops as "for-ever" loops. i personally think it reads better than while(true) when reading source. it's all personal preference i suppose.
archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson
Offline
@rson451: My CS instructors always used the term "for-ever" loops whenever they encountered one, so you wouldn't be the only one to use that terminology.
The water never asked for a channel, and the channel never asked for water.
Offline
Thanks Cerebral for the clarification.
Offline
Practically both loops are ugly : you should always make sure that your loop end at one time. You cannot warranty that the algorithm inside the loop will reach a break instruction at one time, in particular in complex loop.
You can generalize that statement to any C loops.
You can easily imagine a standard for loop "for(int i = 0; i < n; i++)" where i is modified in complex ways inside the loop.
Same for a while loop.
pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))
Offline
Well, at least in this example, it is a horrible way to do it. here is the complete loop:
for(;;) {
xi1=2.0*rand()/RAND_MAX - 1.0;
xi2=2.0*rand()/RAND_MAX - 1.0;
if ((t=xi1*xi1+xi2*xi2)<=1) break;
}
It should be, by all reasonable standards at least:
do{
xi1=2.0*rand()/RAND_MAX - 1.0;
xi2=2.0*rand()/RAND_MAX - 1.0;
} while ((t=xi1*xi1+xi2*xi2)>1);
(re-organizing the algorithm could lead to a nicer while loop i'm sure).
I feel loops with a termination condition like this should always clearly state that condition 'out in the open', instead of hiding it within the body of the loop.
Offline
I feel loops with a termination condition like this should always clearly state that condition 'out in the open', instead of hiding it within the body of the loop.
this is true. i was always brought up to hate breaks.
Offline
Well, I don't mean to say that one should never use breaks. Some algorithms are too complex to realistically forbid the use of breaks. (multiple return-s are another story, however )
Offline