# Excecute multiple arrays in one line?

**URL:** <https://discourse.processing.org/t/excecute-multiple-arrays-in-one-line/4137>\
**Category:** Beginners\
**Created:** [October 4, 2018, 6:35pm UTC](https://discourse.processing.org/t/excecute-multiple-arrays-in-one-line/4137 "2018-10-04T18:35:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [October 4, 2018, 6:35pm UTC](https://discourse.processing.org/t/excecute-multiple-arrays-in-one-line/4137/1 "2018-10-04T18:35:48Z")

</div>

Okay I know this probably sounds confusing and it’s most likely impossible, but I want to know if theres a way to tell Processing to do an action with all the different variables in one array. Like that: example[0 to 4] = 999; I’m new, and was hoping to be able to save some space with that. 😃

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [October 4, 2018, 6:38pm UTC](https://discourse.processing.org/t/excecute-multiple-arrays-in-one-line/4137/2 "2018-10-04T18:38:23Z")

</div>

That’s actually a thing in Python, but the closest thing you’re going to get in Java is a for-each loop: [https://www.geeksforgeeks.org/for-each-loop-in-java/](https://www.geeksforgeeks.org/for-each-loop-in-java/)

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [October 4, 2018, 6:45pm UTC](https://discourse.processing.org/t/excecute-multiple-arrays-in-one-line/4137/3 "2018-10-04T18:45:13Z")

</div>

Or if you don’t want to deal with all the elements, a simple for loop will do.

```auto
for (int i = 0; i < 4; i++) {
  example[i] = 999;
}

```

---

<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:** [October 4, 2018, 8:55pm UTC](https://discourse.processing.org/t/excecute-multiple-arrays-in-one-line/4137/4 "2018-10-04T20:55:46Z")

</div>

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#fill(int[],int,int,int)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#fill(int%5B%5D,int,int,int))

```auto
final int[] example = new int[10]; // 10 indexed integer values

void setup() {
  java.util.Arrays.fill(example, 0, 4, 999); // assign value 999 to indices 0 to 3
  println(example);
  exit();
}

```

---

<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:** [October 4, 2018, 9:06pm UTC](https://discourse.processing.org/t/excecute-multiple-arrays-in-one-line/4137/5 "2018-10-04T21:06:53Z")

</div>

@jb4x’s `for ( ; ; )` loop example can be turned into 1 single statement too: 😇

```auto
final int[] example = new int[10]; // 10 indexed integer values

void setup() {
  //java.util.Arrays.fill(example, 0, 4, 999); // assign value 999 to indices 0 to 3
  for (int i = 0; i < 4; example[i++] = 999); // assign value 999 to indices 0 to 3
  println(example);
  exit();
}

```

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [October 4, 2018, 10:20pm UTC](https://discourse.processing.org/t/excecute-multiple-arrays-in-one-line/4137/6 "2018-10-04T22:20:33Z")

</div>

What! didn’t know that!

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [October 5, 2018, 4:52am UTC](https://discourse.processing.org/t/excecute-multiple-arrays-in-one-line/4137/7 "2018-10-05T04:52:30Z")

</div>

Thanks for the tip!

I sometimes use it in if statement like so:

```auto
If ((myVar = fetchData()) = 5) {} 

```

But I never thought of doing it on a for loop.

And I also realize how off my answer was since he asked for a 1 line statement…

Shouldn’t it be i++ - 1 though? I can’t try now…

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [October 5, 2018, 1:36pm UTC](https://discourse.processing.org/t/excecute-multiple-arrays-in-one-line/4137/8 "2018-10-05T13:36:02Z")

</div>

Thanks, I never thought of using a for loop 😃

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [October 5, 2018, 1:37pm UTC](https://discourse.processing.org/t/excecute-multiple-arrays-in-one-line/4137/9 "2018-10-05T13:37:03Z")

</div>

Thanks as well! 😃

---

<div class="post-metadata">

**Author:** ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)\
**Post date:** [October 5, 2018, 2:00pm UTC](https://discourse.processing.org/t/excecute-multiple-arrays-in-one-line/4137/10 "2018-10-05T14:00:48Z")

</div>

> [@jb4x](#):
>
> Shouldn’t it be i++ - 1 though? I can’t try now…

i++ evalutes to i and then increments i up by 1.

So

```auto
let i = 0;
console.log(i++);
console.log(i++);
console.log(i);

```

Will output

```auto
0
1
2

```

To make it increment up by 1 and then evaluate to the old i + 1, use ++i.

There is a good explaination in [30secondsofinterviews.org](http://30secondsofinterviews.org).

---

<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:** [October 5, 2018, 2:35pm UTC](https://discourse.processing.org/t/excecute-multiple-arrays-in-one-line/4137/11 "2018-10-05T14:35:11Z")

</div>

> [@TheWizardBear](#):
>
> `i++` evaluates to _i_ and then increments _i_ up by `1`.

I’ve heard somewhere that in the actual internal implementation for the 2 postfix unary operators (`var++` & `var--`), they 1st cache the variable’s current value before updating it. And then they return that cached original value.

It’s very common to state that the postfix increment unary operator `var++` corresponds to `var = var + 1` or to `var += 1`.

However, that’s misguidingly wrong! B/c they correspond to the prefix increment unary operator `++var` instead, if they’re used inside expressions!
