> For the complete documentation index, see [llms.txt](https://university-west.gitbook.io/programming-paradigms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://university-west.gitbook.io/programming-paradigms/programming-fundamentals/control-structures/selection-conditional/switch.md).

# Switch

#### Switch Statement in Computer Programming

A switch statement is a control structure used in programming to allow the execution of different code blocks based on the value of a variable or expression. It acts as a simpler way to write a sequence of if-else statements by directly comparing the value of an expression with multiple possible cases and executing the corresponding block of code.

**Python Example**

Python doesn't have a built-in switch statement, but you can simulate it using dictionaries. Here's a simple example:

```python
def switch_example(value):
    switcher = {
        1: "One",
        2: "Two",
        3: "Three",
    }
    return switcher.get(value, "Invalid number")

print(switch_example(2))  # Output: Two
```

**C# Example**

C# has a switch statement, making it much more straightforward. Here's how you use it:

```csharp
using System;

class Program
{
    static void Main()
    {
        int number = 2;
        switch (number)
        {
            case 1:
                Console.WriteLine("One");
                break;
            case 2:
                Console.WriteLine("Two");
                break;
            case 3:
                Console.WriteLine("Three");
                break;
            default:
                Console.WriteLine("Invalid number");
                break;
        }
    }
}
```

In both examples, the code determines the output based on the value of the `number` or `value` variable by matching it with the cases defined in the switch structure, allowing for cleaner and more readable code compared to multiple `if-else` statements.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://university-west.gitbook.io/programming-paradigms/programming-fundamentals/control-structures/selection-conditional/switch.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
