title: Julia - ERROR: UndefVarError: not defined
body: <p>I have a piece of code that works outside a loop, but fails inside a loop.</p>
  
  <p>See:</p>
  
  <pre><code>julia&gt; margin = 1
  1
  
  julia&gt; rat = [-0.3 ; -0.8]
  2-element Array{Float64,1}:
   -0.3
   -0.8
  
  julia&gt; beta = 0
  0
  
  julia&gt; for q=1:2
             if rat[q] - margin &gt; beta
                 beta = rat[q] - margin
             end
         end
  ERROR: UndefVarError: beta not defined
  Stacktrace:
   [1] top-level scope at ./REPL[803]:2 [inlined]
   [2] top-level scope at ./none:0
  
  julia&gt; q=1
  1
  
  julia&gt; if rat[q] - margin &gt; beta
             beta = rat[q] - margin
         end
  
  julia&gt; q=2
  2
  
  julia&gt; if rat[q] - margin &gt; beta
             beta = rat[q] - margin
         end
  </code></pre>
  
  <p>Can someone explain this error ("ERROR: UndefVarError: beta not defined")?</p>
  
  <p>(I have Julia v"1.1.1", on Mac OSX)</p>