3.6 The Switch Statement

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 »

Time
4 hours 19 minutes
Difficulty
Beginner
CEU/CPE
4
Video Transcription
00:00
hi and welcome back to the course.
00:02
Now, if you remember in the last video Lecter or Lab, I should say we went over finishing up our account program in which we implemented the idea or concept of four loops.
00:13
Now, in this video lecture, we're gonna be going over the switch statement in Java.
00:19
So some of the learning objectives for this video lecture are the following.
00:24
Understand what switch statements are and their use,
00:28
as well as essentially understand their syntax and how we can write them.
00:36
All right, So what are switched statements? To begin with?
00:41
A search statement in Java allows the program to select one of many code blocks to be executed.
00:48
Now, it would be best to see a picture or an example of one.
00:54
All right, so if we start from the beginning, we have a class called switch example with a main method.
01:00
And then we have a variable called age, which is set or initialized to zero.
01:08
Keep in mind, this variable age is an integer type,
01:12
and then the chunk of the program is right here, where you start with the switch statement by saying switch
01:21
and then we pass in the age parameter.
01:26
Now we say, if the case happens to be that the age is one than print out this coat over here
01:34
and if the ages to then protect this coat over here
01:40
and if a JJ is neither one of those So if age is not one or not to then print out the default code down here,
01:49
so what do you think is gonna happen in this case?
01:53
If you're thinking along the lines of printing out the default code block, then you are absolutely correct. Because if we carefully notice and look at age, age is already set to zero. And since it is set to zero, there is no way you can go into the first block
02:12
or the second book, because age never happens to
02:15
become one or become the value to it is always going to resort to the default
02:21
value and cold block which will print out. This is what will print when the default case is reached.
02:30
Perfect. So that is how a switch statement looks. And that is how you write a switch statement
02:37
not to summarize in this video, like you're in this very short and quick simple video. Elektra. We went over
02:44
switch statements and their use
02:46
as well. A syntax for Swiss statements or how we can write them in Java. Now there's one more thing that I would like to elaborate on.
02:54
People often use switch statements when there are a lot of paths that you can take in your program. They are not necessary. But it's like an alternative that you can use instead of F l statements.
03:08
All right, Perfect. So in the next video that's coming up, we're gonna be doing our third lab of Marshall three, which is gonna be a month guesser. And in that lab, we're gonna be implementing the idea of switch statements and actually gonna be seeing how they work.
03:25
Now, as always, if you have any questions at all, feel free to contact me via Lincoln. And I would be more than happy to answer any of your questions. Thank you very much for watching. And I'll see you in the next video
Up Next