Skip to content

Documentation error in switch / case reference  #3722

Closed
@nickgammon

Description

@nickgammon

The page https://www.arduino.cc/en/Reference/SwitchCase has example code:

  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
  }

This gives a compiler error:

sketch_aug25a.ino: In function 'void loop()':
sketch_aug25a:21: error: expected primary-expression before '}' token
sketch_aug25a:21: error: expected ';' before '}' token
expected primary-expression before '}' token

Example code should compile, not generate confusing errors. We just get a lot of posts on the forum asking what is wrong.

In this case the default case should have a statement, eg. break:

  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;
  }

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions