# Break and for-loops

**URL:** <https://discourse.processing.org/t/break-and-for-loops/39374>\
**Category:** Beginners\
**Created:** [October 20, 2022, 3:52pm UTC](https://discourse.processing.org/t/break-and-for-loops/39374 "2022-10-20T15:52:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)\
**Post date:** [October 20, 2022, 3:52pm UTC](https://discourse.processing.org/t/break-and-for-loops/39374/1 "2022-10-20T15:52:49Z")

</div>

Just a quick question. I am new to using “break” and I am making a program that checks an integer grid if the number 1 is in an entry. Obviously I don’t want to check the rest of the grid if I have found an entry that is equal to 1. Therefore, I wanted to use “break” but should I use break twice in the inner for-loop to exit the double for-loop?  
My overall program is a little more complicated than that but you should get the idea by looking at the code below:

```auto
for(int i=0; i< minnumberrow; i++){ // does something for each line in the file
          for(int j=0; j< minnumbercol; j++){ // do something for each collum of each line
            if(int(lines[i].charAt(j))==49){ // if it is a 0 or a 1 (49 seems to be 1 in char)
              taken=true;
              break;
            }
          }
        }

```

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [October 20, 2022, 4:29pm UTC](https://discourse.processing.org/t/break-and-for-loops/39374/2 "2022-10-20T16:29:36Z")

</div>

Yes, `break` will only break out of the inner-most loop.

A cleaner alternative might be to test against `taken` in your loop conditions (also, you don’t need to convert your `charAt()` to an `int`, just compare it to `'1'`):

```auto
taken = false;
for(int i=0; i< minnumberrow && !taken; i++){ // does something for each line in the file
          for(int j=0; j< minnumbercol && !taken; j++){ // do something for each column of each line
            if(lines[i].charAt(j) == '1') { // if it is a 0 or a 1
              taken=true;
            }
          }
        }

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [October 20, 2022, 4:37pm UTC](https://discourse.processing.org/t/break-and-for-loops/39374/3 "2022-10-20T16:37:03Z")

</div>

Hi

> **[Reference](https://processing.org/reference/break.html)**
>
> Ends the execution of a structure such as switch, for, or while and jumps to the next statement after.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 20, 2022, 5:50pm UTC](https://discourse.processing.org/t/break-and-for-loops/39374/4 "2022-10-20T17:50:17Z")

</div>

> [@jensemand](#):
>
> Therefore, I wanted to use “break” but should I use break twice in the inner for-loop to exit the double for-loop?

You can define a `label:` right before the outer loop and issue a `break` using that label name:

> **[Labeled statement - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label)**
>
> A labeled statement is any statement that is prefixed with an identifier. You can jump to this label using a break or continue statement nested within the labeled statement.

```auto
boolean taken = false;

taken: 
for (int i = 0; i < 10; ++i) for (int j = 0; j < 20; ++j)
  if (i == 5 && j == 15) {
    println(j, i);
    taken = true;
    break taken;
  }

println(taken);
exit();

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 20, 2022, 6:03pm UTC](https://discourse.processing.org/t/break-and-for-loops/39374/5 "2022-10-20T18:03:56Z")

</div>

When you are in a function of its own, return() command is your friend
