# Switch statement

The `switch` and `case` statements cannot be used most of the time since they are not in the `Norm`.

It could be useful in some exams, and you can use it there since there is no `Norm` during the exams.

### Switch statement

The `switch` and `case` statements help control complex conditional and branching operation.

### Examples

I'll take as example one of the exercises of the `exam rank 02 - level 2`, the [`do_op`](broken://pages/0wkdz6SicAX1XfT1HoCH) one, go read the subject there, then come back here.

I'll first write it using if and else if statements, then I'll write the exact same thing using the `switch` statement.

#### If ... else if

<pre class="language-c" data-overflow="wrap" data-line-numbers><code class="lang-c">if (av[2][0] == '+')
    printf("%d", atoi(av[1]) + atoi(av[3]));
else if (av[2][0] == '-')
    printf("%d", atoi(av[1]) - atoi(av[3]));
else if (av[2][0] == '*')
<strong>    printf("%d", atoi(av[1]) * atoi(av[3]));
</strong><strong>else if (av[2][0] == '/')
</strong><strong>    printf("%d", atoi(av[1]) / atoi(av[3]));
</strong><strong>else if (av[2][0] == '%')
</strong><strong>    printf("%d", atoi(av[1]) % atoi(av[3]));
</strong></code></pre>

#### Switch

{% code overflow="wrap" lineNumbers="true" %}

```c
swtich (av[2][0])
{
    case '+':
        printf("%d", atoi(av[1]) + atoi(av[3]));
        break;
    case '-':
        printf("%d", atoi(av[1]) - atoi(av[3]));
        break;
    case '*':
        printf("%d", atoi(av[1]) * atoi(av[3]));
        break;
    case '/':
        printf("%d", atoi(av[1]) / atoi(av[3]));
        break;
    case '%':
        printf("%d", atoi(av[1]) % atoi(av[3]));
        break;
}
```

{% endcode %}

As you can see, both codes are pretty similar, but I personally think that the `switch` statement is clearer and easier to write.

The switch statement takes a bit more place but could be useful in some cases.

You can find more details about it [here](https://www.w3schools.com/c/c_switch.php).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-cursus.gitbook.io/guide/useful-tools/switch-statement.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
