# "if" doesn't work while "switch" does

**URL:** <https://discourse.processing.org/t/if-doesnt-work-while-switch-does/36826>\
**Category:** Beginners\
**Created:** [May 11, 2022, 4:45pm UTC](https://discourse.processing.org/t/if-doesnt-work-while-switch-does/36826 "2022-05-11T16:45:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sadPG](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@sadPG](https://discourse.processing.org/u/sadPG)\
**Post date:** [May 11, 2022, 4:45pm UTC](https://discourse.processing.org/t/if-doesnt-work-while-switch-does/36826/1 "2022-05-11T16:45:51Z")

</div>

```auto
void setup(){
  
  String a = new String();
//a = "f*";
//a += "**";
  a+='f';
  a+='*';
  a+='*';
  a+='*';

  
  if(a == "f***"){
    println("hooray!");
  }else{
    println("f***");
  }
  
  switch(a){
    case "f***":
      println("why...");
      break;
  }
  
}

```

this problem occurs when i add string or char to String

maybe the problem is that I’m using win11  
but if you have something that comes to your mind, let me know it

---

<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 11, 2022, 5:19pm UTC](https://discourse.processing.org/t/if-doesnt-work-while-switch-does/36826/2 "2022-05-11T17:19:44Z")

</div>

`switch () / case:` blocks invoke **equals()** internally when dealing w/ String types.

You should do the same everywhere else:

> **[equals() / Reference](https://processing.org/reference/String_equals_.html)**
>
> Compares two strings to see if they are the same. This method is necessary because it's not possible to compare strings using the equality operator (==). Returns true if the strings are the sam…

> [@sadPG](#):
>
> This problem occurs when I add String or `char` to String.

- String is an immutable datatype.
- Each time you append a character to it another String object is created.
- When we use the [equality](https://processing.org/reference/equality.html) `==` operator we’re comparing if both String operands are the same object, not if they have the same content.
- Java caches String literals.
- That’s why we can use the [equality](https://processing.org/reference/equality.html) `==` operator on literals.
- Try out `println(P3D == OPENGL);` on the PDE.
- It outputs `true` b/c both constants hold the same String literal object.
- Another example: `println(platformNames[3] == "linux");`
- However avoid using `==` or `!=` for comparing strings b/c they’ll fail outside literal comparing cases.
- Just use method **equals()** for 100% success: `println("windows".equals(platformNames[1])); // outputs 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 12, 2022, 12:40pm UTC](https://discourse.processing.org/t/if-doesnt-work-while-switch-does/36826/3 "2022-05-12T12:40:24Z")

</div>

in short:  
**The == operator compares _references_ not values.**

---

<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 12, 2022, 12:41pm UTC](https://discourse.processing.org/t/if-doesnt-work-while-switch-does/36826/4 "2022-05-12T12:41:49Z")

</div>

String doesn’t work with `==`  
better use `equals()` : `if (myString.equals("Hello")) ....`

`switch() {.....}` works with `String`

---

<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 13, 2022, 11:27am UTC](https://discourse.processing.org/t/if-doesnt-work-while-switch-does/36826/5 "2022-05-13T11:27:08Z")

</div>

Hello,

Processing related references:

> **[Reference](https://processing.org/reference/String.html)**
>
> A string is a sequence of characters. The class String includes methods for examining individual characters, comparing strings, searching strings, extracting parts of strings, and for convertin…

Above links to:

> **[Troubleshooting · processing/processing Wiki](https://github.com/processing/processing/wiki/Troubleshooting#why-dont-these-strings-equal)**
>
> Source code for the Processing Core and Development Environment (PDE) - Troubleshooting · processing/processing Wiki

`:)`
