# How to define two types of variables in a for-loop

**URL:** <https://discourse.processing.org/t/how-to-define-two-types-of-variables-in-a-for-loop/26414>\
**Category:** Beginners\
**Created:** [December 21, 2020, 10:45am UTC](https://discourse.processing.org/t/how-to-define-two-types-of-variables-in-a-for-loop/26414 "2020-12-21T10:45:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 21, 2020, 10:45am UTC](https://discourse.processing.org/t/how-to-define-two-types-of-variables-in-a-for-loop/26414/1 "2020-12-21T10:45:40Z")

</div>

Hi!  
Today I leared something like this

```auto
size(600, 600);
for (int i = 0, mi = 10, r = 200; i < mi; i++) line(cos(TWO_PI/mi*i)*r+width/2, sin(TWO_PI/mi*i)*r+height/2, cos(TWO_PI/mi*(i+1))*r+width/2, sin(TWO_PI/mi*(i+1))*r+height/2);

```

is possible. But how would I be able to use

```auto
for(int i = 0, r = 10; float c = 3; i < 10; i++) {} 

```

?

is it even possible?

I know I can just use

```auto
float c = 3;
for(int i = 0; i < 10; i++) { }

```

but I want to do it in a single line (while formatting the code with ctrl + t )

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 21, 2020, 12:00pm UTC](https://discourse.processing.org/t/how-to-define-two-types-of-variables-in-a-for-loop/26414/2 "2020-12-21T12:00:59Z")

</div>

I know I can just do

```auto
for(float a = 0, i = 0; i < 10; i++) {
   array[int(a+i)];
}

```

but it is annoying

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [December 21, 2020, 9:01pm UTC](https://discourse.processing.org/t/how-to-define-two-types-of-variables-in-a-for-loop/26414/3 "2020-12-21T21:01:36Z")

</div>

In my experience single line code is limited in what it can do. So likelyhood is this isnt possible. The same thing happens when you shorten conditional statements, it only picks up the first subsequent instruction.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 21, 2020, 9:08pm UTC](https://discourse.processing.org/t/how-to-define-two-types-of-variables-in-a-for-loop/26414/4 "2020-12-21T21:08:12Z")

</div>

but you can still do this:

```auto
if(a) println("a"); else if(b) println("b"); else if(c) println("c"); ...

```

the auto format makes it into several lines but still

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [December 21, 2020, 11:40pm UTC](https://discourse.processing.org/t/how-to-define-two-types-of-variables-in-a-for-loop/26414/5 "2020-12-21T23:40:27Z")

</div>

Yeah but in your example each condition is followed by only one instruction.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 22, 2020, 8:00am UTC](https://discourse.processing.org/t/how-to-define-two-types-of-variables-in-a-for-loop/26414/6 "2020-12-22T08:00:53Z")

</div>

Yeah. But it still is useful sometimes! And you can always use

```auto
for(int i = 0; i < 100; i++) for(int j = 0; j < 100; j++) rect(i*scl,j*scl,scl,scl);

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [December 22, 2020, 8:36am UTC](https://discourse.processing.org/t/how-to-define-two-types-of-variables-in-a-for-loop/26414/7 "2020-12-22T08:36:45Z")

</div>

I know ive thought the same thing, as the code just looks neater, alas its limited in what it can do.

ie this will only complete the first instruction.

```auto
if(true)doSomething(); doSomethingElse(); 

```

The doSomethingElse wont be called, only the first one. As essentially the brackets serve to close of the instruction and tell the program it needs to move on, however without brackets there is nothing to tell the program that the condition should still be valid so it stop after one instruction. Otherwise unintentional behaviours could creep in. At least i think thats why.

---

<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:** [December 22, 2020, 8:47am UTC](https://discourse.processing.org/t/how-to-define-two-types-of-variables-in-a-for-loop/26414/8 "2020-12-22T08:47:18Z")

</div>

> [@CodeMasterX](#):
>
> The auto format makes it into several lines, but still…

In such case a `switch () {}` block would be neater than multiple `if () {}`: 😉

> **[switch / Reference](https://processing.org/reference/switch.html)**
>
> Works like an if else structure, but switch is more convenient when you need to select between three or more alternatives. Program controls jumps to the case with the same value as the e…
