Any way to use conditional operator in a case statement

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

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.

    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

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.

1 Like

you want:

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

Some previous ideas from the programming community:

Kf

2 Likes

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.

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

3 Likes