4 hours 19 minutes
hi and welcome back to the course.
Now, if you remember in the last video lecture or part one of this lab, we just wrote out some comments which can be seen over here and now what we're gonna do is we're gonna translate our comments into actual code.
So we said we want to store
each month in a variable now because each month will represent a number. As I mentioned in the last video that we'll be storing that in an integer in an interviewer type variable.
So I could name that variable as a month
and I could declare and initialize it at the same time to something like nine.
All right, Perfect. So
what we have done here is we're saying we want to set a very well called month to the number nine.
Right now, what we want to do is we need to also store the name of the month, which will be printed out by looking at the number of the month
inside a another variable.
So what do you think the type of such a variable would be where you are storing a name?
Now, if you are thinking about a string data type or a string than that is absolutely correct. So I will go ahead and create any string and let's name it printed month,
I'm gonna set it to nothing. I'm just gonna
put a, um, empty set of double courts.
The reason I'm doing that is because
if we don't set it to anything, we'll get in there and we need to know the program. Or the compiler needs to know that you have
initialized and declared it properly, Otherwise it will not
of function as desired.
All right, now we need to get to our
switch statement. So the way you write your switch statement is simply with the keyword switch.
And what What is it that we're testing here? We're testing the value of the month, so I'm gonna go ahead and put in month inside here
and then two sets of curly brackets.
No, Like I mentioned earlier in the example that I showed you in the slides, a switch statement has cases so we could have multiple. Clea case is now. In our case, we have
12 months, so we would have
Now, let's do case one. So case one
if the month is
the number one,
which is what the case one over here represents.
Then we will print out way will store the months name in printed month,
and we will essentially break
out of the
So once again, what this line over here is
doing is it's saying that if the value of the month variable happens to be one than store
the nature of the name of the month in this case, January in a variable called
And then what I do is I break out of it.
All right, so now let's move on to case, too.
In case to, uh, we're saying, What's the value of the month variable is too. Then we will store the printed month. We will store the printed month as February. The value of the printed month string variable would be February. So we continue doing that. So I'm just gonna go ahead and do that for each of the months
said you get the hang of it,
you worry. All right. Perfect. So and then we would break out of the switch
just like that. We would have our third case, case three. And here were saying, if the value your value of the month variable is three, then we would set the printed month variable to march
And then we will continue doing that for all of the months
in this case would be April. So if the value of
the month variable is four than store April inside the printed variable.
yes. Said the printed variable printed months variable.
All right, Now, in case five.
Okay, printed money equals
So I'm gonna go ahead and complete that, and then I'll be right back to finishing up with our program.
All right. Perfect. So
what I've done here is I've gone and completed all of the cases for the months from 1 to 12 and you should definitely go ahead and do that, too, on Second of October. All right. Perfect.
So, no, we have all the cases for months with the names, the corresponding names, the values stored in the printed month. Terrible. Now we need to do is we need to actually go ahead and print it, print this information,
and how are we gonna do that? So we're gonna get out of this switch statement. So if you can see this curly bracket over here corresponds to this one over here. So we're gonna get out of that.
And we are gonna say, system dot out dot print line
and printed months.
So what that there is going to do is it will print the corresponding
month name to the value. So in this case, we have set month to nine, and it should be printing out September if everything happens to go Well,
which always isn't the case, because when your programming, you usually encounter a lot of bugs and stuff that you have to fix. But this is a fairly simple program, so we should be good.
All right. So, as always, go ahead and click on the run button,
and then we should see what our result is Perfect. So
it ah printed September in the council, which is exactly what we wanted. And it did that by looking at the case for the value nine of month,
where we have stored September in our printed month. Variable.
All right, so this is essentially house switch statements work, and
this was our lab. And this is how we went about implementing switch statements. And if we go back to our slides,
then in this video lecture or lab, I should say we learned how to implement a such statement by making use of it and creating a program which determines the name of a month by a given value, which I just showed to you a few seconds ago.
All right, so coming up next, we will be talking about why loops and all about while loops, and then we'll be jumping into a lab after the wild loops video. Lecter
now is always feel free to ask me any questions at all, and you can always go ahead and add me on Lincoln. I would be more than happy to answer any of your questions, and I look forward to seeing you in the next video Electric.