# Possible to make a lot of statements into one?

**URL:** <https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904>\
**Category:** Coding Questions\
**Created:** [May 3, 2023, 8:17pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904 "2023-05-03T20:17:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [May 3, 2023, 8:17pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/1 "2023-05-03T20:17:19Z")

</div>

Can i replace this, with something easier? mabye

```auto
terrainBase[playerxpos+ix-1][playerypos+iy] == 0
&& terrainBase[playerxpos+ix-1][playerypos+iy] == 3
&& terrainBase[playerxpos+ix-1][playerypos+iy] == 4
&& terrainBase[playerxpos+ix-1][playerypos+iy] == 9

```

to

```auto
"0 3 4 9".indexOf(terrainBase[playerxpos+ix-1][playerypos+iy]) >= 1;

```

it this possible?

---

<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, 2023, 8:20pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/2 "2023-05-03T20:20:31Z")

</div>

imho it cannot be all of it at the same time.

Do you mean logical or || instead of the  
logical and &&?

---

<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, 2023, 8:23pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/3 "2023-05-03T20:23:41Z")

</div>

But it’s possible to connect it as you suggested. Maybe convert to String

Or try [hasValue() / Reference / Processing.org](https://processing.org/reference/IntList_hasValue_.html)

When it’s an int

Or [hasValue() / Reference / Processing.org](https://processing.org/reference/FloatList_hasValue_.html)

for float

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [May 3, 2023, 8:38pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/4 "2023-05-03T20:38:40Z")

</div>

Hi @Aryszin,

You mean || instead of && ?  
The below is always false, as it can’t be all true.

> [@Aryszin](#):
>
> ```auto
> terrainBase[playerxpos+ix-1][playerypos+iy] == 0
> && terrainBase[playerxpos+ix-1][playerypos+iy] == 3
> && terrainBase[playerxpos+ix-1][playerypos+iy] == 4
> && terrainBase[playerxpos+ix-1][playerypos+iy] == 9
> 
> ```

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [May 3, 2023, 8:41pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/5 "2023-05-03T20:41:35Z")

</div>

no i mean i know there is “.indexOf(” but does it work with itegers?

---

<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, 2023, 8:43pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/6 "2023-05-03T20:43:02Z")

</div>

You can convert to String with str()

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [May 3, 2023, 8:45pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/7 "2023-05-03T20:45:08Z")

</div>

You can do s.th like…

```auto
if (Arrays.asList(0, 3, 4, 9).contains(terrainBase[playerxpos+ix-1][playerypos+iy])) {
    // do something if the value is valid
} else {
    // do something if the value is not valid
}

```

Alternatively, if you need to know what …

```auto
switch (terrainBase[playerxpos+ix-1][playerypos+iy]) {
    case 0:
        // do something if 0
        break;
    case 3:
        // do something if 3
        break;
    case 4:
    case 9:
        // do something if 4 or 9
        break;
    default:
        // do s.th if nothing from above
        break;
}

```

---

<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, 2023, 9:00pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/8 "2023-05-03T21:00:58Z")

</div>

> [@mnse](#):
>
> `terrainBase`

Alternatively you can make this from int to a class Cell and store there whether it’s air or not

(Maybe you can even say if ( ! terrain … == 0 ) …

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [May 3, 2023, 9:03pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/9 "2023-05-03T21:03:40Z")

</div>

One way would be to sort your blocks so that the ones you can collide against (if that’s what you are testing for) are all consecutive so you could just check if `terrainBase[] < 5` for example.

Another very fast mechanism is to use a bit-field. If you have fewer than 32 different blocks, you can store the block type as an integer with only one bit set for each block, so as powers of 2. For example, air would be 1, dirt is 2, wood is 4, stone is 8, and so on. `(terrainBase[] & 6) > 0` if the block is either dirt or wood since 2 + 4 = 6.

You can make it cleaner with some constants:

```auto
final int AIR = 1;
final int DIRT = 2;
final int WOOD = 4;
final int STONE = 8;

int x = WOOD;

println( (x & (DIRT | WOOD | STONE)) > 0 );

```

If you have between 32 and 64 block types, you could use a 64-bit long integer.

For the larger numbers you can write (1 \<\< 15), for instance which is 1 left-shifted 15 places = 2^15 = 32768. e.g.:

```auto
final int LAVA = 1 << 15;

```

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [May 3, 2023, 9:47pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/10 "2023-05-03T21:47:29Z")

</div>

A better way to organize it might be to create a bit-field of properties for each block type. Lets say you want to test blocks for “fall through”, “stand on”, “does damage”, “slippery”, and so on. Let the block types be consecutive numbers and make an array of block properties of type int.

```auto
final int FALL_THROUGH = 1 << 0;
final int STAND_ON = 1 << 1;
final int DOES_DAMAGE = 1 << 2;
final int SLIPPERY = 1 << 3;

int[] block_properties = {
  /* AIR */ FALL_THROUGH,
  /* LAVA */ FALL_THROUGH | DOES_DAMAGE,
  /* ICE */ STAND_ON | SLIPPERY
};

// somewhere deep in your code:

int blockId = terrainBase[playerPos];
if( block_properties[blockId] & DOES_DAMAGE ) heath--;

```

This way you can easily add new block types and give them the appropriate properties without having to run through your code to add them to the if statements for their behavior. You could add a red lava block that is cool enough you can STAND\_ON it, but still hot enough that it DOES\_DAMAGE. Add it to the block list and properties and no need to change your code.

---

<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, 2023, 10:12pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/11 "2023-05-03T22:12:42Z")

</div>

[“Random Tiled Terrain Gen”](http://studio.processingtogether.com/sp/pad/export/ro.9Ql2E8JBC54LJ):

https://OpenProcessing.org/sketch/436477/embed/

---

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [May 3, 2023, 10:24pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/12 "2023-05-03T22:24:47Z")

</div>

i mean or. im sorry.

---

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [May 3, 2023, 11:33pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/13 "2023-05-03T23:33:40Z")

</div>

FIX: 😊

```auto
String stuff = "0 5 9 12 182";

for(int i = 0; i < 512; i++) {
   if(stuff.indexOf(str()) => 0) {
      //do stuff that only happens for the numbers in stuff
   }
}

```

---

<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 4, 2023, 6:24am UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/14 "2023-05-04T06:24:55Z")

</div>

In this approach you don’t need a for loop.

It’s \>=

Also, str() won’t work.

Or you posted this as an example, right?

---

<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 4, 2023, 8:25am UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/15 "2023-05-04T08:25:22Z")

</div>

this works

```auto

int[] [] terrainBase = new int [1][1];

String stuff = "0 5 9 12 182";

terrainBase [0][0] = 0;

if (stuff.indexOf(str(terrainBase [0][0])) >= 0) {
  //do stuff that only happens for the numbers in stuff
  println("yes");
} else {
  println("no");
}

```

or as a function

```auto

// MAP
int[][] terrainBase = new int[1][1];

String typesOfCellThatAreNotAir = "0 5 9 12 182";

void setup() {

  terrainBase [0][0] = 0;

  if (isAir(0, 0)) {
    println("yes");
  } else {
    println("no");
  }
}

boolean isAir(int x, int y) {
  println (str(terrainBase[x][y]));
  return
    typesOfCellThatAreNotAir.indexOf(str(terrainBase[x][y])) >= 0; // returns true only when the map cell is in the numbers in typesOfCellThatAreNotAir
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 4, 2023, 10:28am UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/16 "2023-05-04T10:28:04Z")

</div>

Hello @Aryszin,

Scrutinize this and output:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/d/6db11856321fbc277501b7fb35b4afa4a977d1c7.png)

It also includes _1, 2, 18_ and _82_.

It may work in your specific project but not as a generic example for every case.

`:)`

---

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [May 6, 2023, 10:33am UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/17 "2023-05-06T10:33:00Z")

</div>

I have solved it: 😆  
If someone needs it just copy it. (obviously)

```auto
String objectsToSearchFor = "1>2>6>12>65>"; //any other symbol than ">" works
boolean hasOneOfTheSearched;

for(int ix = 0; ix < 64; i++) {
   if (!(objectsToSearchFor.indexOf(str(someArray)+">") >= 0)) {
      //here change the ">" to the symbol that is behind every number in the String
      hasOneOfTheSearched = true;
   }
}

```

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [May 6, 2023, 11:12am UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/18 "2023-05-06T11:12:42Z")

</div>

Hi @Aryszin,

the issue @glv pointed out is still not solved by that as it still matches `5>`, and also `2>` at`12>`, but latter is luckily also one you want to search on your search pattern… 😉

Actually I really not understand why you convert to string and searched based on string if you just want to search for some int values and not simply do…

```auto
if (Arrays.asList(1, 2, 6, 12, 65).contains(someArrayElement) {
    // do something if one of searched values
} else {
    // do something else or nothing
}

```

But nevertheless, seems you feel comfortable with your approach, but imo it could be made better…

As a (good) programmer you should strive for almost clean and stable algorithms 🙂

The consequences of getting too lazy is that you run into more confusions the more complexer your tasks are getting…

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [May 7, 2023, 12:24pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/19 "2023-05-07T12:24:32Z")

</div>

couldnt you add “\<83\> \<8\> \<3\>” to the string?

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [May 7, 2023, 5:15pm UTC](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904/20 "2023-05-07T17:15:23Z")

</div>

You only need to put spaces around the numbers as long as you include one at both the start and end of your list. So " 83 8 3 " and then search for " 8 ".

But please don’t. Converting back and forth from integers to strings is just needlessly slow. Just keep things as integers as @mnse suggested and let Java find the integer elements within the array.

Or better yet, use the constant-time solution. Make an array of booleans, all false except true for the ones you want to match and just check:

```auto
boolean[] isSolidBlock = { false, false, true, true, false, ... };

if( isSolidBlock[terrainBase[j][i] ] ) {
  // bounce off of block
}

```

[Next page](https://discourse.processing.org/t/possible-to-make-a-lot-of-statements-into-one/41904.md?page=2)
