Why switch statements continue after a case

After evaluating the case in the switch statement in Java (and I'm sure other languages), the following case is also evaluated if a control statement such as break or return is not used.

I understand that this is probably an implementation detail, but what are the reasons / reasons why this happens?

Thank!

+3
source share
5 answers

Because it’s useful to “fail” from one case to another. If you do not need it (as is often the case), a simple one will breakprevent it. On the other hand, if the case did not go by default, there would be no easy way to do this.

+1

.

void measureCPD (void) {
char setting;
  switch (DrawerType) {
    case 1:
      setting = SV1 | SV7;
      break;

    case 0:
    case 2:
      setting = SV1;
      break;

    case 5:
      PORTA |= SVx;
    case 3:
    case 4:
      setting = SV1 | SV7;
      break;
    default: // Illegal drawer type
      setting = SV1;
    }
  SetValves (setting);
  }
+1

, case goto .

0

, , case . , .

0

, , , case - , , , ( ), (break;), .

0

Source: https://habr.com/ru/post/1742495/


All Articles