# Matching on boolean values using switch statements

All Turing-complete programming languages provide some way to branch conditionally into different sections of code. This is true in the most basic programming languages (i.e. assembly language):

In most higher-level languages (e.g. the JavaScript below), this is implemented in the form of an if..then..else statement:

In fact, even an instruction set with a single instruction (OISC) will implement conditional branching in some way!

However, consider the following slightly messy code:

Although this makes programmatic sense, and after a few seconds we can tell what is going on, we allow ourselves a bit of syntactic sugar when we want to branch out on a few different paths — based on the result of one variable — using a switch statement. So, we could equivalently write the above as:

This is the normal use for a switch statement — it allows the program to branch into different sub-routines based on the system’s evaluation of a variable. However, consider the following code:

This code could (will) start to get messy if we start introducing more branches or additional instructions in each branch. Thankfully, the switch statement can help us again. The subject of a switch statement — the a variable in the switch example above — does not need to be a single variable declaration. It can instead be any logical expression with a [truthy (or falsey) value](http://james.padolsey.com/javascript/truthy-falsey/, against which each of the cases can be evaluated for comparison. For example, the above snippet could be re-written as:

This implementation of a switch statement — switching on a boolean value and matching one case (or multiple cases, if you remove the break statements) — is a little-known paradigm and one that I’ve found hugely useful. Now I just have to work out who thought it was a good idea that Python not have switch statements at all!