title: Initializing a variable on loop.first in jinja2 worked in version 2.8, but not in version 2.9.5
body: <p>I was applying the following logic in jinja2 version 2.8 and the code was working as expected:</p>
  
  <pre><code>{%- for x in timeline -%}
    {%- if loop.first -%}
      {%- set N = 0 -%}
      ...do something with N...
    {%- else -%}
      {%- if prevActionType != "some string" -%}
        {%- set N = N + 1 -%}
        ...do something with N...
      {%- endif -%}
    {%- endif -%}
    {%- set prevActionType = x.actionType -%}
  {%- endfor -%}
  </code></pre>
  
  <p>As can be seen, I need to track <code>N</code> separately from the loop iteration number.</p>
  
  <p>I have upgraded jinja2 to version 2.9.5 and now I get the following error:</p>
  
  <pre><code>...
  {%- set N = N + 1 -%}
  UndefinedError: 'N' is undefined
  </code></pre>
  
  <p>It seems to me, if the <code>timeline</code> data has 1 or more rows, <code>N</code> will always be initialized to the value 0. According to jinja2 docs, <code>if</code> statements do not introduce new scope. My entire code block only has one <code>for</code> loop and <code>N</code> is only used inside this <code>for</code> loop.</p>
  
  <p>The underlying python version is 2.7.x.   Why this error after upgrading?</p>