For completeness, a ``++switch++`` over the values of an ``++enum++`` must either address each value in the ``++enum++`` or contain a ``++default++`` case. ``++switch++`` statements that are not over ``++enum++`` must end with a ``++default++`` case.


== Noncompliant Code Example

----
typedef enum {APPLE, GRAPE, KIWI} fruit;

void example(fruit f, int i) {
  switch (f) {  // Noncompliant; no case for KIWI
    case APPLE:
      //...
    case GRAPE:
      //...
    case 3: // Noncompliant; case value not in enum
      // ...
  }

  switch (i) { // Noncompliant; no default
    case 0:
      // ...
    case 1:
      // ...
  }
}
----


== Compliant Solution

----
typedef enum {APPLE, GRAPE, KIWI} fruit;

void example(fruit f) {
  switch (f) {
    case APPLE:
      //...
    case GRAPE:
      //...
    default:
      // ...
  }

  switch (i) {
    case 0:
      // ...
    case 1:
      // ...
    default:
      // ...
  }
}
----
or

----
typedef enum {APPLE, GRAPE, KIWI} fruit;

void example(fruit f) {
  switch (f) {
    case APPLE:
      //...
    case GRAPE:
      //...
    case KIWI:
      //...
  }

  switch (i) {
    case 0:
    case 1:
      // ...
    default:
      // ...
  }
}
----


== See

* https://github.com/isocpp/CppCoreGuidelines/blob/036324/CppCoreGuidelines.md#enum2-use-enumerations-to-represent-sets-of-related-named-constants[{cpp} Core Guidelines - Enum.2] - Use enumerations to represent sets of related named constants


ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)

include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]
