title: How to implement a return statement when using a for loop in Java?
body: <p>I am creating a method that returns the variable elt if it exists in an array.  If it does not exist in an array, I need to return null.</p>
  
  <p>The issue is, I am checking for the variable elt in each item in the array using an if statement in a for-loop.  I do not think I can put a return statement at the end of the if statement in the for-loop, because each time it executes the if statement, the potential return value would be different.  I think this would make a new return value each time the for-loop was looped through.  To solve this, I created a boolean temp variable called exist.  If true, the method will return the variable elt.  If false, it will return null.  The code I am working with is below.</p>
  
  <pre><code>public T remove(T elt) {
          boolean exist;
  
          for (int i=0; i&lt;data.length; i++) {
              if (data[i] == elt) {
                  data[i] = null;
                  size--;
                  exist = true;
                  System.out.println(exist);
  
                  for (++i; i &lt; data.length; i++) {
                      data[i-1] = data[i];
                  }
              }
          }
          if (exist = true)
              return elt;
          else
              return null;
      }
  </code></pre>
  
  <p>My question is, is there a way to tuck the return statement in the method without using a temp variable?</p>