# Loop after loop in setup

**URL:** <https://discourse.processing.org/t/loop-after-loop-in-setup/6472>\
**Category:** Coding Questions\
**Created:** [December 10, 2018, 12:20pm UTC](https://discourse.processing.org/t/loop-after-loop-in-setup/6472 "2018-12-10T12:20:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)\
**Post date:** [December 10, 2018, 12:20pm UTC](https://discourse.processing.org/t/loop-after-loop-in-setup/6472/1 "2018-12-10T12:20:00Z")

</div>

If i want to use a specific loop for two different functions inside the draw what am i suppose to do??  
For example if i want to use this :

> for (int i=0; i\<table.getRowCount(); i++) {

for two different function inside setup, must i write it like this?

```auto
void setup() {
   for (int i=0; i<table.getRowCount(); i++) {
   float y=table.getFloat(i, "y");
   table.sortReverse("y")
   float max_y= table.getFloat(0, "y")
   float y2=max_y/2;
  }
   for (int i=0; i<table.getRowCount(); i++) {
   float y=table.getFloat(i, "y")
            if (y>y2) {
      println("Everything is ok");
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [December 10, 2018, 12:37pm UTC](https://discourse.processing.org/t/loop-after-loop-in-setup/6472/2 "2018-12-10T12:37:45Z")

</div>

From what I understand in your question, it depends on what you need. For loops just keep repeating whatever is within it until a condition is met.

I pretty sure for your scenario you would need 2 for loops, because you would need to sort an whole array before checking it.

However you should be careful when using `max_y` in your second for loop because it is declared in the first one making it inaccessible in the second one. (Unless you have it declared as a global variable elsewhere)

---

<div class="post-metadata">

**Author:** ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)\
**Post date:** [December 10, 2018, 12:44pm UTC](https://discourse.processing.org/t/loop-after-loop-in-setup/6472/3 "2018-12-10T12:44:38Z")

</div>

Yeah that’s my problem…I want to use the variable max\_y in the second loop to check the other values but i don’t know how to do it…  
My problem is very simple: one for loop finds a max value for a table that i load and divide it by 2 and the other same for loop check if there are values above max\_y/2.

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [December 10, 2018, 12:51pm UTC](https://discourse.processing.org/t/loop-after-loop-in-setup/6472/4 "2018-12-10T12:51:55Z")

</div>

You can have

```auto
float max_y= table.getFloat(0, "y");
float y2=max_y/2;

```

After the first for loop then, because the data will be sorted after it, and you can get the maximum value like you did before.

---

<div class="post-metadata">

**Author:** ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)\
**Post date:** [December 10, 2018, 12:55pm UTC](https://discourse.processing.org/t/loop-after-loop-in-setup/6472/5 "2018-12-10T12:55:46Z")

</div>

You mean this:

```auto
  for (int i=0; i<table.getRowCount(); i++) {
  }
  float y=table.getFloat(0, "ECG");
  table.sortReverse("ECG");
  float max_y= table.getFloat(0, "ECG");

  println(max_y);
  for (int i=0; i<table.getRowCount(); i++) {
    float a=table.getFloat(i, "ECG");
    if (a>max_y) {
      println("Everything is ok");
    }

```

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [December 10, 2018, 12:59pm UTC](https://discourse.processing.org/t/loop-after-loop-in-setup/6472/6 "2018-12-10T12:59:04Z")

</div>

Yes, except you don’t need `y` anymore. If you want to check if anything is above half of `max_y` it should be `a>max_y/2`
