# Problem with conditional structure with « if » into loop

**URL:** <https://discourse.processing.org/t/problem-with-conditional-structure-with-if-into-loop/13551>\
**Category:** Coding Questions\
**Created:** [August 22, 2019, 8:15pm UTC](https://discourse.processing.org/t/problem-with-conditional-structure-with-if-into-loop/13551 "2019-08-22T20:15:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerome51](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@jerome51](https://discourse.processing.org/u/jerome51)\
**Post date:** [August 22, 2019, 8:15pm UTC](https://discourse.processing.org/t/problem-with-conditional-structure-with-if-into-loop/13551/1 "2019-08-22T20:15:35Z")

</div>

Hi,

Sorry for my english, i’m french.

I’d like to check each pixel from image by step of 5 and write coordinates into an array of each pixel whose value is under 150.

I used a loops to check pixels and a « if » to verify its values but it doesn’t work.

Do someone know why ?

Here is my code.

> let bugs = ;  
> let xpos = ;  
> let ypos = ;
> 
> let pix;  
> let img;  
> let ry = 0;  
> let rx = 0;  
> let ba;  
> function preload() {  
> img = loadImage(‘assets/pomme.jpg’);  
> }
> 
> function ecriture(){  
> for (let rx = 0 ; rx \< 225 ; rx = rx+5){  
> if (img.get(rx, ry) \< 150){  
> xpos.push(rx);  
> ypos.push(ry);  
> ellipse(rx, ry, 2, 2);  
> }  
> }  
> }
> 
> function setup() {  
> createCanvas(225, 225);  
> image(img, 0, 0);
> 
> for (let ry = 0 ; ry \< 225 ; ry = ry+5){
> 
> ```
> ecriture();
> }
>       
> textSize(45);   
> text(ypos[55], 100, 130);
> 
> for (let i = 0; i < 100; i++){
> bugs.push(new Jitter(xpos[i], ypos[i]));
> }
> 
> ```
> 
> }
> 
> function draw() {
> 
> ```
> for (let i = 0; i < bugs.length; i++){
> bugs[i].display();
> }
> 
> ```
> 
> }
> 
> class Jitter {  
> constructor (x, y) {  
> this.x = 50;  
> this.y = 50;  
> }
> 
> display(){  
> fill (0);  
> ellipse(this.x, this.y, 5, 5);  
> }  
> }

thank you

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [August 22, 2019, 10:33pm UTC](https://discourse.processing.org/t/problem-with-conditional-structure-with-if-into-loop/13551/2 "2019-08-22T22:33:06Z")

</div>

please paste your code into the

```auto
</> "preformatted text" Code tag 

```

* * *

-a- i think  
rx ry are global vars and used in setup and a called function “ecriture()”  
so the 2 FOR loops should not use a local var LET instruction

-b- you compare of the img.get(x,y) ( what is a pixel color info ) with 150 ?  
try like  
[https://p5js.org/reference/#group-Color](https://p5js.org/reference/#group-Color)

```auto
brightness(img.get(rx, ry))

```

see  
[https://editor.p5js.org/kll/sketches/YMhmZzAVq](https://editor.p5js.org/kll/sketches/YMhmZzAVq)

---

<div class="post-metadata">

**Author:** ![jerome51](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@jerome51](https://discourse.processing.org/u/jerome51)\
**Post date:** [August 23, 2019, 8:29am UTC](https://discourse.processing.org/t/problem-with-conditional-structure-with-if-into-loop/13551/3 "2019-08-23T08:29:11Z")

</div>

Thank you very much kll.  
Your answer helps me a lot to understand coding.
