title: Lua - For loops: saving the value of the control variable
body: <p>I'm having a hard time understanding the example from the doc(<a href="https://www.lua.org/pil/4.3.4.html" rel="nofollow noreferrer">https://www.lua.org/pil/4.3.4.html</a>) and need some clarification.</p>
  <blockquote>
  <p>If you need the value of the control variable after the loop (usually when you break the loop), you must save this value into another variable:</p>
  <pre><code>-- find a value in a list
  local found = nil
  for i=1,a.n do
    if a[i] == value then
      found = i      -- save value of `i'
      break
    end
  end
  print(found)
  </code></pre>
  </blockquote>
  <p>I don't understand the <code>a.n</code> and <code>if a[i] == value then</code> parts. Are they creating a table <code>a={n=5,...}</code> and calling a single value like <code>a.n=5</code>?</p>
  <p>I think I need a written explanation of what's occurring in the example, and what is missing, or a complete example. I'm guessing its missing the declaration of table/variables...?</p>
  <p>Cause <code>a[i]</code> is calling entries of <code>a={}</code> and I don't understand what 'value' is...? A variable I have to declare first and then set to a specific value...? What value though?</p>
  <p>Why am I calling other entries in a table (i.e. <code>a[i]</code>) when I'm defining <code>a.n</code> as the entry I want to be dealing with?</p>
  <p>And in this case do I have to define the entry I want the control variable to break on by predefining the number and that's what value is set to...?</p>
  <p>That would defeat the point of calling the value of the control variable if I already define what its going to be. I'm very confused. Like I understand if the example was:</p>
  <pre><code>local found = nil
  local a=7
  for i=1,a do
    print(i)
    found=a
    break
  end
  </code></pre>
  <p>However <code>print(found)</code> is equal to 7 rather than the last iteration of the incomplete <code>for</code> loop (2 or 1?).</p>
  <p>What I was looking for was a way to save whatever number the control variable was on when the loop was interrupted.</p>
  <p>So if it was <code>for i=1,5 do</code>... and the last printed iteration was 4, how would I call this value? I'm unsure if the doc is providing that in its example or not.</p>