‫-: Hi. Within this lecture we are going to
‫learn a new structure called Switch.
‫And this is basically the same thing with 'if' statements,
‫but it's a little bit more clear,
‫when it comes to 'as if' statements.
‫So if you have a lot of 'as if' statements
‫in a given condition in a given 'if' control
‫then you may want to check out Switch.
‫So best way to explain this is with an example.
‫So I'm gonna create a 'day' into the row here
‫and day will be '1' at first.
‫So I'm gonna create another day string
‫and I'm going to check for those.
‫If day is '1', then I'm gonna print out 'Monday'.
‫If day is '2', then I'm gonna print out 'Tuesday'.
‫Okay? So let me create a new day string
‫over here and this will be empty at first
‫like an empty string with only two double quotation marks.
‫And then later on I suggest you pause the video
‫and try to do this with 'if' statements.
‫Okay? Because it's fairly easy.
‫If day is '1', then day string will be 'Monday'.
‫Okay? And I'm going to print out this day string later on.
‫We shouldn't bother printing out at this point.
‫We can do it at the end of the 'if' control like this.
‫Else if day is '2', day string would be Tuesday, okay?
‫And else if day is '3',
‫the day string will be something like 'Wednesday.'
‫Okay? And I'm not gonna do the whole thing over here.
‫I'm gonna skip Thursday, Friday, Saturday.
‫Maybe we can just go for a Sunday with an 'else'. Okay?
‫So maybe we forgot about the day '4', '5', and '6'
‫and we just go directly to this Sunday over here.
‫Of course you can do the whole thing in your own example
‫but I'm not going to do that
‫in order not to make it way too much.
‫But this should work fine.
‫Let me run this and here you go.
‫We see the 'Monday', let me change it to '2'
‫and we're gonna see Tuesday, right?
‫Here you go.
‫We see the Tuesday.
‫Great. Now I'm gonna do the same thing but with 'switch.'
‫So sometimes it might look a little bit more
‫complicated even to you then maybe you don't
‫prefer using Switch, but sometimes people find it much
‫more easier.
‫So I'm going to show you anyway.
‫So you write 'switch' and you write the value
‫that you may want to check for.
‫Okay, we are gonna check for day as
‫we we did in the 'if' statement.
‫So if day is one, then we are gonna do something.
‫And over here we use 'case'
‫keyword,
‫if case is
‫'1'.
‫So 'case 1' means if day is '1' in switch
‫and rather than same column, we use a column over here
‫and we don't even use a curly brace and this is one
‫of the unique places that we use column and just go
‫to the next line and write whatever we want like this.
‫And we are writing break over here,
‫it means that we are done over here.
‫Just break out of the switch loop
‫because we got what we want, right?
‫If case is '1', we are gonna change the day string
‫to Monday and we're gonna break out of this.
‫So this is how you work with Switch.
‫We can just go along and say
‫case '2' day string is Tuesday.
‫Okay? And we're gonna break again.
‫So we can go for case three.
‫As you can see, it's a little bit more clear
‫on the code when you first look into it
‫like cases and it looks better.
‫But sometimes it can be much more complicated
‫than a simple 'if' and 'as if' control statement.
‫So if you didn't like it, just don't use it,
‫but if you see one, then don't be surprised.
‫So over here,
‫we did the case '1', '2', '3'.
‫And also there is a case of default in the switch as well.
‫Default means if none of this holds
‫then, by default the value will be like this.
‫For example, we can just make it equal
‫to 'Sunday', it will be exactly the same like we did
‫in the 'if' control, right?
‫And we did the same thing with 'else.'
‫So over here we are doing the same thing
‫exactly the same thing actually.
‫If day is '1', we are gonna make it print 'Monday'.
‫If day is '2', we are gonna make it print Tuesday
‫and 'Wednesday'.
‫And if none of the conditions hold, then we're gonna make it
‫even equal to actually 'Sunday.'
‫So this is cool.
‫So maybe we may want to delete everything in the
‫'if' control right now and then we can just control this
‫around this, execute this with switch control so
‫that we can see if we get the same result or not.
‫So I don't wanna delete this.
‫So you can get, and you can just get the access from GitHub.
‫I'm going to comment this out.
‫So remember how we comment things out,
‫how we write comments, we put double slashes,
‫but there are so many lines over here.
‫So actually there is a better way to do this
‫with multiple lines.
‫You put a '/' and a '*' and you put a '*'
‫and a '/' whenever you may want to end this.
‫So between those slash and starts, it's all commented out
‫and it won't get executed once we run it.
‫So now day is '2'
‫and I'm executing this and I'm getting 'Tuesday'.
‫Thanks to the switch that we have written.
‫Let's try with '3', we are gonna get 'Wednesday'.
‫Here you go, it works.
‫So this is how we work
‫with Switch and this is how we work with 'as if',
‫and right now, you know how to work with both.
‫You may choose your own way.
‫Let's stop here and continue within the next lecture.