# Any way to use conditional operator in a case statement

**URL:** <https://discourse.processing.org/t/any-way-to-use-conditional-operator-in-a-case-statement/16846>\
**Category:** Beginners\
**Created:** [January 3, 2020, 9:34pm UTC](https://discourse.processing.org/t/any-way-to-use-conditional-operator-in-a-case-statement/16846 "2020-01-03T21:34:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)\
**Post date:** [January 3, 2020, 9:34pm UTC](https://discourse.processing.org/t/any-way-to-use-conditional-operator-in-a-case-statement/16846/1 "2020-01-03T21:34:52Z")

</div>

Is there any way to use a condition such as || or && in a case: statement?

```auto
switch (int) {
  case 0 || 1: 
    println("Not sure, let me check.");
  case 0:
    println("Nope.");
    break;
  case 1:
    println("Yup.");
    break;
}

```

Of course, I saw

> The operator || is undefined for the argument type(s) int, int

but is there some other way to make it happen?

EDIT:  
Below is the example I was actually trying to simplify using OR conditions and switch fall-through.

```auto
    switch (type) {
    case 0:
      // as rectMode CORNER
      minX = x;
      minY = y;
      midX = x + x2 / 2;
      midY = y + y2 / 2;
      maxX = x + x2;
      maxY = y + y2;
      frameWidth = x2;
      frameHeight = y2;
      break;

    case 1:
      // as rectMode CORNERS
      minX = x;
      minY = y;
      midX = (x2 - x) / 2;
      midY = (y2 - y) / 2;
      maxX = x2;
      maxY = y2;
      frameWidth = x2 - x;
      frameHeight = y2 - y;
      break;

    case 2:
      // as rectMode CENTER
      minX = x - x2 / 2;
      minY = y - y2 / 2;
      midX = x;
      midY = y;
      maxX = x + x2 / 2;
      maxY = y + y2 / 2;
      frameWidth = x2;
      frameHeight = y2;
      break;

    case 3:
      // as rectMode RADIUS
      frameWidth = (x2 / 2);
      frameHeight = (y2 / 2);
      minX = x - ((x2 / 2) / 2);
      minY = y - ((y2 / 2) / 2);
      midX = x;
      midY = y;
      maxX = x + ((x2 / 2) / 2);
      maxY = y + ((y2 / 2) / 2);
      break;

    default:
      println("switch default");
      break;
    }

```

But as soon as I use

```auto
case 0:
case 1:
  minX = x;
  minY = y;
  
case 0:
  // as rectMode CORNER
  midX = x + x2 / 2;
  midY = y + y2 / 2;
  maxX = x + x2;
  maxY = y + y2;
  frameWidth = x2;
  frameHeight = y2;
  break;

```

then I get an error about Duplicate Case.

So maybe it’s just not going to be 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:** [January 3, 2020, 9:36pm UTC](https://discourse.processing.org/t/any-way-to-use-conditional-operator-in-a-case-statement/16846/2 "2020-01-03T21:36:19Z")

</div>

> [@ddownn](#):
>
> case 0 || 1:  
> println(“Not sure, let me check.”);  
> case 0:  
> println(“Nope.”);  
> break;

you want:

```auto
 case 0:
 case 1: 
    println("Not sure, let me check.");
    break; 
  case 2:
    println("Nope.");
    break;

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [January 3, 2020, 9:38pm UTC](https://discourse.processing.org/t/any-way-to-use-conditional-operator-in-a-case-statement/16846/3 "2020-01-03T21:38:45Z")

</div>

Some previous ideas from the programming community:

- [https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement%20%20](https://stackoverflow.com/questions/16706716/using-two-values-for-one-switch-case-statement%20%20)
- [https://stackoverflow.com/questions/5086322/java-switch-statement-multiple-cases/39123659](https://stackoverflow.com/questions/5086322/java-switch-statement-multiple-cases/39123659)

Kf

---

<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:** [January 7, 2020, 1:06am UTC](https://discourse.processing.org/t/any-way-to-use-conditional-operator-in-a-case-statement/16846/5 "2020-01-07T01:06:46Z")

</div>

Also, don’t be afraid to mix if() and switch() statements if that is what you mean. Clustering cases is fine, but a series of if and switch statements may be more readable than trying to cram everything into the switch somehow. Or you can do both – combine cases with fall-through on ranges, but otherwise handle more complex checks with pre or post if statements.

```auto
void draw () {
  if(frameCount%61==0) pumpText(int(random(7)));
}
void pumpText(int x) {
  print("\n", x, ": Your tank is ");

  if (x<2 || x==5) print("very ");
  switch(x) {
  case 0:
    print("empty");
    break;
  case 1:
  case 2:
    print("low");
    break;
  case 3:
    print("fine");
    break;
  case 4:
  case 5:
    print("full");
    break;
  default:
    print("overflowing");
  }
  if (x>3) print(" ...so stop pumping");
}

```

> 0 : Your tank is very empty  
> 1 : Your tank is very low  
> 2 : Your tank is low  
> 3 : Your tank is fine  
> 4 : Your tank is full …so stop pumping  
> 5 : Your tank is very full …so stop pumping  
> 6 : Your tank is overflowing …so stop pumping
