
Lesson Description
The "Conditional Statements" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:
Melkey demonstrates two conditional control structures. If statements include if, else if, and else conditions. Switch statements define a series of cases that a variable is checked against. Unlike other programming languages, switch statements do not require a break statement to end the condition.
Transcript from the "Conditional Statements" Lesson
[00:00:00]
>> Melkey: Let's go ahead and talk about control structures. This kind of involves if statements, switch statements, for loops, while loops, etc. I won't go in too much depth with them, I just wanna kinda show you how they are, how you can use them, because Go doesn't have any tricks about them.
[00:00:17]
An if statement in any other language, is more than likely going to look the exact same way in Go. So with that being said, let's go ahead and create some random variable. I'm gonna make one called age, and then I'm gonna explore the conditional statement. So that's if.
[00:00:33]
So in Go it's just if, we can put a condition, if age is greater or equal to 18, let's just say fmt.PrintLn, and we can say, you are an adult, all right? If you wanna chain this with something like an else if, well, then it's else if, and then the sub condition of age is greater or equal to, let's say, 13.
[00:00:57]
We can do fmt.PrintLn.
>> Melkey: Like so. You can say, you are a teenager, all right? And if you wanna capture everything in kinda this default condition, let me just fix this typo here. You can just capture everything in this else and just say, fmt.PrintLn, you are a child, right?
[00:01:19]
So, very simple, the if else statement, we can go ahead and just run this. So, go run main.go. You can see you are an adult. Very straightforward, no kind of tricks. You can omit the else if, if you want to just if else, very typical. The next kinda, control statement we'll look at will be switches.
[00:01:38]
So, switches are used quite a bit in Go. We can create this variable, let's just call it Tuesday. All right, and then we can do a switch statement. So it's the keyword switch, and then the first argument is, what are you going to be evaluating, right? So we can do switch day, followed by the curly braces.
[00:01:58]
So now, depending on the value of day, we can go into the cases. That syntax looks like the keyword case, and then what the results should be. So we can put Monday, followed by this colon right here, so we have case Monday, depending on what the condition of day is going to be.
[00:02:15]
We can just do format. We can print the line and say, start of the week, all right? So the syntax here looks a little bit different, but it's not too crazy if you wanna chain a bunch of conditions into one. So let's say we wanna look for Tuesday, but we also wanna look for Wednesday, and let's say, Thursday as well.
[00:02:35]
You can do so like this. So if a bunch of conditions fall into the same logic, we can just chain them like so. We can just say fmt.PrintLn, you can say midweek, and type it. All right, we can have one more that says case Friday, you can say fmt.PrintLn, TGIF, whatever you want.
[00:02:57]
And then in switch statements, we also have a keyword called default, all right? And this default is basically, if none of the conditions are met, what does our switch statement fall to, right? There has to be a terminating condition. You can actually omit the default if you want to, your switch may not even be executed, but here we can have a default.
[00:03:17]
And you could just say, for fmt.PrintAll. It could be also considered like a catch all, if you want. We can say it's the weekend, yeah.
>> Speaker 2: You have dynamic case statements in Go, like a Boolean check as a case?
>> Melkey: Yep.
>> Speaker 2: Okay.
>> Melkey: Yep, you can have dynamic check in Go as well.
[00:03:37]
So if the switch statement here again, you can also replace, I'm not gonna show it, but our if else, you can do the same with the switch. So it'd be a switch with no expression, and I'll turn it to if else. I personally would recommend you stick with the if else.
[00:03:58]
It's not broken, don't fix it. If you have a particular use case for a switch, like you're valuing a certain condition and there's different cases, obviously, I think switch has been more appropriate structure here.
>> Speaker 3: No break necessary, and switches?
>> Melkey: Never.
>> Speaker 3: Or other languages, you would just keep hitting the other
[00:04:15]
>> Melkey: Yeah, a good question, so yeah, if your condition's met, it only goes to that condition. It's not like it continues on. So it doesn't go into case one, and then, once it's evaluated, checks for the following cases. Once it goes into the case one, that's it. It will break out of the switch statement itself.
[00:04:31]
So once the switch statement has been adhered, it's good. That's why we have the default. So in case none of them get hit, it's going to hit this last statement regardless, and then break out of the switch statement.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops