# Different break conditions in a for loop

**URL:** <https://discourse.nodered.org/t/different-break-conditions-in-a-for-loop/15658>\
**Category:** General\
**Created:** [18 September 2019 12:03 UTC](https://discourse.nodered.org/t/different-break-conditions-in-a-for-loop/15658 "2019-09-18T12:03:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobiasselin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobiasselin/32/4492_2.png) [@tobiasselin](https://discourse.nodered.org/u/tobiasselin)\
**Post date:** [18 September 2019 12:03 UTC](https://discourse.nodered.org/t/different-break-conditions-in-a-for-loop/15658/1 "2019-09-18T12:03:19Z")

</div>

I've been googling around a bit as I would like to break a loop with two different conditions. Haven't found anything useful so I will try here. I'm not after a complete solution, just a general approach.

At the moment I loop through the array with something like

`for (var i=0;i<length;i++) ... if (this || that)...break ...msg`

but when the break is triggered I don't which condition it was that caused it. So I tried to do something like

`for (var i=0;i<length;i++) ... if (this)...break ...msg1... else if (that) ... break...msg2`

but that didn't feel right and I don't know if it's even possible to do it like that. I thought I could do the first condition loop in one function node and then deal with the other condition in a second node that loops the messages that go through the first one with out triggering a break, but I'm thinking there has to be a better way. Any suggestions?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 September 2019 13:03 UTC](https://discourse.nodered.org/t/different-break-conditions-in-a-for-loop/15658/2 "2019-09-18T13:03:36Z")

</div>

Either test this or that again after the break, or set a flag in the loop

```auto
let flag = 0
for (var i=0;i<length;i++) {
  if (this) {
    flag = 1
    break
  }
  if (that) {
    flag = 2
    break
  }
  ...
}
// test flag here
```

---

<div class="post-metadata">

**Author:** ![tobiasselin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobiasselin/32/4492_2.png) [@tobiasselin](https://discourse.nodered.org/u/tobiasselin)\
**Post date:** [18 September 2019 13:35 UTC](https://discourse.nodered.org/t/different-break-conditions-in-a-for-loop/15658/3 "2019-09-18T13:35:20Z")

</div>

Thanks @Colin. Do I treat flags just like variables?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 September 2019 14:26 UTC](https://discourse.nodered.org/t/different-break-conditions-in-a-for-loop/15658/4 "2019-09-18T14:26:56Z")

</div>

It is a variable, you can call it whatever you like, I called it flag because I didn't know what it actually means in your system, you can call it something more meaningfull in your system.  
On a side note you should generally use `let` rather than `var` as the scope of `let` is more constrained. If you wanted to test `this` and `that` after the break rather than use a variable then define `i` before the for loop then it will still have a valid value after the break.

---

<div class="post-metadata">

**Author:** ![tobiasselin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobiasselin/32/4492_2.png) [@tobiasselin](https://discourse.nodered.org/u/tobiasselin)\
**Post date:** [18 September 2019 16:09 UTC](https://discourse.nodered.org/t/different-break-conditions-in-a-for-loop/15658/5 "2019-09-18T16:09:25Z")

</div>

Great, thanks.
