# If statements in array

**URL:** https://discourse.processing.org/t/if-statements-in-array/34670
**Category:** Beginners
**Created:** [January 16, 2022, 4:23pm UTC](https://discourse.processing.org/t/if-statements-in-array/34670 "2022-01-16T16:23:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jimma](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jimma](https://discourse.processing.org/u/Jimma)
#### Post date: [January 16, 2022, 4:23pm UTC](https://discourse.processing.org/t/if-statements-in-array/34670/1 "2022-01-16T16:23:44Z")

</div>

Hi all,

First of all I am very inexperienced with processing and this is a first attempt to create a simple set of rules. My goal is to transfer between various states (A,B,C,D) and finally make the program terminate itself in the desired state. Now I’ve set up a few array’s that refer to a state that could either be 1 or 0.

I want to use if statements to make them progress further. So A (random 1 or 0) → A1 goes to state D1 and D1 goes to state A0 (example). Whenever I want to use the array to specify this, it referes to int not matching with boolean. I’m a bit lost, below my code.

int A1 = 1; //values for the states.  
int A0 = 0;  
int B1 = 1;  
int B0 = 0;  
int C1 = 1;  
int C0 = 0;  
int D1 = 1;  
int D0 = 0;

//READ, REWRITE, MOVE

int[] A = {A0, A1}; //array with the values A0 and A1  
int[] B = {B0, B1}; //array with the values B0 and B1  
int[] C = {C0, C1}; //array with the values C0 and C1  
int[] D = {D0, D1}; //array with the values D0 and D1

void setup() {

int randomA = (int)random(A.length); // choose a random number from the A possibilities.  
println(A[randomA]);

int randomB = (int)random(B.length); // choose a random number from the B possibilities.  
println(B[randomB]);

int randomC = (int)random(C.length); // choose a random number from the C possibilities.  
println(C[randomC]);

int randomD = (int)random(D.length); // choose a random number from the D possibilities.  
println(D[randomD]);  
}

void draw() {

if (A == 0) {  
//fill (0, 90, 10);  
//rect (25, 25, 25, 25);  
} else if (A == 1) {  
fill (80, 90, 10);  
rect (25, 25, 25, 25);  
}  
}

Thanks for the help!

---

<div class="post-metadata">

### Author: ![dams2007](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dams2007/32/7434_2.png) [@dams2007](https://discourse.processing.org/u/dams2007)
#### Post date: [January 24, 2022, 7:15am UTC](https://discourse.processing.org/t/if-statements-in-array/34670/2 "2022-01-24T07:15:03Z")

</div>

Hi @Jimma, I checked your code and one thing that you missed is that you were not saving the random value of your array in a variable. So I created some for you.

```auto
int A_picked;
int B_picked;
int C_picked;
int D_picked;

```

And in the draw function you were actually comparing a array with a int which is not possible, so I update the array with the new variable I created. Here is the code. I hope it helps.

```auto
int A1 = 1; //values for the states.
int A0 = 0;
int B1 = 1;
int B0 = 0;
int C1 = 1;
int C0 = 0;
int D1 = 1;
int D0 = 0;

//READ, REWRITE, MOVE

int[] A = {A0, A1}; //array with the values A0 and A1
int[] B = {B0, B1}; //array with the values B0 and B1
int[] C = {C0, C1}; //array with the values C0 and C1
int[] D = {D0, D1}; //array with the values D0 and D1

int A_picked;
int B_picked;
int C_picked;
int D_picked;

void setup() {

  int randomA = (int)random(A.length); // choose a random number from the A possibilities.
  A_picked = A[randomA];
  println(A[randomA]);
  
  int randomB = (int)random(B.length); // choose a random number from the B possibilities.
  B_picked = B[randomB];
  println(B[randomB]);
  
  int randomC = (int)random(C.length); // choose a random number from the C possibilities.
  C_picked = C[randomC];
  println(C[randomC]);
  
  int randomD = (int)random(D.length); // choose a random number from the D possibilities.
  D_picked = D[randomD];
  println(D[randomD]);
  
}

void draw() {

  if (A_picked == 0) {
    fill (0, 90, 10);
    rect (25, 25, 25, 25);
  } else if (A_picked == 1) {
    fill (80, 90, 10);
    rect (25, 25, 25, 25);
  }
  
}

```

---

<div class="post-metadata">

### Author: ![Jimma](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jimma](https://discourse.processing.org/u/Jimma)
#### Post date: [February 8, 2022, 1:03pm UTC](https://discourse.processing.org/t/if-statements-in-array/34670/3 "2022-02-08T13:03:26Z")

</div>

Thank you so much for your help! And sorry for the late response. I did not receive a notification unfortunately. This solves my problem of having to create multiple integers for each new state the program executed.

Many thanks!
