
Lesson Description
The "Loops" 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 codes a few examples using loops. The syntax of a for loop is similar to other programming languages with an initial value, condition, and a statement that's run on each iteration. However, Go does not have an explicit while keyword for while loops. Instead, the for-loop syntax is used but with a different signature.
Transcript from the "Loops" Lesson
[00:00:00]
>> Melkey: For loops and go, are relatively simple. I would say, there's a few gushes here, but we can use the for keyword. So I'm gonna show you one way to create a for loop. We can use the for keyword, and then we're gonna create a variable in scope I.
[00:00:15]
I'm gonna set I to 0, and then it's going to be the condition of I, which will be evaluating on every iteration. Say so, while i is less than 5 could be a way of phrasing this and then the increment. So, we wanna increase I per iteration in the for loop, okay?
[00:00:32]
And here, we can just say format print line say this is I, okay? So if I run this, you can see we have this is I 0, 1, 2, 3, 4.
>> Melkey: We do have a recommendation from the compiler to change this. We're gonna ignore this for now.
[00:00:51]
We will look into this, but I just wanna show kind of all the states of a for loop, I suppose. While loops, I'm assuming everyone is aware of the while loop they've used while loop probably like a keyword, while something like Python I think has the while keyword.
[00:01:11]
I think, maybe I'm wrong, but typically language may have a while loop. So Go doesn't have a while keyword, this does not exist. To do a while loop and Go, it's simply a for loop, okay? So let's say we have this variable called counter. We can set counter to 0, and then for a while loop.
[00:01:28]
It's simply just for counter and, essentially the condition of the while loop. So, this basically saying while counter, less than 3. So we just use the for loop instead. Now, careful if you run this like so, you can have an infinite loop. So always add the conditions we can do counter ++ which will increment our counter to make sure it adhere to the while loop.
[00:01:52]
And if you want you can do fmt.Println( "this is the counter") to visualize what that is. You don't have to do this, am gonna write here. You can see we have our first loop, this is the for loop right here. And then we have the while loop down here.
[00:02:06]
In go, this is also valid just this. If you just put a for loop like this this is going to work. This is an infinite loop, this is an explicit infinite loop. It's designed for the case where you need one. I say that loosely because you'd want to have a break, right?
[00:02:23]
You typically have this infinite loop running, running, running until some condition is met, and then you have break to break the for loop cuz you don't want something to run, I guess, infinitely. Maybe in some cases you do, but this is how you do an infinite loop. So, for this, again, simple, similar to the counter up above, you can do iterations=0.
[00:02:43]
Put that into the for loop. You can say if iterations is greater than 3, we can break, right? And then right here, we can just put iterations plus plus and we can print it as well. So that's gonna be a way for us to use infinite for loops without having to actually brick our computer.
[00:03:05]
And there's also the continue keyword as well in for loop. So we have break, we also have continue. So if a condition is met, you can continue through the for loop. But these are, I would say, pretty basic interest to looping,
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops