Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-out.readnews.com!transit3.readnews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: Holy boop: goto for Java Date: Mon, 4 Jun 2012 10:45:58 -0700 (PDT) Organization: http://groups.google.com Lines: 72 Message-ID: References: <6AVyr.1859$8l2.827@newsfe14.iad> <9hrps7h7fnsv1etndoklmq4h4r1dnke6kv@4ax.com> NNTP-Posting-Host: 69.28.149.29 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1338832065 13794 127.0.0.1 (4 Jun 2012 17:47:45 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 4 Jun 2012 17:47:45 +0000 (UTC) In-Reply-To: <9hrps7h7fnsv1etndoklmq4h4r1dnke6kv@4ax.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=69.28.149.29; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 Xref: csiph.com comp.lang.java.programmer:15053 Gene Wirchenko wrote: > Robert Klemme wrote: >> Mike Schilling wrote: >>> * Do-while. It seems almost never to be what's needed. Though I do quite >>> often need >>> >>> while(true) >>> { >>> stuff >>> if (condition) >>> { >>> break; >>> } >>> more stuff >>> } >> >> Interesting: I cannot remember having needed this. While I do remember >> using do {} while. What use cases do you have for the construct above? > > In the sense that there are other ways one can do this, no, you > do not need this. It can be useful if you have multiple tests in that > loop, especially multiple tests that cannot be combined. An example > of this would be a body of: > process first piece > if error > break or continue as needed > process middle piece > if error > break or continue as needed > process last piece > > [snip] public void processResources(String ... resourceNames) { for (String name : resourceNames) { BufferedReader br; try { br = new BufferedReader(new FileReader(name)); } catch(FileNotFoundException exc) { logger.error("Cannot find "+ name, exc); continue; } assert br != null; // and is valid try { doSomething(br); } finally { try { br.close(); } catch(IOException exc) { logger.error("Cannot close "+ name, exc); continue; } } reportComplete(name); } } (Not using try-with-resources here, in order to illustrate the idiom.) -- Lew