This course has been updated! We now recommend you take the Basics of Go course.
Transcript from the "Control Structures: For Loops" Lesson
[00:00:00]
>> Next on the list, we'll cover some details about for loops. And as we discussed at the beginning of class, the loop that does not exist is the do while. But there are some different ways we can use for loops to do some dynamic things within the logic.
[00:00:16] I am looking at the for.go file in the basic syntax directory. Once again, I'll be uncommenting the whole file and starting at the top. So our basic for loop is gonna look super similar to JavaScript. And this is one of the only occasions where you're gonna see semicolons hanging out in Go.
[00:00:37] And so if we have an integer, assign it to a value of one. Bar i assigned to one, we have three statements that instantiate a for loop. The first is. This one up here, we're setting the initial value of i to one. And then we're saying hey, keep going in this for loop until i reaches 100, is less than or equal to 100.
[00:01:00] And then for every iteration increment the variable by one. We wanna go ahead and run this code, open up our terminal. We're gonna go run, we are in basic syntax code and then our for loop. So when we run Go you'll see that our for loop has printed out all of the integers to 100.
[00:01:45] Option two is we can actually kill both of the statements to the left and the right. So on this top one, if we review the conditionals in the middle, for if i is less than or equal to 100. In the second version here, the behavior of this for loop will actually turn out kind of like a while loop.
[00:02:03] So while i is less than or equal to 100, do the stuff that's in the middle. So even though we don't actually have a while loop syntax, we can make a for loop behave a little bit differently. Save and rerun. And then this last one here, which is my favorite and a really powerful version of our for loop uses the range keyword.
[00:02:29] And so on line 29 we have a sentence. It is assigned to the value of this as a sentence. And then our for loop takes a couple additional arguments here. So we're kind of circling back to that structure that we saw with the if blocks, where we have a couple different variables on the left.
[00:02:47] And then we have our shorthand syntax with the colon equal sign. Which means that these variables are getting assigned to whatever's on the right-hand side. So indexing letter is getting assigned to the range keyword, which means, I'm gonna iterate over a thing. And then what are we iterating over?
[00:03:02] We're iterating over my sentence, which is a string. So Go is allowing us to treat a string kind of like a collection of things, like an array. And so we're iterating over every element which is in this string, so we're iterating over each of the letters. So once we're in the for loop here, I want to use the function print line function to print index.
[00:03:23] And then that actual index value, and then a letter. And then whatever thing we're looking at within our sentence. I'm gonna save this and run it. I wanna call attention to some interesting behavior that we're gonna see here. So we're iterating over the sentence. Our sentence is a series of letters, but you'll notice that the thing we're printing out is actually two numbers.
[00:03:43] So on the left-hand side, we're seeing a series of integers that increment, those are the index values. And then on the right-hand side, we're actually seeing integers, not letters, which is surprising. So behind the scenes, similar to how when you're talking about what from to print line returns.
[00:03:58] We're talking about bytes here, we're not actually talking about the letter itself. And this is where type conversion can come back and really handy. So if I actually do want the string version of my letter. We can circle into this for loop here, and then wrap our letter in the word string.
[00:04:23] Run this again.
>> Think you need one more-
>> Thank you, I do need one more parentheses. So now you'll see that actually the more expected behavior of the index on the left and then the letter on the right. Takeaways from this particular structure though, is really to keep an eye on that range keyword.
[00:04:42] Range indicates, okay, we wanna iterate over something. I don't have a set number of conditionals. We're not working with a classic for loop like this. I want to kind of for each my way around a particular thing, in this case it's a sentence.