Hello, and welcome. In this lecture, I want to talk about the switch statement.
It's an alternative to an if statement,
but usually used only in specific situations.
But it has some nice cool syntax for doing some repetition in your if statement.
So the conditional operator and the if than else statements
make it easy to write programs that choose between two alternatives.
It's very easy to ask questions and make decisions.
However, many times,
a program needs to choose one of several alternatives.
And this would essentially being
the if else if else if else if else if.
It's messy because you can obviously implement this with the if statement
when you have several alternatives,
but you could have a hundred else ifs. So it's tedious, it's prone errors,
and it's really just a pain in the butt. It's like writing a bunch of boilerplate code.
So the alternative to this syntax and
everything you can do in the switch you can do in an if.
But it's cleaner and it's easier to and it's a lot more efficient actually too.
So when the value of a variable is successfully compared against different values,
use the switch statement.
And what this means if you have an if statement and then you have an else if, else if, else if, else if,
else if, else if, use the switch statement.
Now if those Boolean expressions are a little more complicated.
You can't use a switch statement. You can only use a switch statement
when you're comparing against a value, like a constant.
The expression cannot be that complicated.
But it is much more convenient and very efficient to use the switch statement,
and most languages have this construct as well. And see it's very nice. The one thing
you notice a lot of languages will build based off of c.
If you've programmed before
you're going to see and be familiar with a lot of these constructs
because they originally came out in c or be, the language before.
So here's the syntax of a switch statement. You have the switch keyword.
And then inside the switch, you have these left and right parenthesis,
which is the expression. So it looks just like the if statement at the beginning
except instead of the if keyword, you have the switch keyword.
You then have the left bracket.
The left brackets required. That left bracket starts the body of the switch.
And you see the ending right bracket at the very bottom, that ends the body of the switch.
The other thing that's important here is these keywords case.
Case is actually like the
if statement itself. So switch is actually
going to be looking at an expression,
not whether or not it's necessarily true or false.
The switch could be just looking at a variable or something.
And then it's going to evaluate that variable in the case statements.
So if value 1 equals expression,
if value n equals expression.
But you can look at this as like
the switch being at the top level and then the cases each being an if statement,
comparing the values against the expressions in the switch.
So cases are also keyword. You also notice the colon after the case.
So you have case and then you have your value.
And then inside there, you can have multiple program statements.
And then at the end, you have a break keyword. That's another keyword. So there's three keywords here,
actually four. There's the switch keyword, the case keyword, the break keyword
and then the default at the bottom.
So these are the important things to notice. We'll go through a detailed example,
but you can have different cases. And again, this is just doing like an if else, if else, if else, else if.
So if value one is equal expression, execute the code.
Else if value two equals expression,
evaluate that. Else if value three equals expression, evaluate that.
Else if value n equals expression, execute the code inside that block.
And the block for the cases start with the colon and end with the break.
And if you don't have that break there,
it's going to fall through the other cases. We're going to talk about that again.
And then the last thing here that's important is this default keyword. This is essentially
the else, else if, else if, else if and then else at the bottom.
So this gets set if none of the other cases evaluate to the expression.
So let's talk about some of those details that I mentioned.
The expression enclosed within the parentheses is successfully compared against the values.
So again,
if we go back to the syntax, expression is compared to all the values.
And if it equates to any of the values, you execute the code inside that value.
Cases must be simple constants or constant expressions,
expressions that evaluate to a constant.
So you could switch on x plus y
because if x is 5 that would just switch on 10 and it would compare the values to 10.
But usually, you switch on a variable that's equal to some kind of number.
If a case is found whose value is equal to the value of the expression,
then the statements that follow the case are executed.
When more than one statement is included, they do not have to be closed within braces.
So that's just saying that
when more than one statement is included,
there's no braces required for the case statements.
Remember, for the if statement, we would always have to have those braces,
that's not the case.You can just put them one line after the other, the key is the break there.
The break statement signals the end of the particular case
and causes the execution of the statement to be terminated,
it jumps out.
If you don't have the break there, the next case it will fall through.
So you want to include the break statement end of every case. When you don't include breaks,
it usually means there's a bug or something.
The only reason you want to include a break is if you had multiple conditions
that you wanted to execute code. So if you had multiple cases,
basically saying if
the expression was this case, this case or that case,
then do the same thing,
then you would take out the breaks.
So be careful with the breaks because the breaks can cause issues
whether or not certain blocks are being executed in the next case.
The special optional case called the default is executed
if the value of the expression does not match any of the cases. It's the same as a fall through else
it happens if none of the cases are true.
So here's a nice example where we're creating a switch to evaluate an enum.
In this case, we have an enum type called weekday,
which is just specified by all the weekdays.
And then we created a variable of type enum weekday and we set it to Monday
and then we switch on today.
So depending on the value of today, the variable,
one of the case statements is going to be executed.
If today is equal to Sunday,
it's going to print out today is Sunday and then jump out.
If today is equal to Monday, it's going to print out today is Monday and jump out.
If it's not equal to any of them,
it will go to the default case. So let's run through this example in code blocks.
Here we have our sample code. And you're going to see that
because today is equal to Monday,
case Monday is going to be evaluated and you're going to see output today is Monday.
So let's go ahead and build this code and run it. And that's what we're going to see,
today is Monday.
Because what's happening here is today is going to be the Monday.You put this the
expression you want to evaluate the variable today then evaluates it.
Key things to remember here are
you have a bracket to start the switch statement on line 16
in an ending bracket to end the switch name in line 29.
You can have multiple lines inside each case statement without using brackets.
Now let's see what happens if we take out the
break statement after Monday.
Because remember, break will jump right out of the case. But if we remove it,
what's going to happen is it's going to fall through to
Tuesday and you're going to be seeing both printfs
executed for today's Monday and today is Tuesday.
So now when we run the program,
we see today is Monday, today is Tuesday.
That's the importance of the break statement.
The break jumps out.
So this might be useful if you wanted the same output for Monday and Tuesday.
So for example sometimes you'll see case statements like this,
case Tuesday because you want the same output and it doesn't matter
what the variable is equal to.
So you may see things like that, but when you have separate case statements that have different
execution, you have to add that break in there.
So we're only looking at Sunday, Monday and Tuesday.
What happens if we set the today value equal to Sunday
because there are no cases that are going to match that.
Well, what's going to happen is it's going to fall through the default and it's going to print out whatever.
So let's try running this again.
And we see today is Sunday.
Sorry, set it to the wrong variable. I want to set it to,
let's just say, Wednesday. We had a case for Sunday.
So now we'll see that it actually prints out whatever
because it didn't match any of the cases.
So all you have to do for switch is really memorize the syntax.
The expression goes into parentheses that's what you're going to evaluate.
The cases have to match the expression. So usually, you're doing variables that's why it has to be constants
or expressions that evaluate to a specific constant value.
And then you have cases for each thing and then a default all case, and don't forget about the breaks.
Let's go back and talk a little bit more and another example of
the switch construct with cases.
In this next example, it's a little different.
What we're doing here is we're getting data from the user and then switching on that.
So in this example in our main function, it starts off creating a couple floats
and then it creates a character operator and then it asks the user to enter
a value, an operator and a value two.
And it's using format specifiers to distinguish, and they all have an address of ampersands.
Depending on the character you enter for the operator, whether it's plus minus or star,
that's what you want to include in your switch case.
So if the user entered a plus,
so you see in the next column here, the switch construct, it's switching on operator.
It's going to compare the value of operator, the value of the variable operator
to each one of these case statements. And you'll notice that these case statements have
single quotes around them because we're comparing characters.
So you can compare numbers,
variables to values specific constants or characters to specific constants.
And then inside each case statement, we're printing out a specific format.
Value one plus value two, value one minus value two.
We're actually performing calculation in our print.
And then again you see those important break statements
those are jumping out, and they're needed if you don't wanna fall through any of the other cases.
You also notice that in that last case statement where it has the forward slash the divider,
we have an if statement.
We don't have any brackets around the case
because we can have multiple statements in each case without brackets.
So the last case we say if value two is equal to 0,
we're doing a little check to make sure we don't divide by 0.
And if we don't divide by 0, then we're going to actually perform our division.
And then at the very end of that case, we have another break.
Our default case is saying you entered an operator that we don't know how to handle.
But the switch statement is is very efficient.
It's very quick to write whenever you're comparing many
different values to a specific variable and then you want to do different things.
The last thing I wanted to mention was the go to statement in relation to decision making.
It's not a direct decision statement,
it's more of jumping to a different line of code, using a label,
but it is available in c, and that's why I wanted to mention it.
There's two parts to the go to statement, the label and the go to keyword.
Now for those of you that have programmed before
in Fortran or some other basic languages, you're probably going to recognize this
and you probably used it quite a bit.
Now coming to c, there's no need to use it
really. There's a couple cases where I saw it was pretty useful,
which I'll explain in a second, but the go to is jumping out
to some code, and you could go to any line of code.
One of the reasons people don't like using it because it's very hard to follow.
When you're maintaining a program and if it's sequential,
you're going line by line, if statements in the while loops.
It's bad enough when you have the if statements in the while loops.
But now if you have something where basically you're on line 27 of your source code
and you're going to jump to line 800 for no reason other than just the go to keyword,
all sudden you're skipping a ton of code, and it's very hard to maintain and follow.
So the two parts to go to and the label name. The label's
named following the same convention for naming a variable, alphanumeric characters,
you can't start with digits, you can start with underscores.
you can start with underscores.
Basically, you just use the go to keyword and you go to some identifier,
part two. Part two is a label somewhere.
So in order to actually jump to that line of code, you have to label that line of code.
There has to be a statement that says part two.
Now I'll show you an example of this, but you really should never use this
because of the problems associated with maintenance.
However, I wanted to mention it if you programmed before and you're used to using it.
In c, you don't really need to use it as much.
There's one use case that I saw that might be useful.
I was working the other day with a fellow programmer,
and he had a bunch of nested loops
or nested if statements.
But the loops is really probably the better example. Loops, remember, repeating code. He had a lot of nested loops.
And at the very inner nested loop, like the fifth one,
he wanted to jump out of all the loops because he had some condition.
And so he used to go to to jump out of all those loops.
Now the other way to jump out of loops is using the brake keyword,
but the brake's only going to jump out of the most inner.
So there was really no effective way without him setting a bunch of flags
and having a bunch of if statements to exit every single loop,
so he used to go to. And that was one case I thought it was pretty useful because
it reduced the amount of code that you had to write and it was pretty clear on what was happening.
You were just jumping out of all the nested loops
because you met a certain condition.
And that's one case. But for the most part, you don't really need to go to and for the reasons I mentioned
it's very problematic, it's hard to follow code when you're jumping all over the place.
But the go to is just like you use it just like you use in other languages.
You have a go to keyword with the label name.
And what will happen in your code is you'll jump to that line of code with the label.
So you can skip all sorts of code.
So in this example here, we have a label called top,
and it has a colon. That's how you know it's a label.
And top has some statements. And at the very bottom of that,
you have an if statement. And you say if the char equals y, go to the top.
So again, you see how you can just jump all around in your code and that makes it very hard to follow.
So don't use it basically, unless you have many, many, many nested
loops and you want to jump out of all the loops. Thank you.