From: Dave Harris on 30 Apr 2010 16:56 daniel_t(a)earthlink.net (Daniel T.) wrote (abridged): > The extra complexity is already obvious in the fact that three for > loops are required to iterate through one container in the first > place. That's not "extra"; it was already there. And really it's not that complex, since it's just iterating. > The conditions in each of the for loops isn't much of a > problem at all in comparison. It makes each loop a bit more complex. Say, 30%? for(int xInd = 0; xInd < datasize; ++xInd) versus: for(int xInd = 0; pResult != nullptr && xInd < datasize; ++xInd) In addition, it has this thing going on where although each loop is different, each has a "pResult != nullptr" fragment embedded in it which is the same. A person reading the code has to spot that similarity in the middle of all the differences. Then they have to decode what it means, by tracking the value of pResult as it changes over the whole function. It's not 10 times as complex, perhaps not even twice as complex, but at least 50% more complex. Which I'd say was significant. -- Dave Harris, Nottingham, UK.
From: robin on 30 Apr 2010 20:59 "James Van Buskirk" <not_valid(a)comcast.net> wrote in message news:hr1q1g$h5v$1(a)news.eternal-september.org... | "Alexei A. Frounze" <alexfrunews(a)gmail.com> wrote in message | news:aab23e01-b4d4-44c4-a136-a619da00a3f7(a)m24g2000prc.googlegroups.com... | | > I wish there were distinct breaks for loops and switches as well as | > multi-level breaks for nested loops. goto's at times are the least | > evil since they don't require you to implement extra variables to | > break out of nested loops and they help to simplify function cleanup | > code (where resources are being freed on various conditions). | | Amusing that Fortran has a mechanism to break out of nested loops | and permits one to use internal subroutines for function cleanup, | not to mention no fall-through in SELECT CASE constructs. Thank goodness. | These uses | of goto are simply artifacts of the limitations of C-style languages.
From: James Kanze on 30 Apr 2010 21:16 On Apr 25, 1:54 am, Andrew Poelstra <apoels...(a)localhost.localdomain> wrote: > On 2010-04-24, James Kanze <james.ka...(a)gmail.com> wrote: > > On Apr 24, 1:20 pm, "Leigh Johnston" <le...(a)i42.co.uk> wrote: > >> "spinoza1111" <spinoza1...(a)yahoo.com> wrote in message > >>news:051fa27d-5cf8-4f7e-94c8-e9e5b26566b7(a)g34g2000pro.googlegroups.com... [...] > > But when do you have to break out of a nested loop? (And of > > course, the only valid use of break in C++ is to end a case in a > > switch. Otherwise, it's just a goto in disguise.) > This is just as true in C, according to some people (myself > included). The only exception I can think of is when you are > looping only to set the loop index to something. > ie, > for(i = 0; i < 10; ++i) > if(arr[i].state = READY) > break; > /* now use arr[i], or if i is 10, indicate failure. */ > 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. */ > To me, the latter option looks horrific, plus if you swap the > operands of &&, you get a very hard-to-spot overrun. So don't swap them:-). Seriously, the "standard" idiom is: i = 0; while (i < 10 && arr[i].state != READY) ++ i; This idiom was developped in languages where for was a lot less flexible, but in this particular case, I don't see any real reason to switch to a for. -- James Kanze
From: James Kanze on 30 Apr 2010 21:31 On Apr 25, 12:17 pm, Phil Carmody <thefatphil_demun...(a)yahoo.co.uk> wrote: > James Kanze <james.ka...(a)gmail.com> writes: > > On Apr 24, 1:20 pm, "Leigh Johnston" <le...(a)i42.co.uk> wrote: > >> "spinoza1111" <spinoza1...(a)yahoo.com> wrote in message [...] > >> Because break only breaks out of the innermost loop, using > >> goto to break out of nested loops is one of the few sensible > >> uses of goto. > > But when do you have to break out of a nested loop? > For the same reason you want to break out of a single loop. > Do you have issues with people breaking out of single loops? I've never really seen a case where it was appropriate (if by "breaking out", you mean using the break statement to obfuscate the code by hiding the actual program flow). > > (And of course, the only valid use of break in C++ is to end > > a case in a switch. Otherwise, it's just a goto in > > disguise.) > What do you mean by 'otherwise' - even that's just a goto in > disguise? Yes, because switch is fairly broken. On the other hand, the language doesn't provide anything better. One of the most structured programs I ever saw was full of goto's. Because it was written in Fortran IV, and the language didn't provide any of the necessary structures natively. In the case of C/C++, about the only useful flow control structure that isn't provided natively is a true switch or case statement, so we have to use the one provided. -- James Kanze
From: Richard Heathfield on 2 May 2010 03:13
Juha Nieminen wrote: > In comp.lang.c++ Richard Heathfield <rjh(a)see.sig.invalid> wrote: >> If the data are sorted, bsearch. >> >> If not, why not? >> >> In any case, your loop condition expressions do not correctly describe >> the conditions under which the loop will terminate. Please rewrite your >> example so that they do, and then by all means ask me again if you wish. > > No offense, but it always amuses me how some C programmers try to > wiggle themselves out of actually giving honest answers when they are > presented with problematic situations related to their coding style, C, > or both. Your paragraph, above, is a classic example. <snip> > And I have no idea what you mean by "your loop condition expressions do > not correctly describe the conditions under which the loop will terminate". That's the problem. > Of course they do. No, they don't. > Is this just another attempt at wiggling yourself out > of actually giving the answer? No, it's an attempt at wiggling *your*self out of fixing your code. > Is it *really* that hard to simply say "yes, in this particular example > the 'return' is indeed the simplest way"? Does it hurt your pride or > something? What has pride got to do with it? It's common sense, that's all. The simplest way (and one of the fastest) to get from Tower Bridge Approach to St Katharine's Way is to jump off the bridge, but that doesn't necessarily mean it's the /best/ way. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ "Usenet is a strange place" - dmr 29 July 1999 Sig line vacant - apply within |