Control structures continued

GetGoing: Introduction to Golang Data types and control structure
7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

Hi everyone. So in this video we're going to be learning more about switch case statements and keywords like continue and break. So you might have noticed that I'm naming my files in a very weird way main one load, go into document six Dotto. So for the sake of the tutorial right now I'm naming the files in this way. But then eventually when I make my repository or the sources repository public, then you guys are going to be off one night with a fun time with the naming conventions. So here we go.

So in this video, we're going to be talking about continue. I'm sorry, guys, I have a sore throat and then switch case. So diving are going to continue. So let's say we have four block for is equal to zero, i less than 10 i plus plus. So if I is an even number, if I person two is equal to zero then I wanted to continue They only want to print or numbers. So what this is going to say is that, okay, you have your eye.

And then if I is a even number, then you just go continue this loop you don't go below, you just continue this loop from start. And then if it is an odd number, then this if statement isn't going to run and then your from T dot readline is going to run. So let's see what happens in Golden. Mean, six and only or numbers. So I apologize for the naming conventions right now. So now we go to break statements.

So what I want to do is whenever I encounter an even number, I just want to break the loop. And then let's see I put in work though value of my current is what are the, let me do a flag, false flag. So what this is going to do is that first time it's going to loop is going to say that okay is equal to zero, rather start the first one. The first time is going to do it's going to say that i is equal to one, this block doesn't run, FMT dot print line prints one. And then the second time this block runs, it says, okay, flag, it turns a flag to false, and then it breaks out of the loop, and then it prints a flag. Right?

So then, the thing is, the thing about the break keyword is that it breaks up the loops. So this condition won't be run ever again. And then this break break is gonna take us right to line number 17. So I'm just gonna do a quick initialization sort of a declaration. And then and as they're doing so this is continuing break. And then now we're going to be talking about, let me continue here.

If I equal to equal to one, I want to continue, I don't want to print anything. Good. So let's talk about switch. Right. So let's say I have a D. That is one that is Friday. Right?

Now I want to switch I want to see, I don't know, look at all of the conditions and then match those conditions against the date. So I have two choices. I can say that okay, which day and if the day is Friday, you just put in or Tango, it is Friday. And then you have keys and if dark days Monday or Tuesday or Wednesday or any other Nerdy Wednesday okay. boarding cool. And then new before So, in line number 24 There seems to be an error okay my bad fine.

So in other languages like Python c c++, you might have seen that okay, if you want to do multiple matches of the same case, you do like this case Friday case Saturday or Sunday or things like that, but in go, you do you can easily do it by separating all of the values by a comma So in the case of Friday's gonna print t if in case of Monday, Tuesday or Wednesday is going to print boring. And if any other default case arises like Thursday, Saturday and Sunday is just going to print default. So, let's see what happens when we go to go to go run. And then triangle it's right. Indeed. So, this is one way to run the switch statement.

The other way is simply by not defining what to switch on. So you can easily say switch, just a simple switch block without any conditions and then you can say, yes, you can define you can define the controls treatments, right in the case of day is equal to is equal to Friday. No time. Did you get the idea. And then you can also have blocks over here. So it throws no error if there are big blocks in the switch statement, like other languages, and then look, we do a simple go run and TJ Nice.

So this is all switch statements work in golang. So a quick thing to notice that this comma separated list. So just don't fall for this Don't fall for what other languages or languages are doing with which case statements go clearly defined that you if you want to match multiple cases, then you can separate those values by commerce. So a real life application might be that if you if you're doing a web development project, and then you get a new error, so errors dot new to any new window, then you can simply do a switch on switch error case, some error code or something like case era. So this is a really, really important thing when it comes to catching errors and real life examples of web development and you're gonna see this so Thank you guys and see you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.