Switch
The switch statement is also a statement used when you have conditional branching. It is most often used to map a numeric value to a string, for example, when you are mapping a status code to a string to return to a user.
If there is a break statement at the end of the case block, the code will return from the switch statement at that line. Otherwise it will "fall through" to the next case statement.
The code above demonstrates how the cases flow through, but it also demonstrates the limited use-cases for this approach. There are around 100 codes for each category, so you wouldn't actually want to use this technique here. It would be better to use an if statement.
A better use case is where there a just a small number of cases mapping to a single result. Also notice how you can use a return statement instead of storing the value in a variable and returning that value at the end of the function.
Exercise
Write a function that accepts a single letter representing the first character of a DNA nucleotide and return the first letter of the corresponding RNA nucleotide that it maps to. The mapping is as follows:
G maps to C
C maps to G
T maps to A
A maps to T
Last updated
Was this helpful?