- [Narrator] When making decisions in code,
it's common to use an if statement
or a series of if/else statements.
An alternative to this can be the switch statement.
A switch statement is a more streamlined conditional
that's used when you wanna compare a single variable
to a series of constants.
A common use of switches is to make decisions,
based on enumerations, or enums.
Consider a game where a character's conversation options
were based on their intelligence.
You could set those options
using a series of if/else statements,
but that could quickly get cumbersome.
Instead, we can use a switch statement
to more easily implement our solution.
Here, we have a script called Conversation.
In this script, we will implement a function
that will allow a character to give a greeting
based on their intelligence.
First, we will create
an int for our character's intelligence.
We'll set this to equal five.
For the purpose of this example,
let's say that intelligence ranges
from five being the highest
to one being the lowest.
Next, we'll create a function called Greet
which will use a switch statement to output a greeting
based on the character's intelligence.
The syntax for a switch statement
starts with the key word, switch,
followed by the variable,
which will be comparing in parenthesis.
In Visual Studio,
it's possible to quickly create switch statements
using snippets by typing S-W and then hitting Tab.
You can also type the name of the variable
that the switch will evaluate,
and, for certain variables like enums,
it will auto-fill the cases of the switch,
which is really helpful.
In this case,
we will be comparing our "intelligence" variable.
We follow that with some open and closed braces.
Inside the braces, we need to define a series of cases.
These cases are the constants
that we are comparing our variable against.
To create a case we use the key word "case"
followed by our constant and a colon.
As you can see,
we're starting to define what the character will say,
if they have an intelligence of five.
Let's print out a greeting
that someone of high intelligence might give.
As I am not of particularly high intelligence myself,
I'm going to guess.
That seems about right.
After the instructions for each case
we need to include something called a jump statement.
The most commonly used jump statement for switches is break.
This is a key word that jumps the code execution
out of the switch
so that none of the subsequent cases are executed.
You can think of break working similarly
to the return key word,
but whereas return, most commonly is used in functions,
break is used in switches and loops.
After encountering a break,
code will continue execution
after the block of code it is in.
In our situation,
since the break is inside of a switch,
after it's encountered,
execution will continue just after the switch.
Let's put in a couple more cases
with phrases of decreasing intelligence.
For our final case, we will wanna catch everything
that doesn't have a case of its own.
To do this, instead of using case,
we use another key word, default.
Since this should be for any case
that hasn't already been covered,
it doesn't need a value.
Other than that, we treat it as we would any other case.
Think of default as being similar
to the else in an if/else statement.
Just like else, default covers any situation
not caught by a preceding conditional.