# Where am I going wrong in my loop structure?

**URL:** https://discourse.processing.org/t/where-am-i-going-wrong-in-my-loop-structure/18923
**Category:** Coding Questions
**Created:** [March 23, 2020, 3:00am UTC](https://discourse.processing.org/t/where-am-i-going-wrong-in-my-loop-structure/18923 "2020-03-23T03:00:51Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.processing.org/u/abc)
#### Post date: [March 23, 2020, 3:00am UTC](https://discourse.processing.org/t/where-am-i-going-wrong-in-my-loop-structure/18923/1 "2020-03-23T03:00:51Z")

</div>

I’m trying to make this if statement repeat, but each time it fails. I tried using a while statement instead, that didn’t even run? I’m not sure why only if statements work in my programs and just where I’m generally going wrong.

```auto
int r = 0;

int g = 0;

int b = 0;

float timer = 0;

void setup(){

size(800, 800); // size of the project window

};

void draw(){

background(r, g, b);

if(timer < 255){

r = r + 5;

g = g + 2;

b = b + 10;

timer = timer + 0.01;

}

//if the timer is 255/ for each moment timer is less than 255, add 5 to integer r,

//2 to integer b, and 10 to integer b.

//once the integer called timer is greater than 255, this function will end.

//But once the timer is greater than 255, this function will run which will set it back to zero, and the previous function will start again.

};

failing while program

int r = 0;

int g = 0;

int b = 0;

float timer = 0;

void setup(){

size(800, 800); // size of the project window

};

void draw(){

while(timer < 355) {

background(r, g, b);

timer = timer + 0.01;

r= r + 10;

g = g + 3;

b = b + 8;

if(timer > 355){ r = 0; g = 0; b = 0; timer = 0;}

};

```

---

<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: [March 23, 2020, 11:33am UTC](https://discourse.processing.org/t/where-am-i-going-wrong-in-my-loop-structure/18923/2 "2020-03-23T11:33:47Z")

</div>

here is your first program but running on and on in a loop, repeating the colors

```auto

int r = 0;
int g = 0;
int b = 0;

float timer = 0;

void setup() {
  size(800, 800); // size of the project window
}

void draw() {
  background(r, g, b);

  if (timer < 255) {
    r = r + 5;
    g = g + 2;
    b = b + 10;

    timer = timer + 1;
  } else {
    timer = 0;
    r = 0;
    g = 0;
    b = 0;
  }

  //if the timer is 255/ for each moment timer is less than 255, add 5 to integer r,
  //2 to integer b, and 10 to integer b.
  //once the integer called timer is greater than 255, this function will end.
  //But once the timer is greater than 255, this function will run which will set it back to zero, and the previous function will start again.
}

```

The program with While won’t run as you expect, because draw() doesn’t update the screen throughout but only once at its end. Therefore you won’t see changes in the while-loop but only at the end of draw().

```auto

// failing while program

int r = 0;
int g = 0;
int b = 0;

float timer = 0;

void setup() {
  size(800, 800); // size of the project window
}

void draw() {

  background(r, g, b);

  while (timer < 355) {

    background(r, g, b);

    timer = timer + 0.01;

    r= r + 10;
    g = g + 3;
    b = b + 8;
  }

  if (timer > 355) { 
    r = 0; 
    g = 0; 
    b = 0; 
    timer = 0;
  }
}

```

Warm regards,

Chrisir
