Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From \$16.50/m
FREELessons:16Length:2.2 hours

• Overview
• Transcript

# 2.4 Loops

Data is all around us. In the world of software development, we constantly need to dig through large amounts of data in many different forms. One of the most common forms is that of a collection, whether it is an array or some other form. In this lesson, I will show you how to use Go’s looping construct—the for loop. I’ll also show you how Go does away with while loops with some special for loop syntax.

## 4.Conclusion1 lesson, 01:32

### 2.4 Loops

Let's take a few minutes to talk about loops within Go. Now I say a few moments, because it's actually a pretty short conversation compared to some other languages. But let's go ahead and jump into it, and I'll show you what I mean. So let's start off by creating a function, and we're just going to call this basicFor. And within here, I want to do a pretty common operation. So let's loop through the first some number of integers, and let's add them all together and let's return the sum. So if I wanted to do that, actually I'm gonna need to return an integer. And let's start by creating a sum. So I'm gonna use my shorthand notation and initialize sum to be 0. And now I wanna loop through say the first ten numbers and add them all together, assign them to sum, and then I wanna return that value. So how do we do that? Well, for loops within the world of Go are very similar to for loops in many other languages. They're broken up into three parts. We kinda have that initialization step, then we have that conditional check. And then we have some sort of host step where we're doing something with the value that we initialized typically earlier on. So the format is pretty similar. We have a for, and then in the world of Go, we don't put the open and close parentheses. We just leave them wide open here, so let's start off by doing the initialization step. So that's going to be i, and then we're going to initialize that to be, let's say, 1. And then we use a semicolon, and then we do our conditional steps, where we're gonna say, while i is, say, less than or equal to, so we can do up to and including 10. And then every time we finish our body, I want to increment. So as you can see I've introduced a couple of different things here that we've seen in other programming languages. We have the conditional operation here to check for less than. You can have greater than. You can have greater than or equal to, less than or equal to, and so on and so forth. And we also have this incrementing syntax here, the double plus sign, and I'm gonna show you another one here in just a moment. So once we initialize our variable, we check to see that it meets the conditional criteria. And then we're gonna execute our body, which is going to be sum, and then I want to increment sum by the value of i. So I could say, sum = sum + i, but then just like in many other programming languages, we have another construct where I can say sum += to i. So that's gonna do the same thing. And then once I've finished this, I'm going to increment my value of i. I'm gonna come back and check to make sure that it meets this conditional criteria, and then I will execute this again. And if in any point, I get to a number of i that is outside of this range, then I'm simply going to jump down, and I'm going to return sum. That's it, so this is probably very similar to for loops in many other languages if you have experience with them. And then I can run basic for, so let's save that. We'll say, go run Loops.go, and I get 55. So I added together the numbers 1 through 10 and got 55. So this seems pretty basic, nothing too out of the ordinary. But there are some things you can do with Go with respect to for loops that you maybe can't do with some other languages. So in this case, what I can do is I can actually start to trim out the values or the steps in the for loop logic, where we have the initialization, the conditional, and the incrementation. So now I'm gonna come down and I'm gonna say function, trimmedFor, and I'm going to say int here. And so now within this logic, I can do something very similar, where now I can skip some of those steps if I so choose. So I could say something like initializing number to be equal to 1, and I could go through my for logic again. But in this case, let's say I wanted to use the num variable that I've created here, and I don't wanna have to initialize another variable. I don't have to. I could simply put a semicolon here, and it's going to skip over that step as long as I have this semicolon here. So then I can say while num is less than 10, then I wanna do something. And depending on what that do something is I could also skip over this final step here of my post step. So if I were to come in here now and put in some logic in here, I could say num += num. So as you as can see, I've now incorporated this concept of my post step, this incrementing the changing of whatever value is that's in my conditional. I've put it in the body of my for loop, so now this num is changing by a certain value. And then we're gonna come back up in here, there's no incrementation or post step. We'll check my conditional again and try to come down here and run again. And then I could simply return num. Now while I could definitely do this and I could run it and show you and I'll show you that in a second, Go wants to be as efficient as possible. So it's going to want to tell me that I don't need to use these semicolons, because all of these steps are being skipped. So it's gonna wanna get rid of them. So I can actually get rid of these and use this kind of shorthand notation, but this is kind of interesting now. So if you've ever looked at a while loop before in other languages, you would see while some number is less than 10, and I wanna do something. So this all of a sudden now is looking strikingly similar to a while loop in other languages. And that's one of the reasons why I said the conversation becomes pretty short when you talk about loops in the Go programming language. Because as you see here, this for loop has been able to get trimmed down to really be a while loop using a for keyword. So kind of an interesting concept but definitely one worth noting. So I have some bad syntax here. I think I lost some of my formatting here, so let's go ahead and cut this return, ring it up in here. We wanna return num, and I believe this is a bad spot here. Okay, let's save that, so now I can run my trimmedFor, save that. Run my loop and now it goes up through 16, because we are adding to number for each number up to, as long as number is less than 10. So there you have it. We have now covered really the basic crux of what a for loop looks like, whether it's a for loop or a while loop within the Go programming language.

Back to the top