How can I assign variable scope within a for loop iteration?

I have the following loop:

{% for line in line_items %}  
  {% if condition %}
    {% assign foo = 'bar' %}
  {% endif %} 
{% endfor %}

How do I create a variable that’s only accessible for the scope of the current iteration?
If the condition turns out true in the first iteration, then false in the second iteration, I don’t want foo to have a value.

What I ended up doing is set foo to false at the end of the iteration, but wondering if there’s a more clever way.

It would probably be easier to answer your question if you described what you’re actually trying to achieve. What’s the condition in your code? Are you trying to find out if any line item meets the condition, or all line items?

That said, maybe {% break %} is what you’re looking for: https://help.shopify.com/en/themes/liquid/tags/iteration-tags

The other reply seemingly has no idea what your talking which is mildly funny from an “expert” since scoped variables are a very basic programming concept but from what I have seen, all variables in liquid have global scopes (at least up to the section level). This is similar to how var is in JavaScript, before let was introduced. See this article for more info.

This seems like a pretty lazy design decision but I would love to hear an explanation as to why and if scoped variables are on the table for future iterations of liquid.