Prev: is this NP-Hard?
Next: An M/M/n queuing model simulation with Object Pascal and my Thread Pool Engine...
From: James Kanze on 30 Apr 2010 21:21 On Apr 25, 8:02 am, Juha Nieminen <nos...(a)thanks.invalid> wrote: > In comp.lang.c++ red floyd <no.spam.h...(a)its.invalid> wrote: > > As a purely stylistic matter, my empty loops always look like: > > for ( begin_expr; test_expr; next_expr) > > /* do nothing */ ; > No, the proper way of writing an empty loop is: > for(begin_expr; test_expr; next_expr) {} > The empty block makes it explicitly clear that it's intended > to be empty, and not just a typo. (The empty block could also > be put in the next line, below the 'for', if so desired. A > matter of style.) I'd argue that it's more than just a question of style. What the loop controls is on a line (or lines) after the loop, and if it controls nothing, there should be a line which is clearly "nothing". Either: for (begin_expr; test_expr; next_expr) ; or for (begin_expr; test_expr; next_expr) { } (or whatever your coding guideline says with regards to the placement of the braces). -- James Kanze
From: James Kanze on 30 Apr 2010 21:26 On Apr 28, 8:26 am, Nick Keighley <nick_keighley_nos...(a)hotmail.com> wrote: > On 25 Apr, 08:02, Juha Nieminen <nos...(a)thanks.invalid> wrote: [...] > > The empty block makes it explicitly clear that it's intended to be empty, > > and not just a typo. (The empty block could also be put in the next line, > > below the 'for', if so desired. A matter of style.) > for (begin_expr; test_expr; next_expr) > ; > is my usual form. And I cam across this and was taken with it > for (begin_expr; test_expr; next_expr) > continue; > (the place I'd comtemplate a continue) I'll bet the programmer who invented that one was an ex-Fortran'er. -- James Kanze
From: Sjouke Burry on 30 Apr 2010 21:57 James Kanze wrote: > On Apr 28, 8:26 am, Nick Keighley <nick_keighley_nos...(a)hotmail.com> > wrote: >> On 25 Apr, 08:02, Juha Nieminen <nos...(a)thanks.invalid> wrote: > > [...] >>> The empty block makes it explicitly clear that it's intended to be empty, >>> and not just a typo. (The empty block could also be put in the next line, >>> below the 'for', if so desired. A matter of style.) > >> for (begin_expr; test_expr; next_expr) >> ; > >> is my usual form. And I cam across this and was taken with it > >> for (begin_expr; test_expr; next_expr) >> continue; > >> (the place I'd comtemplate a continue) > > I'll bet the programmer who invented that one was an > ex-Fortran'er. > > -- > James Kanze Fortran uses cycle to get to the end of a loop.
From: Moi on 1 May 2010 04:24 On Wed, 28 Apr 2010 05:18:41 +1000, Lie Ryan wrote: > On 04/25/10 16:19, red floyd wrote: >> On 4/24/2010 5:54 PM, Andrew Poelstra wrote: >> >>> The alternative here, using the loop structure, would be: >>> >>> for(i = 0; i< 10&& arr[i].state != READY; ++i) >>> ; >>> /* now use arr[i], or if i is 10, indicate failure. */ >>> >>> >> As a purely stylistic matter, my empty loops always look like: >> >> >> for ( begin_expr; test_expr; next_expr) >> /* do nothing */ ; >> >> That is, I always explicitly comment the empty loop so that it's clear >> that I wanted the loop to be empty. >> >> > Stylistically, I would put an empty { } to the side, containing a single > space: > > for ( begin_expr; test_expr; next_expr) { } > > instead of semicolons. Most brace-language programmer would > unconsciously filter out semicolons from the end of lines, while { } is > much more visible since it's more unusual to open a block only to close > it again. Personally I tend to use: for ( begin_expr; test_expr; next_expr) {;} , which immediately makes clear that the empty loop is intended. AvK
From: Phil Carmody on 1 May 2010 05:49 James Kanze <james.kanze(a)gmail.com> writes: > On Apr 25, 8:02 am, Juha Nieminen <nos...(a)thanks.invalid> wrote: >> In comp.lang.c++ red floyd <no.spam.h...(a)its.invalid> wrote: > >> > As a purely stylistic matter, my empty loops always look like: > >> > for ( begin_expr; test_expr; next_expr) >> > /* do nothing */ ; > >> No, the proper way of writing an empty loop is: > >> for(begin_expr; test_expr; next_expr) {} > >> The empty block makes it explicitly clear that it's intended >> to be empty, and not just a typo. (The empty block could also >> be put in the next line, below the 'for', if so desired. A >> matter of style.) > > I'd argue that it's more than just a question of style. What > the loop controls is on a line (or lines) after the loop, Bollocks. What the loop controls is the statement that follows it. (Or in the case of a do-while, the statement between the do and the while.) By the time the C parser is interpreting the loop construct, there's no such thing as a 'line'. You're addressing _nothing_ but style, stop pretending otherwise. However, thanks for making me peek into the documentation itself, I've discovered a horribly misleading sentence in the standard, so shall propose a correction. Phil -- I find the easiest thing to do is to k/f myself and just troll away -- David Melville on r.a.s.f1
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: is this NP-Hard? Next: An M/M/n queuing model simulation with Object Pascal and my Thread Pool Engine... |