You are not logged in.

#1 2008-06-05 14:35:32

Stalafin
Member
From: Berlin, Germany
Registered: 2007-10-26
Posts: 617

C programming language - syntax question

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! roll So, what does this 'for' do? What does that (;;) evaluate?

Last edited by Stalafin (2008-06-05 14:36:54)

Offline

#2 2008-06-05 14:38:22

chicha
Member
From: France
Registered: 2007-04-20
Posts: 271

Re: C programming language - syntax question

This is exactly the same as :

while (true) {
}

ie : infinit loop

Offline

#3 2008-06-05 14:40:35

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,365
Website

Re: C programming language - syntax question

As chicha pointed out, this loop just keeps going until it hit a break statement.

Offline

#4 2008-06-05 14:43:39

Stalafin
Member
From: Berlin, Germany
Registered: 2007-10-26
Posts: 617

Re: C programming language - syntax question

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

#5 2008-06-05 14:52:58

chicha
Member
From: France
Registered: 2007-04-20
Posts: 271

Re: C programming language - syntax question

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

#6 2008-06-05 18:44:57

dr.cranium
Member
Registered: 2008-01-25
Posts: 65

Re: C programming language - syntax question

I'm not that great with programming yet but isn't that a fork bomb?

Offline

#7 2008-06-05 18:50:25

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: C programming language - syntax question

dr.cranium wrote:

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

#8 2008-06-05 19:49:49

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: C programming language - syntax question

Stalafin wrote:
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

#9 2008-06-05 22:54:29

xaw
Member
From: Chapel Hill
Registered: 2007-08-09
Posts: 177

Re: C programming language - syntax question

@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. smile


The water never asked for a channel, and the channel never asked for water.

Offline

#10 2008-06-06 03:57:32

dr.cranium
Member
Registered: 2008-01-25
Posts: 65

Re: C programming language - syntax question

Thanks Cerebral for the clarification.

Offline

#11 2008-06-09 11:41:19

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: C programming language - syntax question

chicha wrote:

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

#12 2008-06-09 18:26:31

underpenguin
Member
Registered: 2007-02-01
Posts: 116

Re: C programming language - syntax question

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

#13 2008-06-09 19:40:31

melrom
Member
From: Bayville, NJ / Medford, MA
Registered: 2008-06-06
Posts: 9

Re: C programming language - syntax question

underpenguin wrote:

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. big_smile

Offline

#14 2008-06-09 20:33:58

underpenguin
Member
Registered: 2007-02-01
Posts: 116

Re: C programming language - syntax question

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 smile )

Offline

Board footer

Powered by FluxBB