0

The Arduino Switch Case Reference has a switch structure written two different ways:

First in the Syntax as:

switch (var) {
  case label1:
    // statements
    break;
  case label2:
    // statements
    break;
  default:
    // statements
}

and then in the Example code as:

  switch (var) {
    case 1:
      //do something when var equals 1
      break;
    case 2:
      //do something when var equals 2
      break;
    default:
      // if nothing else matches, do the default
      // default is optional
      break;
  }

Is there any reason to (or not to) add a break; in the default: case? Does the need for 'break;' change if the final case is not default:?

ATE-ENGE
  • 941
  • 3
  • 19
  • 32

1 Answers1

2

You can, but you don't have to.

Should you? That's up to you.

I do. That's because it's then simple to add another case at the end of the structure in the future if you want, and you don't have to remember to add the break to the existing last case.

Majenko
  • 105,851
  • 5
  • 82
  • 139