title: Compare the current value with previous value in a loop in java
body: <p>How do I compare the current value with previous value in for loop in java and determine if they are different?</p>
  
  <p>Here I have for loop that loop every time I click the mouse. Therefore <code>i</code> depends on where I click.</p>
  
  <p>Here both the current <code>i</code> and the previous <code>i</code> are equal to 3.</p>
  
  <p><a href="https://www.dropbox.com/s/55dao9a9r8o4yis/Untitled%20picture%206.png" rel="nofollow">https://www.dropbox.com/s/55dao9a9r8o4yis/Untitled%20picture%206.png</a></p>
  
  <p>Here the current <code>i</code> equal to 7 and the previous <code>i</code> equal to 11.</p>
  
  <p><a href="https://www.dropbox.com/s/pj38l23ughn6ey9/Untitled%20picture%207.png" rel="nofollow">https://www.dropbox.com/s/pj38l23ughn6ey9/Untitled%20picture%207.png</a></p>
  
  <p>I am trying to make <code>if</code> statement to compare the two values of previous <code>i</code> with the current <code>i</code>.</p>
  
  <p>Here is my for loop</p>
  
  <pre><code>public void mousePressed() {      
      if (state == 0) {            
          for (int i = 0; i &lt; 6; i++) {
              if (mouseX &gt;= cards[i].x &amp;&amp; mouseX &lt;= cards[i].x + cards[i].WIDTH &amp;&amp; mouseY &gt;= cards[i].y &amp;&amp; mouseY &lt;= cards[i].y + cards[i].HEIGHT) {
                  cards[i].flip();                     
              }               
          }
  </code></pre>