title: if/else constructs inside and outside functions
body: <p>When I look at R functions I often find the following structure:</p>
  
  <pre><code>f &lt;- function(exp=T) {
    if (exp)
      a &lt;- 1
    else
      a &lt;- 2
  }
  f()
  f(F)
  </code></pre>
  
  <p>This will run without error. But executing the inner function code throws an error as R probably assumes that the statement is finished after the first assignment <code>a &lt;- 1</code> and cannot handle the following else.</p>
  
  <pre><code>exp=T
  if (exp)
    a &lt;- 1
  else
    a &lt;- 2
  </code></pre>
  
  <p>Now, this makes sense to me, but I still would like to understand why the behaviour of the executed code differs when executed inside or outside a function.</p>