title: How to avoid code duplication?
body: <p>Is it possible to avoid code duplication in such cases? (Java code)</p>
  
  <pre><code>void f()
  {
      int r;
      boolean condition = true;
      while(condition)
      {
          // some code here (1)
  
          r = check();
          if(r == 0)
              break ;
          else if(r == 1)
              return ;
          else if(r == 2)
              continue ;
          else if(r == 3)
              condition = false;
  
          // some code here (2)
  
          r = check();
          if(r == 0)
              break ;
          else if(r == 1)
              return ;
          else if(r == 2)
              continue ;
          else if(r == 3)
              condition = false;
  
          // some code here (3)
      }
      // some code here (4)
  }
  
  int check()
  {
      // check a condition and return something
  }
  </code></pre>
  
  <p>A possible solution may be using Exceptions, but that doesn't seem to be a good practice.
  Is there any so-called good pattern of program flow control in such cases? For example, a way to call <code>break ;</code> from inside the <code>check()</code> function.
  (Possibly in other programming languages)</p>