# Loops with different colors

**URL:** https://discourse.processing.org/t/loops-with-different-colors/30257
**Category:** Coding Questions
**Tags:** homework
**Created:** [May 24, 2021, 8:44pm UTC](https://discourse.processing.org/t/loops-with-different-colors/30257 "2021-05-24T20:44:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Kenga123456](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Kenga123456](https://discourse.processing.org/u/Kenga123456)
#### Post date: [May 24, 2021, 8:44pm UTC](https://discourse.processing.org/t/loops-with-different-colors/30257/1 "2021-05-24T20:44:42Z")

</div>

why doesn’t this code work?

```auto
	color[] c = new color [6];

	int x;
int l;

void setup(){
	size(900, 550);

	
	c[0]=color(8,100, 0);   
  c[1]=color(12);  
  c[2]=color(16);  
	c[3]=color(4);
	c[4]=color(4);
	c[5]=color(4);

	
}

void draw() {
	int x = 0;
	int l = 125;
  for (int y = 0; y <= 6; y= y + 1 ) {
	fill(c[x];
		rect(width - 35, l, 50, 50);
		x++;
		l = l + 75;
}
}

```

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [May 24, 2021, 9:03pm UTC](https://discourse.processing.org/t/loops-with-different-colors/30257/2 "2021-05-24T21:03:39Z")

</div>

Hi @Kenga123456 ,

When asking a question on the forum, it’s better to provide more details about what you are trying to solve and what you already did (what doesn’t work…) 😋

Quick tip : you can press `Ctrl+T` in the Processing IDE to auto format your code (correct indentation for example)

I your code there’s an error at this line : `fill(c[x];` but it may be caused by copy pasting…

The first error that comes out is :

```auto
ArrayIndexOutOfBoundsException: 6

```

when trying to access a color in your array.

If you look at `y` in the for loop, it goes from `0` to what? 😉

---

<div class="post-metadata">

### Author: ![Kenga123456](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Kenga123456](https://discourse.processing.org/u/Kenga123456)
#### Post date: [May 25, 2021, 1:52pm UTC](https://discourse.processing.org/t/loops-with-different-colors/30257/3 "2021-05-25T13:52:28Z")

</div>

nvm i solved it

```auto
color[] c = new color[0];
int x;

void setup() {
  size(900, 550);
  c[0]=color(255, 0, 0); 
  c[1]=color(0, 255, 0); 
  c[2]=color(255, 255, 0); 
	c[3]=color(0, 0, 255);
  c[4]=color(64, 224, 208); 
  c[5]=color(255); 
}

void draw() {
  x = 0;
	rectMode(CENTER);

  for (int i = 125; i <= 500; i = i+75 ) {
    fill(c[x]);
    rect(width - 35, i, 50, 50);
    x++;
   
  }
}

```
