Hey there stranger!

Sign up to get access.

Spreadsheet Automation 101 Lesson 2: For Loop

About this Tutorial

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.

Courses

Spreadsheet Automation 101: Introduction to Pre-course Videos

Breaking Through Errors In Apps Script

Think Like a Programmer: Develop The Mindset of an Apps Script Coder

Tips to Navigating Thousands of Lines of Code In Apps Script

Spreadsheet Automation 101: Functions

Spreadsheet Automation 101: Variables

Spreadsheet Automation 101: Dot Notation

Spreadsheet Automation 101: Camel Case

Spreadsheet Automation 101: Parentheses

Spreadsheet Automation 101 Lesson 1: GetValue - Introduction to SpreadsheetApp

Spreadsheet Automation 101 Lesson 1: Spreadsheet Taxonomy

Spreadsheet Automation 101 Lesson 1: A1 Notation vs Row,Column Syntax

Spreadsheet Automation 101 Lesson 1: getActiveSpreadsheet() vs getActiveSheet()

Spreadsheet Automation 101 Lesson 1: onOpen() Trigger - Custom Menu

This Seems Like Automation

Spreadsheet Automation 101 Lesson 2: Get Values - Introduction

Spreadsheet Automation 101 Lesson 2: Arrays

Spreadsheet Automation 101 Lesson 2: For Loop

Spreadsheet Automation 101 Lesson 2: Bracket Notation

Spreadsheet Automation 101 Lesson 2: Logger.log()

Spreadsheet Automation 101 Lesson 2: If ( ){ } and Checkboxes

Spreadsheet Automation 101 Lesson 2: onEdit() Trigger

Introduction to Spreadsheet Automation 101 Lesson 3

Spreadsheet Automation 101 Lesson 3: MailApp

Spreadsheet Automation 101 Lesson 3: Email Yourself For Loop

Spreadsheet Automation 101 Lesson 3: Send Email Every Week Trigger

Spreadsheet Automation 101 Lesson 3: Email Other People For Loop

Spreadsheet Automation 101 Lesson 4: Access APIs Introduction

Spreadsheet Automation 101 Lesson 4: UrlFetchApp

Spreadsheet Automation 101 Lesson 4: OmdbAPI get ApiKey, get Data in URL

Spreadsheet Automation 101 Lesson 4: OmdbAPI get data in Apps Script

Spreadsheet Automation 101 Lesson 4: JSON (beautifier) and OmdbAPI parameters

Spreadsheet Automation 101 Lesson 4: OmdbAPI Parameter Picker

Automatically Clear Content | Refresh Reuse Recycle Templates

Automate Google Sheets With Zero Experience

Automatically Uncheck A Daily Checklist

Activate A Certain Sheet When Opening a Spreadsheet

Scoping Functions in Apps Script