 This tutorial is available for all users. Start with a 7 Day Free Trial today and get access.

# Hey there stranger!

Let's not be strangers any more. Start a FREE TRIAL today to view this tutorial, right now.

# Spreadsheet Automation 101 Lesson 2: For Loop

Learn about the syntax of the For Loop. Find out how to do fantastic automations.

Video Transcript

0:00 Hello. So this video we're gonna get into some pretty interesting coding here called the for loop. It has a very specific syntax, which I wanted to go over in this particular video and introduce it to you.
0:13 It also involves if we want to use it to its sort of conclusion to, to help us will also use bracken notation, which I have in another video.
0:24 But here we'll just talk about the syntax of the four loop and then we'll make it a little bit more interesting.
0:30 So let's talk about, let me show it to you in one way. So the four loop, we have four and we use a number or a variable called I I is usually the one that's used first.
0:45 And then you can always do four loops inside of a h loops. So they'll use I then j, then K.
0:50 In this case we're only gonna do one. We have three parts of the four loop, which are in parentheses. And then whatever we have in there, then we execute it between the curly brackets.
1:01 And what happens in the curly brackets will be iterated upon time and time again. And the three parts of this will help us know how many times to go through I equals zero, meaning the start, where do we start?
1:16 Then we wanna know where do we end? Will we end when I is less, which when this expression turns out to be false.
1:24 So as long as it's true, we continue to go. So I is less than the length of the array will be.
1:31 If it is, we'll keep going. And then if we keep going at the end of the run, how do we know to go to the next I?
1:39 Well, we do I plus plus, which adds one to I. So I starts at zero. It runs through the curly brackets, it adds one, it checks, Hey, are we still okay to continue running?
1:51 And so that's how an array loop works. Let's, let's type it out. So four, let's actually write a function four loop.
2:01 There we go. We need some curly brackets. And inside, here we go. Four with the parenthesis I equals zero. Semicolon is used as a delimiter here we wanna do I is less than five.
2:17 Then let's give all of this a little bit of space with some spaces around everything so we can read it easier.
2:24 We need one more thing, which is space i plus plus, which just adds one to I each time it goes around.
2:30 We also need the curly brackets. All right? Now once we run through the curly brackets, what we're gonna go logger dot log, which I will get into even deeper in another video.
2:40 But all we wanna do is log I for right now. And let's see what happens, right? We're starting an I, we are going until I is not less than five, right?
2:50 We have less than five here and we're just gonna keep adding I. So let's run the four loop and we'll see down here, 0, 1, 2, 3, 4.
2:59 So I starts at zero, then it goes, okay, we're still less than five. Add one, we're still less than five.
3:06 Add one, we're still less than five. We're add one, right 0, 1, 2, 3, 4. We can do I times, you know, 55 right here.
3:15 And we'll see. Let's run that and see, even though the number that it returns is 55, I is still less than five.
3:26 So it continues going through. This is very, very useful with arrays. If we have a, let's say variable new array is equal to, let's do first, second, and third logger dot log.
3:49 We can log array dot length, and let's see what happens there. Let's just do this run. The very first number here is three.
4:01 This array dot length. This is three items long. There's three elements inside of this array. So what we can do is we can go through an entire array by just saying I is less than array dot length.
4:16 So this will go three times through, right? Let's see, oh wait, we have to, I think we saved it 3 0 1 2.
4:23 So the very first item of an array is zero, then it's one, then it's two. So this is actually really good.
4:29 This is just showing you I zero one and two. So this will get us, if we use BRCA notation again, that'll be in another video.
4:36 If we use BRCA notation right here, array I, we will get all of the parts of the array. Isn't that cool?
4:47 So I will go deeper into brack notation in the next video, and I'll also go deeper into logger. But now that you have the syntax of the for loop, we have some pretty magical things we can do, some really fantastic automations that we can do and we're gonna get to a lot of them with oned.
5:03 This is a really cool section. Hope you enjoy it. Bye.

Better Sheets