# \[SOLVED\] A good way to compress nested "if else" statements?

**URL:** https://discourse.processing.org/t/solved-a-good-way-to-compress-nested-if-else-statements/8419
**Category:** Beginners
**Created:** [February 15, 2019, 2:16pm UTC](https://discourse.processing.org/t/solved-a-good-way-to-compress-nested-if-else-statements/8419 "2019-02-15T14:16:54Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![kandles11](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kandles11/32/3645_2.png) [@kandles11](https://discourse.processing.org/u/kandles11)
#### Post date: [February 15, 2019, 2:16pm UTC](https://discourse.processing.org/t/solved-a-good-way-to-compress-nested-if-else-statements/8419/1 "2019-02-15T14:16:54Z")

</div>

hey yall, i have a long script that lets a user choose a team once they enter a set of characters. it goes through a long list of if else statements in order to choose it. are there any other commands that would be more fitting? Here is my current code:

```auto

  if (finalChar.equals("aa") == true) {
    r = 151;
    g = 35;
    b = 63;
    title = "Arizona Cardinals Time!";
    imageSelected = "cardinals";
  } else {
    if (finalChar.equals("ab") == true) {
      r = 151;
      g = 35;
      b = 63;
      title = "Atlanta Falcons Time!";
      imageSelected = "falcons";
    } else {
      if (finalChar.equals("ac") == true) {
        r = 26;
        g = 25;
        b = 95;
        title = "Baltimore Ravens Time!";
        imageSelected = "ravens";
      } else {
        if (finalChar.equals("ad") == true) {
          r = 0;
          g = 51;
          b = 141;
          rh = 198;
          gh = 12;
          bh = 48;
          title = "Buffalo Bills Time!";
          imageSelected = "bills";
        } else {

```

any help will be appreciated 🙂

EDIT: i forgot to mention, but the code continues to go on, and it repeats over and over about 30 more times. sorry lol

---

<div class="post-metadata">

### Author: ![bryan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bryan/32/2538_2.png) [@bryan](https://discourse.processing.org/u/bryan)
#### Post date: [February 15, 2019, 2:59pm UTC](https://discourse.processing.org/t/solved-a-good-way-to-compress-nested-if-else-statements/8419/2 "2019-02-15T14:59:05Z")

</div>

Use the function switch instead.  
[https://processing.org/reference/switch.html](https://processing.org/reference/switch.html)

---

<div class="post-metadata">

### Author: ![kandles11](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kandles11/32/3645_2.png) [@kandles11](https://discourse.processing.org/u/kandles11)
#### Post date: [February 15, 2019, 3:01pm UTC](https://discourse.processing.org/t/solved-a-good-way-to-compress-nested-if-else-statements/8419/4 "2019-02-15T15:01:15Z")

</div>

@bryan does the switch function work with strings? it says it only works with primative data types.

---

<div class="post-metadata">

### Author: ![bryan](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bryan/32/2538_2.png) [@bryan](https://discourse.processing.org/u/bryan)
#### Post date: [February 15, 2019, 3:36pm UTC](https://discourse.processing.org/t/solved-a-good-way-to-compress-nested-if-else-statements/8419/5 "2019-02-15T15:36:48Z")

</div>

Oh my bad, i thought that string were supported in the switch statements but it looks like no.  
I was reading this thread : [https://forum.processing.org/two/discussion/15489/string-comparison-else-if-or-switch](https://forum.processing.org/two/discussion/15489/string-comparison-else-if-or-switch) . and they mention that you can use a hashMap to assign an index to every string. [https://processing.org/reference/HashMap.html](https://processing.org/reference/HashMap.html)

At the end, i don’t know if this will be more troublesome rather than use the if else statement.

---

<div class="post-metadata">

### Author: ![kandles11](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kandles11/32/3645_2.png) [@kandles11](https://discourse.processing.org/u/kandles11)
#### Post date: [February 15, 2019, 4:08pm UTC](https://discourse.processing.org/t/solved-a-good-way-to-compress-nested-if-else-statements/8419/6 "2019-02-15T16:08:58Z")

</div>

@bryan this looks like it might work! thank you so much. imma mark it as solved. thank you 🙂

---

<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: [February 15, 2019, 5:49pm UTC](https://discourse.processing.org/t/solved-a-good-way-to-compress-nested-if-else-statements/8419/7 "2019-02-15T17:49:09Z")

</div>

This would be far better done with a table lookup rather than a long code string of tests. Either use a simple indexed array or, if you need your string lookup, use a HashMap.

---

<div class="post-metadata">

### Author: ![Nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nathan](https://discourse.processing.org/u/Nathan)
#### Post date: [February 18, 2019, 5:39pm UTC](https://discourse.processing.org/t/solved-a-good-way-to-compress-nested-if-else-statements/8419/8 "2019-02-18T17:39:16Z")

</div>

I concur with @bryan and @scudly that a lookup table is probably the best solution here.

Although for the example cases a little reorganization can simplify things somewhat. If you always check the last two characters, then you can test the length, then extract the last two characters, check the first with if/elseif and nest switch statements. It’s not vastly different, but adds less if/elseif visual mess and so is potentialyl slightly more readable.

```auto
String str = "vlad";

if( str.length() >= 2) {
    final char one = str.charAt( str.length() - 2 );
    final char two = str.charAt( str.length() - 1 );
    
    if( one == 'a' ) {
        switch(two) {
            case 'a': break;
            case 'b': break;
            case 'c': break;
            case 'd': break;
            default: break;
        }
    }
}

```

As an addendum though, if anyone is still wondering, Java does support String “constants” for switch cases as of Java/JDK 7 (aka 1.7). Also, although Java String variables are technically immutable, you must either use final variables or quoted strings (e.g. “testcase”) because they are re-assignable.

Taking advantage of that, a more straightforward switch block can be devised.

```auto
switch(test) {
    case "aa": doStuff(); break;
    case "ab": doStuff(); break;
    case "ac": doStuff(); break;
    case "ad": doStuff(); break;
    //....
    default: break;
}

```

---

<div class="post-metadata">

### Author: ![kandles11](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kandles11/32/3645_2.png) [@kandles11](https://discourse.processing.org/u/kandles11)
#### Post date: [February 19, 2019, 2:27pm UTC](https://discourse.processing.org/t/solved-a-good-way-to-compress-nested-if-else-statements/8419/9 "2019-02-19T14:27:43Z")

</div>

ok, so i spent some time implementing this, and it worked perfectly. it does exactly what we wanted it to do. thank you so much!!

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [February 21, 2019, 4:36am UTC](https://discourse.processing.org/t/solved-a-good-way-to-compress-nested-if-else-statements/8419/10 "2019-02-21T04:36:36Z")

</div>

Another approach is to use objects / classes.

```auto
class Team {
  String code;
  int r, g, b;
  String title;
  String image;
  Team(String code, int r, int g, int b, String title, String image){
    this.code = code;
    this.r = r;
    this.g = g;
    this.b = b;
    this.title = title;
    this.image = image;
  }
}

```

Then you can add create class objects and add them to a hashmap

[https://processing.org/reference/HashMap.html](https://processing.org/reference/HashMap.html)

like this:

```
HashMap<String,Team> hm = new HashMap<String,Team>();
Team tm = new Team("ad", 0, 51, 141, "Buffalo Bills Time", "bills");
hm.put("ad", tm);
// get the team later
Team selected = hm.get(finalChar); // "ad"
println(selected.title); // prints "Buffalo Bills Time"
```
