# Easier way to write if else statement

**URL:** https://discourse.processing.org/t/easier-way-to-write-if-else-statement/17250
**Category:** Beginners
**Created:** [January 22, 2020, 2:41pm UTC](https://discourse.processing.org/t/easier-way-to-write-if-else-statement/17250 "2020-01-22T14:41:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![MikkelS](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@MikkelS](https://discourse.processing.org/u/MikkelS)
#### Post date: [January 22, 2020, 2:41pm UTC](https://discourse.processing.org/t/easier-way-to-write-if-else-statement/17250/1 "2020-01-22T14:41:34Z")

</div>

Hey, I was just wondering if there exists an easier way to write the following code?

```auto
    if (lives == 3) { //
      image(heart, heartX1, heartY, heartWH, heartWH);
      image(heart, heartX2, heartY, heartWH, heartWH);
      image(heart, heartX3, heartY, heartWH, heartWH);
    } else if (lives == 2) {
      image(heart, heartX1, heartY, heartWH, heartWH);
      image(heart, heartX2, heartY, heartWH, heartWH);
    } else if (lives == 1) {
      image(heart, heartX1, heartY, heartWH, heartWH);
    }

```

---

<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: [January 22, 2020, 2:46pm UTC](https://discourse.processing.org/t/easier-way-to-write-if-else-statement/17250/2 "2020-01-22T14:46:21Z")

</div>

[Processing.org/reference/switch.html](http://Processing.org/reference/switch.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: [January 22, 2020, 2:55pm UTC](https://discourse.processing.org/t/easier-way-to-write-if-else-statement/17250/3 "2020-01-22T14:55:09Z")

</div>

Hello,

and welcome to the forum! Nice to have you here!

The command if() can be replaced with `switch()` (in this case).

**Approach with a For-loop**

In this case: You could also use a for-loop

```auto
for(int i=0; i<=lives; i++) 
    image(heart, (heartX1-15) + i*(heartWH+12) , heartY , heartWH, heartWH);

```

This for-loop

- uses `lives` as upper bound (`i<=lives`), so that the number of loops is determined by the variable lives (how many hearts we see)
- uses i to calculate the x position for each heart: `(heartX1-15) + i*(heartWH+12)`. This can be simplified.

**Remark**

I would recommend to use image only with 3, not with 5 parameters. This slows things down. Avoid this everywhere in your game!

Instead apply `resize()` command once to “heart” in setup().

Chrisir

---

<div class="post-metadata">

### Author: ![MikkelS](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@MikkelS](https://discourse.processing.org/u/MikkelS)
#### Post date: [January 22, 2020, 3:02pm UTC](https://discourse.processing.org/t/easier-way-to-write-if-else-statement/17250/4 "2020-01-22T15:02:05Z")

</div>

Thank you it was the for loop approach i was looking for

---

<div class="post-metadata">

### Author: ![MikkelS](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@MikkelS](https://discourse.processing.org/u/MikkelS)
#### Post date: [January 22, 2020, 3:16pm UTC](https://discourse.processing.org/t/easier-way-to-write-if-else-statement/17250/5 "2020-01-22T15:16:01Z")

</div>

Tried the resize function for my graphic, unfortunately it seems to apply anti-aliasing even though no smooth is on

---

<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: [January 22, 2020, 3:56pm UTC](https://discourse.processing.org/t/easier-way-to-write-if-else-statement/17250/6 "2020-01-22T15:56:51Z")

</div>

Oh, I didn’t know that.  
Still image() with 5 parameters is much slower.

Maybe you can copy images and change in external graphics program

---

<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: [February 2, 2020, 9:44pm UTC](https://discourse.processing.org/t/easier-way-to-write-if-else-statement/17250/7 "2020-02-02T21:44:09Z")

</div>

Hello,

Welcome to the Processing forum.

This is my first pass at this:

```auto
int [] x = {100, 200, 300};
int [] y = {300, 200, 100};

int lives = 3;
 
 for (int i = 0; i < lives; i++)
    {
    xl = x[i];
    yl = y[i];
    image(image1, xl, yl);
    }

```

(xl, yl) can be anything you want; I put data into an array for testing only.

Update: I did not realize I was answering this so late; I do not always look at other posts as this influences my “first pass” coding efforts. I will leave it…
