title: Changing between var and val in Scala leads to logical checks behaving differently
body: <p>I was working with Project Euler <a href="https://projecteuler.net/problem=4" rel="nofollow">Problem 4</a> (finding the largest palindrome formed from the product of two three-digit numbers), and produced the solution <code>euler4_1</code>, which uses a variable <code>cand</code>. The function runs as expected, iterating through all pairs <code>(i, j)</code> and only replacing <code>cand</code> with a bigger palindrome.</p>
  
  <pre><code>def euler4_1(): Int = {
  
      var cand=0
  
      for(i &lt;- 100 until 999){
          for(j &lt;- 100 until i){
            if (((i*j).toString.reverse == (i*j).toString)&amp;&amp;(i*j&gt;cand)){
              cand=i*j
              println(cand)
              }
          }
      }
  
      return cand
      }
  
      /** 
      ...
      666666
      678876
      696696
      749947
      819918
      824428
      828828
      861168
      888888
      906609
       */
  </code></pre>
  
  <p>However, initializing <code>cand</code> as a <code>var</code> instead of a <code>val</code> seems to mess things up - a number of palindromes which are smaller than the current best are also printed out.</p>
  
  <pre><code>def euler4_2(): Int = {
  
      val cand=0
  
      for(i &lt;- 100 until 999){
          for(j &lt;- 100 until i){
            if (((i*j).toString.reverse == (i*j).toString)&amp;&amp;(i*j&gt;cand)){
              val cand=i*j
              println(cand)
              }
          }
      }
  
      return cand
      }
  
      /** 
      ...
       601106
       650056
       853358
       141141
       282282
       119911
       906609
       514415
       580085
       */
  </code></pre>
  
  <p>What's happening in the second code sample, labelled <code>euler4_2</code>?</p>
  
  <p>[Comments on how to make the code more idiomatic in scala are also appreciated].</p>