# For loops

The forloop object contains the attributes of its parent for loop.

The `forloop` object contains the attributes of its parent [for loop](https://developer.versacommerce.de/articles/liquid-tags-iteration-tags#for).

The `forloop` object can only be used inside `for` tags.

## forloop.first

Returns `true` if it is the first iteration of the for loop, `false` otherwise.

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

```text
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.

```liquid
{% for product in collections.frontpage.products %}
  {{ forloop.index }}
{% else %}
  // no products in your frontpage collection
{% endfor %}
```

```text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
```

## forloop.index0

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

```liquid
{% for product in collections.frontpage.products %}
  {{ forloop.index0 }}
{% endfor %}
```

```text
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
```

## forloop.last

Returns `true` if it is the last iteration of the for loop, `false` otherwise.

```liquid
{% for product in collections.frontpage.products %}
  {% if forloop.last == true %}
    This is the last iteration!
  {% else %}
    Keep going...
  {% endif %}
{% endfor %}
```

```text
Keep going...
Keep going...
Keep going...
Keep going...
Keep going...
This is the last iteration!
```

## forloop.length

Returns the number of iterations of the loop.

```liquid
 <!-- if collections.frontpage.products contains 4 products -->
{% for product in collections.frontpage.products %}
  {% if forloop.first %}
  <p>This collection has {{ forloop.length }} products:</p>
  {% endif %}
  <p>{{ product.title }}</p>
{% endfor %}
```

```text
This collection has 4 products:
Pear
Orange
Nectarine
Cherry
```

## forloop.rindex

Returns `forloop.index` in reverse order.

```liquid
{% for product in collections.frontpage.products %}
  {{ forloop.rindex }}
{% endfor %}
```

```text
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
```

## forloop.rindex0

Returns `forloop.index0` in reverse order.

```liquid
{% for product in collections.frontpage.products %}
  {{ forloop.rindex0 }}
{% endfor %}
```

```text
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
```
