<commit_before>
# Switch statement

The switch statement is similar to C, except for the changes below:

    * no auto-fallthrough
    * auto-scoping
    * default (if present) must be last

## Auto-fallthrough

Unexpected fallthrough is a big source of issues in C programs, so in C2
this implicit behaviour has been removed. Any case statement must end in
either: *break* | *fallthrough* | *return* | *continue* | *noreturn-func*.
The fallthrough statement can only appear at the top-level of the case body,
not in some sub-expression (eg. _if (x) { fallthrough; }_ is not allowed).

```c
   switch (i) {
      case 0:  // this gives an error, last statement of a case must be one of
               // break/fallthrough/continue/return.
      case 2:
         fallthrough;    // will fallthrough
      case 3:
         break;
      default:
         fallthrough; // not allowed in last case
    }
```

## Case auto-scope

```c
    switch (i) {
        case 1:
            i32 a = 10;     <- in C, user would have to add {} around case body.
            return calc(a);
        case 2:
            i32 a = 20;     <- no clash, since it's another scope
            ...
    }
```

## Default last

Finally, the default statement must be last, to increase code-uniformity.


# Sswitch statement

C2 introduces a new statement: *sswitch* that can be used to
make a switch-like stament, but with strings (sswitch = string switch).

```c<commit_msg>docs: added enum switch scope example<commit_after>
# Switch statement

The switch statement is similar to C, except for the changes below:

    * no auto-fallthrough
    * auto-scoping
    * default (if present) must be last

## Auto-fallthrough

Unexpected fallthrough is a big source of issues in C programs, so in C2
this implicit behaviour has been removed. Any case statement must end in
either: *break* | *fallthrough* | *return* | *continue* | *noreturn-func*.
The fallthrough statement can only appear at the top-level of the case body,
not in some sub-expression (eg. _if (x) { fallthrough; }_ is not allowed).

```c
   switch (i) {
      case 0:  // this gives an error, last statement of a case must be one of
               // break/fallthrough/continue/return.
      case 2:
         fallthrough;    // will fallthrough
      case 3:
         break;
      default:
         fallthrough; // not allowed in last case
    }
```

## Case auto-scope

```c
    switch (i) {
        case 1:
            i32 a = 10;     <- in C, user would have to add {} around case body.
            return calc(a);
        case 2:
            i32 a = 20;     <- no clash, since it's another scope
            ...
    }
```

## Default last

Finally, the default statement must be last, to increase code-uniformity.

## Automatic Enum scoping

C2 places enum constants in the Enum type namespace, for example:

```
State s = State.Begin;
```

For a switch statement using an enum type, the scope is automatic, so it's possible
to do:

```
func void demo(State s) {
    switch (s) {
    case Begin:
        break;
    case Middle:
        break;
    case End:
        break;
    }
}
```

So no need to add `State.` to every case.

# Sswitch statement

C2 introduces a new statement: *sswitch* that can be used to
make a switch-like stament, but with strings (sswitch = string switch).

```c
{'fraction_non_alphanumeric': 0.0914560770156438, 'fraction_numerical': 0.009326113116726835, 'mean_word_length': 2.3790650406504064, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6807855', 'n_tokens_mistral': 1006, 'n_tokens_neox': 948, 'n_words': 430}