# How to find a max value with loop

**URL:** <https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476>\
**Category:** Beginners\
**Created:** [June 11, 2022, 2:23pm UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476 "2022-06-11T14:23:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvb](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@mvb](https://discourse.processing.org/u/mvb)\
**Post date:** [June 11, 2022, 2:23pm UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476/1 "2022-06-11T14:23:29Z")

</div>

How do I find the maximum and minimum value of an array with loops?

---

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [June 11, 2022, 2:47pm UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476/2 "2022-06-11T14:47:02Z")

</div>

Look at [this](https://www.youtube.com/watch?v=rxJRJV3eEaI)

---

<div class="post-metadata">

**Author:** ![Chigoz](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@Chigoz](https://discourse.processing.org/u/Chigoz)\
**Post date:** [June 11, 2022, 6:09pm UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476/3 "2022-06-11T18:09:34Z")

</div>

For some reason I don’t know Processing is not giving the minimum and maximum values when I used the standard logical code to get minimum and maximum values so I resorted to use Processing Syntax. Good luck with your project.

int houses = {25,30,18,105,33,10,17,8,65};  
void setup(){  
size(400,400);  
background(200);

int maxXM = max(houses);  
int minXM = min(houses);  
//println(maxXM);  
//print(minXM);

}

void draw(){  
}

---

<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:** [June 11, 2022, 6:24pm UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476/4 "2022-06-11T18:24:06Z")

</div>

Processing’s container IntList has both **min()** & **max()** methods:

> **[Reference](https://processing.org/reference/IntList_min_.html)**
>
> Return the smallest value.

> **[Reference](https://processing.org/reference/IntList_max_.html)**
>
> Return the largest value.

How about taking a peek on how they’re implemented straight from their source?

> <https://github.com/processing/processing/blob/processing-0270-3.5.4/core/src/processing/data/IntList.java#L544-L551>

> <https://github.com/processing/processing/blob/processing-0270-3.5.4/core/src/processing/data/IntList.java#L574-L581>

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 11, 2022, 6:26pm UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476/5 "2022-06-11T18:26:33Z")

</div>

Hello @Chigoz,

Please follow instructions here for posting code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

This is what you are posting:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/5/45a6f3105f6d6c7d599f52551258371620986da3.png)

This is what you should be posting:

```auto
int [] houses = {25, 30, 18, 105, 33, 10, 17, 8, 65};

```

Properly formatted and posted code helps the community focus on the code instead of the errors from a quick cut and paste.

`:)`

---

<div class="post-metadata">

**Author:** ![Chigoz](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@Chigoz](https://discourse.processing.org/u/Chigoz)\
**Post date:** [June 11, 2022, 6:33pm UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476/6 "2022-06-11T18:33:12Z")

</div>

Ok, I’ll learn that, is formatting possible using a phone?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 11, 2022, 7:09pm UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476/7 "2022-06-11T19:09:41Z")

</div>

> [@Chigoz](#):
>
> Ok, I’ll learn that, is formatting possible using a phone?

I do not use my phone for this.

I suggest you start a _ **separate topic on the subject** _ and users with experience can advise you.

This would be of great benefit to others as well!  
There are a lot of sloppy posts recently and this may be related to their development environment.

Details are important.

What are you using to develop your code on the phone?

`:)`

---

<div class="post-metadata">

**Author:** ![Chigoz](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@Chigoz](https://discourse.processing.org/u/Chigoz)\
**Post date:** [June 11, 2022, 7:19pm UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476/8 "2022-06-11T19:19:40Z")

</div>

Recently for some reason I don’t know some websites do not load on my Google Chrome browser hence after I write the code in Processing IDE I transfer it over the internet to my phone and if there is any need to do further work on the code. I edit the code with my phone and run it on OpenProcessing.

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [June 12, 2022, 3:22am UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476/9 "2022-06-12T03:22:19Z")

</div>

No need for a long winded approach, depending on the array type, if its just ints or floats its simple

```auto
int[] list = {1,6,7,3,5,6,3,4,2,8};

println(max(list));

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 12, 2022, 2:58pm UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476/10 "2022-06-12T14:58:51Z")

</div>

> [@mvb](#):
>
> How do I find the maximum and minimum value of an array with loops?

> [@Mikey83](#):
>
> No need for a long winded approach

The need is determined by the the original poster.

The original poster may be a beginner and understanding basic loops and search algorithms is an important first step and a very common question.

It is important to master the basics and also nice to have the options of the helper methods for consideration.

I may have been a bit long winded…

`:)`

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [June 12, 2022, 3:12pm UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476/11 "2022-06-12T15:12:15Z")

</div>

No, not long winded, you make a good point, its better to know HOW the functions work at their core then just using them

---

<div class="post-metadata">

**Author:** ![mvb](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@mvb](https://discourse.processing.org/u/mvb)\
**Post date:** [June 12, 2022, 8:17pm UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476/12 "2022-06-12T20:17:47Z")

</div>

The thing is that the loop goes on and one, and I have the maximum and minimum value a lot of times.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 12, 2022, 8:25pm UTC](https://discourse.processing.org/t/how-to-find-a-max-value-with-loop/37476/13 "2022-06-12T20:25:16Z")

</div>

Hello @mvb,

Think about what the loop is doing and where you want these statements:

```auto
  print(min);
  print(max);

```

Consider using _println()_ instead.

I am seeing “this piece of code” in other posts as well with different user names.

Is this a homework assignment?

`:)`
