title: java: looping on the two boolean values (false, true)
body: <p>This is a stylistic question. I want to loop twice with a variable <code>on</code> which is set to false, then to true. Which of these is clearer:</p>
  
  <p>A) </p>
  
  <pre><code>for (final boolean on : new boolean[] { false, true} )
  {
     doStuffBasedOnABooleanFlag(on);
  }
  </code></pre>
  
  <p>B) </p>
  
  <pre><code>for (int i = 0; i &lt; 2; ++i)
  {
     final boolean on = (i == 1);
     doStuffBasedOnABooleanFlag(on);
  }
  </code></pre>
  
  <p>C) something else</p>
  
  <hr>
  
  <p>edit: Murphy's law of unintended interpretations comes into play... the use case I have originally looked something like this instead of doStuffBasedOnABooleanFlag:</p>
  
  <pre><code>for (final boolean on : new boolean[] { false, true} )
  {
     JButton button = on ? onButton : offButton;
     button.addActionListener(new ActionListener() {
        @Override public void actionPerformed(ActionEvent event) {
        doStuffLaterBasedOnABooleanFlag(on);
        }
     }
  }
  </code></pre>
  
  <p>But I think I like Brendan's answer, I'll just refactor the loop contents into a separate method:</p>
  
  <pre><code>doStuffBasedOnABooleanFlag(false);
  doStuffBasedOnABooleanFlag(true);
  
     ...
  
  private void doStuffBasedOnABooleanFlag(final boolean on)
  {
     JButton button = on ? onButton : offButton;
     button.addActionListener(new ActionListener() {
        @Override public void actionPerformed(ActionEvent event) {
        doStuffLaterBasedOnABooleanFlag(on);
        }
     }
  }
  </code></pre>