3.8 Month Guesser Part 2
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Video Transcription
00:00
hi and welcome back to the course.
00:02
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.
00:17
So we said we want to store
00:20
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.
00:34
So I could name that variable as a month
00:38
and I could declare and initialize it at the same time to something like nine.
00:46
All right, Perfect. So
00:48
what we have done here is we're saying we want to set a very well called month to the number nine.
00:55
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
01:07
inside a another variable.
01:10
So what do you think the type of such a variable would be where you are storing a name?
01:15
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,
01:29
and then
01:30
I'm gonna set it to nothing. I'm just gonna
01:33
put a, um, empty set of double courts.
01:37
The reason I'm doing that is because
01:40
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
01:47
initialized and declared it properly, Otherwise it will not
01:51
of function as desired.
01:53
All right, now we need to get to our
01:57
switch statement. So the way you write your switch statement is simply with the keyword switch.
02:02
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
02:13
and then two sets of curly brackets.
02:15
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
02:25
12 months, so we would have
02:30
12 cases.
02:30
Now, let's do case one. So case one
02:35
would be
02:36
if the month is
02:39
the number one,
02:40
which is what the case one over here represents.
02:44
Then we will print out way will store the months name in printed month,
02:51
and we will essentially break
02:55
out of the
02:57
switch statement.
02:59
So once again, what this line over here is
03:02
doing is it's saying that if the value of the month variable happens to be one than store
03:09
the nature of the name of the month in this case, January in a variable called
03:15
printed month.
03:16
And then what I do is I break out of it.
03:21
All right, so now let's move on to case, too.
03:25
Case too
03:27
perfect.
03:28
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
03:47
said you get the hang of it,
03:52
you worry. All right. Perfect. So and then we would break out of the switch
03:59
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
04:13
break.
04:15
And then we will continue doing that for all of the months
04:20
printed. Month
04:25
in this case would be April. So if the value of
04:28
the month variable is four than store April inside the printed variable.
04:33
Um,
04:35
yes. Said the printed variable printed months variable.
04:39
All right, Now, in case five.
04:45
Okay, printed money equals
04:48
May.
04:53
So I'm gonna go ahead and complete that, and then I'll be right back to finishing up with our program.
05:00
All right. Perfect. So
05:01
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.
05:15
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,
05:30
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.
05:43
And we are gonna say, system dot out dot print line
05:47
and printed months.
05:50
So what that there is going to do is it will print the corresponding
05:56
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,
06:10
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.
06:18
All right. So, as always, go ahead and click on the run button,
06:26
and then we should see what our result is Perfect. So
06:30
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,
06:42
where we have stored September in our printed month. Variable.
06:46
All right, so this is essentially house switch statements work, and
06:50
this was our lab. And this is how we went about implementing switch statements. And if we go back to our slides,
06:59
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.
07:15
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
07:27
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.
Up Next
Similar Content