# Enhanced For Loops and selecting every nth

**URL:** https://discourse.processing.org/t/enhanced-for-loops-and-selecting-every-nth/3593
**Category:** Coding Questions
**Created:** [September 17, 2018, 7:42am UTC](https://discourse.processing.org/t/enhanced-for-loops-and-selecting-every-nth/3593 "2018-09-17T07:42:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![omerpekin](https://avatars.discourse-cdn.com/v4/letter/o/7ab992/32.png) [@omerpekin](https://discourse.processing.org/u/omerpekin)
#### Post date: [September 17, 2018, 7:42am UTC](https://discourse.processing.org/t/enhanced-for-loops-and-selecting-every-nth/3593/1 "2018-09-17T07:42:24Z")

</div>

Hi,

I have a simple question about enhanced for loops.

I created a new arraylist and filled them with objects. and now I am trying to select every nth object. I tried to use:

`if(arraylist.size() % 2 == 0) {}`

but it doesn’t work. What should I do for selecting every nth object in that arraylist, inside an enhanced for loop?

```auto
cells = new ArrayList <Cell>();
createCells(cellSize);

void setup() {
  size(1920, 960); 
  rectMode(CENTER);
  cells = new ArrayList <Cell>();
  createCells(cellSize);
}

void draw() {
  for ( Cell c : cells) {

    if (cells.size() % 2 == 0) {

      // do struff here
    }
  }
} 

void createCells (float size) {

  for ( int i = 0; i<height; i++) {
    for (int j = 0; j<width; j++) {
      if ( i% size == 0) {
        if (j% size == 0) {
          float cellOpacity = random(20);
          float cellRotation = random(360);
          cells.add(new Cell( j, i, size, cellOpacity, cellRotation));
        }
      }
    }
  }
}

class Cell {
  float x, y, scale;
  float opacity;
  float rotation;

  Cell( float x, float y, float scale, float opacity, float rotation) {
    this.x = x;
    this.y = y;
    this.scale = scale;
    this.opacity = opacity;
    this.rotation = rotation;
  }
}

```

---

<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: [September 17, 2018, 7:52am UTC](https://discourse.processing.org/t/enhanced-for-loops-and-selecting-every-nth/3593/2 "2018-09-17T07:52:55Z")

</div>

A container’s **size()** doesn’t change just by iterating over it alone. 🙄

Either use a regular `for ( ; ; )` loop or declare some counting variable: 😉

```auto
int count = 0;

for (final Cell c : cells) {
  if ((count++ & 1) == 0) {
  }
}

```

And given you’re simply alternating the decision about doing the stuff for each iteration, you can also use a `boolean` variable too: 🎲

```auto
boolean doIt = false;

for (final Cell c : cells) {
  if (doIt ^= true) {
  }
}

```

---

<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: [September 17, 2018, 8:00am UTC](https://discourse.processing.org/t/enhanced-for-loops-and-selecting-every-nth/3593/3 "2018-09-17T08:00:14Z")

</div>

For performance reasons, the regular loop version would be better: 🏎

```auto
for (int len = cells.size(), i = 0; i < len; i += 2) {
  final Cell c = cells.get(i);
}

```

The `if ()` condition can be now omitted in this case, given the iterator _i_ is already being increased by `2`. 🤩

---

<div class="post-metadata">

### Author: ![omerpekin](https://avatars.discourse-cdn.com/v4/letter/o/7ab992/32.png) [@omerpekin](https://discourse.processing.org/u/omerpekin)
#### Post date: [September 17, 2018, 8:10am UTC](https://discourse.processing.org/t/enhanced-for-loops-and-selecting-every-nth/3593/4 "2018-09-17T08:10:24Z")

</div>

hi! thank you very much for many different answers 🙂

I think I understand all of them.

Since I have an additional for loop in the `draw()` section I think I will use the boolean method. 3 for-loops will make it really slow, or at least I think it will make it slow 🙂

I didn’t know this method?

```auto
  if (doIt ^= true) {
  }

```

---

<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: [September 17, 2018, 8:15am UTC](https://discourse.processing.org/t/enhanced-for-loops-and-selecting-every-nth/3593/5 "2018-09-17T08:15:01Z")

</div>

The expression `doIt ^= true` is the same as `doIt = !doIt`: 🤓  
[Processing.org/reference/logicalNOT.html](http://Processing.org/reference/logicalNOT.html)

And `(count++ & 1) == 0` is the same as `count++ % 2 == 0`: 😇  
[Processing.org/reference/bitwiseAND.html](http://Processing.org/reference/bitwiseAND.html)
