 Welcome back everyone. Today we're going to talk about loops in go language. Please like and subscribe to help this channel grow. So this is the fourth video in this series. So far we've already talked about setting variables, we've already written a hello world program, gone through how to install go language, and today we're going to jump right into a very useful feature of most programming languages called loops. So in programs you normally want to do something over and over again, and we can achieve that by running what we call a loop. So you might notice that my console looks a little bit different today. Instead of just using the command line, I am using Microsoft Visual Studio Code to write my program. It just makes things a little bit easier to edit and modify as we get more complicated programs. So I'll add a link to this Visual Studio Code below. Please download it. You can also just use the command line or even notepad if you want to edit your program. It just makes things a little bit more difficult sometimes. You'll notice on the left side of my screen I have my past go scripts that we've already worked on before. And today I'm working on the 004 loops script. And I will upload all of these scripts to GitHub if you want to follow along with that. On the main part of the screen, you can see our familiar package.main, our go program package.main. And then the function main, this is where our program actually begins or where we jump into our program. And then at the bottom here, I have a terminal, what's called the terminal. So I can run all of my go programs from this directory. If I do LS on Linux or DIR and Windows, I can see all of the files or programs that I have in this directory right now. And we're working with the 004 loops.go. Okay, so we already have that. Now what we need to do is actually create the loop or start creating something like a loop. So remember from last time, anytime we want to print, we need to use a package called format or FMT. So the first thing I need to do is make sure I import FMT. Okay, this is a very common program or library to import if we want to print outputs. And that's what we want to do is print the output to make sure that we're looping properly. Okay, you don't need it to do a loop, but we want to actually output something. So we need to use it. So in go loops are called for loops. And for loops have kind of an interesting, I guess, way to set them up. I'll just write out the whole thing first, it'll make a lot more sense. So we can say, for example, for I, and I here is a variable that we are creating for the first time. So just like in the last video, we need to use the semicolon or colon, sorry, equals. And then I'm going to say I equals one. So here we're saying for I equals one. And then I need some condition to check. So this is the variable that we're monitoring. And I've already set that variable to one. The next field that we need to fill in is the check, like when, when will we stop looping actually. So this is whenever I, let's say is less than or equal to 10, just the value 10. So that will loop over 10 times. And then what do we want to do after each successful loop? Well, if we've looped one time, what we probably want to do is add one to I. So what I can do is just I plus plus. And if you've never seen the plus plus before, it just means increment by one. Okay, so that's increment by one. So then I need my bracket. And now I have the beginning of my for loop. So this looks a little bit complicated. Let's go back through this again, we say for, and that's just looping. We initiate the variable I. So I equals one starting out. And then we say, as long as I is less than or equal to 10, keep looping. If it's over 10, then stop looping. Okay, and then the last part is what do we do after every successful loop? Well, we increment I or we add one to I. Yeah, we just add one to I. Okay. So now we already have our loop done, this will loop around, it will start I equals one, and it will loop around until I equals or is greater than 10. All right, so we want to make sure that we're looping properly. So let's go ahead and print the value of I every time we're looping. So we can do FMT from our imported package dot print line print l in. And then I just want to print the value of I and nothing else. Okay. So if we look at this whole thing, we have our for loop, we're creating our new variable, we are checking to see if I is less than or equal to 10. And if it's not, if it doesn't meet that condition, then we will add one to I. And then every time that that happens, we will print format print line I. Okay, so let's go ahead and save that. And then we can just like before we can run go run. And then this is 004 loops dot go, press enter. And what we have here is counting from one, this is how we initiated I. And then we counted up to 10, because this is saying less than or equal to. So if it's less than or equal to 10, then we print. If it's greater than 10, we don't print and we stop the loop. Okay, so now remember, because of these are variables, because I is a variable, I can actually assign it anything, I can say I equals five. So instead of counting from one to 10, what will it do? Well, it should count from five, six, seven, eight, nine, 10. So it should start from five and count to 10. Right, we can change I initiate I to be anything. And we can also change our check to be anything. So I could say, instead of, instead of I is less than or equal to 10, I could say I is less than or equal to 99. And then, as you can imagine, it will count over will loop almost 100 times. Okay, right. So this is a very basic for loop where we were just counting through here, we're incrementing I and we're printing it out every single time. There's lots of different ways that we use loops. Counting is just one of the very basic ways. But the important thing to notice here is the way that we're initiating variables for the loop. So instead of just initiating or setting everything up in our loop right here, maybe I don't want to do that. Okay, so maybe instead, I want to say something like test, I'm going to initiate the test variable. And I'm going to say test is equal to 20. Okay, so now we have a variable called test. And we say that that variable is equal to 20. Well, I can put that in the place of basically any of these numbers as well. So test, let's say that I want to count check and see if I is less than or equal to test. Well, what's the value of test? 20. Okay. So here, instead of checking for some hard coded value, I've said I is less than or equal to the whatever the variable test is set to. So if I back down to one. So now I have test is equal to 20. I is initiated at value of one. And I'm checking for I is less than or equal to test. Okay, so what should happen? It should count from one to 20. Okay, let's go ahead and run that. And it does. Okay, so starting at one counting all the way to 20, because we started at the value of one, I equals one, and we're counting until we get to test. And once it gets over the value of test, then it stops. Okay, so this is the format for a for loop is actually pretty standard in most programming languages. This is very similar to what you'll see in other languages as well. For loops are just really common across, across most languages. Looping is kind of a main feature of basically all programming languages. Okay, so sometimes, however, you don't necessarily want it to be this complicated. Maybe we want it to be just a test instead of creating our variables at the for loop definition, we can pre define all of our variables before. So for example, let's say that I have test, and I have another variable end. Okay, so end, I'll say 30. Okay, so instead of initiating all of these things, I'm going to go ahead and comment out this old for loop. Now notice here, I'm putting slash star. And then I'm ending the comment with star slash. Now what this does is say that I don't want the, I don't want this to be part of my program anymore. You can use comments for all sorts of things. So for example, just writing text. So I can add some human readable text in here, like, hello, this is a for loop. Okay, so we can use comments to add notes. And if you do slash star, this is a multi line comment. Okay, if you just want one line, so for example, maybe at the top, we want to put the author. So at the top, I want to, let's say explain what this program does. So I can just use forward slash forward slash, and then type whatever message you want. So this program is a for loop example. Okay, now if it's not part of the program, what's the point of comments? The point of comments is specifically for humans to be able to read what you're doing. Whenever you start getting more detailed programs, you really need to start commenting what different functions are doing, because sometimes it's not very clear what your code is actually, what, what the goal of your code is. So if we make a comment, we can leave a note for other people that see our code to understand what, what, what everyone is doing. Okay, so I have these comments in here. So now I've commented this out, this will no longer be part of my program. So for all intents and purposes, this is essentially erased as far as the, the programming side is concerned. Okay, so now, because I don't have a for loop, I need to make a new for loop. Now our goal here is instead of creating all of our variables in the for loop itself. So for example, initializing I, and then saying that we're going to count or increment I every time, I just want to do my test. So what I'll say here is test the test variable, which we've already created and is set to 20. And I just want to say, keep running until test is less than or equal to end. Okay, so test is less than or equal to end. Now what should happen here? Test is initialized with the value of 20. End is initialized with a value of 30. So what do we expect to happen for test less than or equal to end? Well, right now, we're not incrementing test. So it would just keep printing out the same thing over and over again forever. And we would have an infinite loop, because now test is never getting larger than end. Okay, so we can do another format, print line, let's, let's print test. Okay, so if I ran this right now, I think Go is smart enough to know that this is an infinite loop, so it wouldn't stop. So it probably just warned me. But if I print it out now, we would just keep printing the value of test over and over again. And since we're not doing anything else with the value of test, it would always be 20. Okay, so we don't want it to print forever. It's very unlikely you want your code to loop forever. So what we want to do instead is actually increment test. Okay, so just like before, test is an integer. So we can just increment with plus plus. Okay, so we still have our formats, we have our comments here, and we have for test less than or equal to end, and then format, print line, test, and then we increment test, or we add one to test. So what should happen here? Well, test is starting at 20. We're incrementing test. So the first time it's going to be a value of 20, it's going to print 20, and then it's going to increment, and then check, and 21 is going to be less than 30. So we're going to print again 21, and so on. And we're going to keep doing that until test becomes 30. And then it's going to stop. Okay, so let's go ahead and go run zero, zero, four. Okay, so we're starting at 20, which was the value of test. And we're ending at 30, because we printed, and then we incremented again, and we got 31. And then we checked, and then the loop stopped. So that's pretty much it for loops. And go, the main type of loop that you probably want to be familiar with is a for loop. You can either, with a for loop, you can either initiate all of the variables in the for loop definition, or you can just do a simple kind of variable comparison for the for loop. It's fairly flexible and fairly easy to use. Okay, so that's two ways to use a for loop in go. Thank you very much.