Re: Holy boop: goto for Java
 
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