The forloop object contains attributes of its parent for loop.

forloop.first

Returns true if it's the first iteration of the for loop. Returns false if it is not the first iteration.

{% for product in collections.frontpage.products %}
  {% if forloop.first == true %}
    First time through!
  {% else %}
    Not the first time.
  {% endif %}
{% endfor %}
First time through!
Not the first time.
Not the first time.
Not the first time.
Not the first time.

forloop.index

Returns the current index of the for loop, starting at 1.

forloop.index0

Returns the current index of the for loop, starting at 0.

forloop.last

Returns true if it's the last iteration of the for loop. Returns false if it is not the last iteration.

forloop.length

Returns the number of iterations of the loop.