# \`continue\` statement in \`range\` block

**URL:** <https://discourse.gohugo.io/t/continue-statement-in-range-block/19015>\
**Category:** support\
**Created:** [June 3, 2019, 2:01am UTC](https://discourse.gohugo.io/t/continue-statement-in-range-block/19015 "2019-06-03T02:01:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [June 3, 2019, 2:01am UTC](https://discourse.gohugo.io/t/continue-statement-in-range-block/19015/1 "2019-06-03T02:01:23Z")

</div>

I was just wondering if it is possible to conditionally skip an iteration inside a range.

---

<div class="post-metadata">

**Author:** ![RickCogley](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rickcogley/32/10269_2.png) [@RickCogley](https://discourse.gohugo.io/u/RickCogley)\
**Post date:** [June 3, 2019, 2:48am UTC](https://discourse.gohugo.io/t/continue-statement-in-range-block/19015/2 "2019-06-03T02:48:34Z")

</div>

Range conditionals are possible, but I am not sure what you need. Searching this forum for “range conditional” here is one example:

> [@Condition in range](https://discourse.gohugo.io/t/condition-in-range/5793):
>
> Hello! I have a range of the 20 element. I need every 4 element to pastle the HTML content. After 20-th elements no need to pastle the code. How do that? In PHP I have the code foreach ($array as $key =\> $value) { if(is\_int($key/4) AND $key \< count($array)) { echo "html\_code"; } }

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [June 3, 2019, 5:33am UTC](https://discourse.gohugo.io/t/continue-statement-in-range-block/19015/3 "2019-06-03T05:33:09Z")

</div>

I’m looking for a way to skip an unnecessary iteration in range. For example, in Java, we can do so.

```java
for(int i=0; i<3; i++) {
  if(i==2)
    continue;
  System.out.print(i+" ");
}

```

This example would output `0 1 3`. Here `continue` skips the `i=2` iteration. I’m looking for an analog for Hugo.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [June 3, 2019, 9:10am UTC](https://discourse.gohugo.io/t/continue-statement-in-range-block/19015/4 "2019-06-03T09:10:55Z")

</div>

Your example can be rewritten as:

```auto
for(int i=0; i<3; i++) {
  if(i!=2)
    System.out.print(i+" ");
}

```

(IF i IS NOT 2) and does not require a `continue` then.

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [June 3, 2019, 10:06am UTC](https://discourse.gohugo.io/t/continue-statement-in-range-block/19015/5 "2019-06-03T10:06:29Z")

</div>

I myself know that. But there are some situations where I need `continue`. Does Hugo have something similar?

---

<div class="post-metadata">

**Author:** ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)\
**Post date:** [June 3, 2019, 12:02pm UTC](https://discourse.gohugo.io/t/continue-statement-in-range-block/19015/6 "2019-06-03T12:02:04Z")

</div>

Hi @UtkarshVerma. I think you’ll have better luck if you give us a concrete example of what you’d like to do.

---

<div class="post-metadata">

**Author:** ![regis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/regis/32/4958_2.png) [@regis](https://discourse.gohugo.io/u/regis)\
**Post date:** [June 3, 2019, 7:11pm UTC](https://discourse.gohugo.io/t/continue-statement-in-range-block/19015/7 "2019-06-03T19:11:17Z")

</div>

To my knowledge and the [Go Template doc](https://golang.org/pkg/text/template/#hdr-Actions) there is nothing equivalent to `continue` or `break` in `ranges`

---

<div class="post-metadata">

**Author:** ![Jura](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jura/32/13581_2.png) [@Jura](https://discourse.gohugo.io/u/Jura)\
**Post date:** [June 4, 2019, 5:25pm UTC](https://discourse.gohugo.io/t/continue-statement-in-range-block/19015/8 "2019-06-04T17:25:53Z")

</div>

I agree, it’s not possible. Our own Bep did make a feature request for it [here](https://github.com/golang/go/issues/20531), which got accepted by the Go team. 🙂

In the past this feature was added, but then got [reverted back](https://github.com/golang/go/commit/fd7331a821a6b79d5f2f82075c032ac459670d7d) because of issues.

Currently `break` and `continue` are unplanned. So it looks it may take a while. (If they were in an upcoming Go version, they move from unplanned to planned.)

---

<div class="post-metadata">

**Author:** ![ju52](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ju52/32/6749_2.png) [@ju52](https://discourse.gohugo.io/u/ju52)\
**Post date:** [June 4, 2019, 5:37pm UTC](https://discourse.gohugo.io/t/continue-statement-in-range-block/19015/9 "2019-06-04T17:37:32Z")

</div>

use **range** with index

> **[Introduction to Hugo Templating](https://gohugo.io/templates/introduction/#example-3-declaring-variable-names-for-an-array-element-s-index-and-value)**
>
> Hugo uses Go's \`html/template\` and \`text/template\` libraries as the basis for the templating.

and the **math** functions

> **[Math](https://gohugo.io/functions/math/#readout)**
>
> Hugo provides nine mathematical operators in templates.

with **if eq** all is done

> **[eq](https://gohugo.io/functions/eq/)**
>
> Returns the boolean truth of arg1 == arg2.

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [June 5, 2019, 2:18am UTC](https://discourse.gohugo.io/t/continue-statement-in-range-block/19015/10 "2019-06-05T02:18:41Z")

</div>

I myself have a workaround for that. It is exactly what @davidsneighbour had suggested. Therefore what I was looking for wasn’t a solution. It was just me asking if such a feature was present in Go or not.

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [June 5, 2019, 2:19am UTC](https://discourse.gohugo.io/t/continue-statement-in-range-block/19015/11 "2019-06-05T02:19:30Z")

</div>

Oh. Anyhow, thanks for the information.
