Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Doc O'Leary Newsgroups: comp.lang.javascript Subject: Re: Using break in while loops bad habit? Date: Tue, 19 Jan 2016 16:48:29 -0000 (UTC) Organization: Subsume Technologies, Inc. Lines: 55 Message-ID: References: <94ec98fa-9956-48c9-ad34-de8d07cd6419@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Tue, 19 Jan 2016 16:48:29 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="35a2a0a28e4aa659e58d71f46ac8d861"; logging-data="8096"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18sjONKqyR1MTBaubbDF+gMye/VUaiaNnM=" User-Agent: com.subsume.NNTP/1.0.0 Cancel-Lock: sha1:/YUOmCS+41nZwGp8LBPlaqJxEs0= Xref: csiph.com comp.lang.javascript:29350 For your reference, records indicate that Gene Wirchenko wrote: > Even more simply put: because it is useful. Better: because that’s the way it *actually* works. Loops are all conceptually the same. They go round and round (i.e., jump from the end to the beginning) until there is a reason to break out of them (i.e., jump past the end). It is a mere language convention/convenience that *one* particular set of exit conditions is given with the control statement. for (var foo = 0; foo < 16; foo++) { document.write('bar
'); } is the same as var foo = 0; while (foo < 16) { document.write('bar
'); foo++; } is the same as var foo = 0; while (1) { if (foo > 15) break; document.write('bar
'); foo++; } Languages like JS that have a proliferation of loop types (especially ones that torture syntax conventions like do-while does) are less pure. It could just as easily be argued that break should be kept and all the those arbitrary loop “wrappers” should go. The only way it makes computational sense to get rid of arbitrary breaks is to add a construct that can be evaluated to a *known* non-infinite number. Iterators do that. But even then you’ll run into situations where you want to get just the *first* of some item in a list that matches some criteria, and it make no sense to loop over the rest of the list after it has been found. -- "Also . . . I can kill you with my brain." River Tam, Trash, Firefly