# Conditional inside an IF

**URL:** <https://discourse.processing.org/t/conditional-inside-an-if/10790>\
**Category:** Coding Questions\
**Created:** [April 30, 2019, 5:33pm UTC](https://discourse.processing.org/t/conditional-inside-an-if/10790 "2019-04-30T17:33:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaps421](https://avatars.discourse-cdn.com/v4/letter/c/ed8c4c/32.png) [@chaps421](https://discourse.processing.org/u/chaps421)\
**Post date:** [April 30, 2019, 5:33pm UTC](https://discourse.processing.org/t/conditional-inside-an-if/10790/1 "2019-04-30T17:33:31Z")

</div>

is there a way to put a conditional inside an of stament? for

```auto

for (int i=0; i<10; i++){
  if(i==col(i).size ){//col1 or col2... col 9 depending on the i

    //whatever you want
 
  }
}

```

what i am currently using is this a separate void that gets the size in a global variable and then use that variable.

i use a void because is a preces that i repet a lot. but i would like to avoid use it and insert the conditional(?) in the if statement

```auto

//create my void for choosing which
int size;
void cols(int col) {                                  
  size=(col==1 ? col1:
        col==2 ? col2:
        col==3 ? col3:col4).size() //each list has a diferent size

}

for (int i=0;i<4;i++)
  cols(i)
  if(i==size){
//whatever i want to do
  }
}

```

to resume i want these conditional inside an if statment but it doesnt recognise it as an integer.

```auto
(col==1 ? col1: col==2 ? col2: col==3 ? col3: col4).size()

```

---

<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:** [April 30, 2019, 5:48pm UTC](https://discourse.processing.org/t/conditional-inside-an-if/10790/2 "2019-04-30T17:48:01Z")

</div>

[Processing.org/reference/switch.html](http://Processing.org/reference/switch.html)

---

<div class="post-metadata">

**Author:** ![chaps421](https://avatars.discourse-cdn.com/v4/letter/c/ed8c4c/32.png) [@chaps421](https://discourse.processing.org/u/chaps421)\
**Post date:** [May 3, 2019, 12:51pm UTC](https://discourse.processing.org/t/conditional-inside-an-if/10790/3 "2019-05-03T12:51:09Z")

</div>

it doesn´t do what i want.  
what i need is to repet the same proces with diferent variables that are called similar, but i dont want to write massive blocks of code repiting the same proces again and again, insted, i want to kind of “automatise” it.

---

<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:** [May 3, 2019, 12:57pm UTC](https://discourse.processing.org/t/conditional-inside-an-if/10790/4 "2019-05-03T12:57:40Z")

</div>

Every time we have a collection of variables having almost same name & datatype like: 🤖  
_col_, _col1_, _col2_, _col3_, etc.

We should turn them into 1 array: _cols[]_. So we can use a loop to iterate over them: 💡  
[Processing.org/reference/for.html](http://Processing.org/reference/for.html)

---

<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:** [May 3, 2019, 2:15pm UTC](https://discourse.processing.org/t/conditional-inside-an-if/10790/5 "2019-05-03T14:15:49Z")

</div>

> [@chaps421](#):
>
> what i am currently using is this a separate void that gets the size in a global variable and then use that variable.

**Step 1**

(Please don’t say `void`, it’s a function. `void` is just its return type.)

It’s actually good to use a function, because it puts your code into separate sections (functions) which is good. This is also one purpose of OOP (see [Objects / Processing.org](https://www.processing.org/tutorials/objects/)).

instead of `void cols(int col) {` you can use your function like this:

```auto
int cols(int col) {
   int colsLocal=0;
   .....
   return colsLocal;
}

```

And then call the function like: a = cols (i); or so.

**Step 2**

It’s not fully clear what you want to achieve. You have an int A and depending on A you want to get a int value B. But B is not exactly A but another value.

I think to use switch or an array is a good idea. Also map() command is similar.

What I like to mention is an array of the values B and this array uses A as an index:

```auto
int[] B_values = { 17, 27, 312, 3455 }; // B 
int i=2; // A

println ( B_values [i] ); // using A or i as an index to receive B

```

**Step 3**

Another way of doing this would be an `HashMap` :

(This example is String → int but we could also use int → int)

```auto

// This example is String -> int but we could also use int -> int 

// https://www.processing.org/reference/HashMap.html 

// Note the HashMap's "key" is a String and "value" is an Integer
HashMap<String, Integer> hm = new HashMap<String, Integer>();

// Putting key-value pairs in the HashMap
hm.put("Ava", 1);
hm.put("Cait", 35);
hm.put("Casey", 36);

// We can access values by their key
int val = hm.get("Casey");
println("Casey is " + val);

```

Chrisir
