02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

The For Loop Syntax

In this video we will look into the basic structure of the "for loop". What are loops good for, are they only used to repeat things? (Nope, nope, nope. There much more then that).
Breaking down the for structure and understanding the 3 things a basic for loops expects(instantiation variable, condition and a stepper), endless loop and how to avoid them.

Video Transcript

00:01Let's look at the loop syntax and we're going to start with the most basic type of loop and the most used one
00:07Which is the for loop. There are a few different types of loops, but in the end they all
00:12Basically do the same thing. They just have a few different variations
00:17The for loop you'll see as you look at other people's code and you download code from the web
00:21Especially action script 2 or 3 code. You'll see it's the most used one
00:25So that's why it's well why from one reason it's a little bit easier to comprehend it a little bit easier to control
00:33So let's start with the syntax. So first of all, our our syntax is actually the word for which basically
00:39stands for our
00:42for loop and is
00:44The beginning where in we're going to open a round bracket and in that round bracket. We're going to give three different
00:51Different descriptions for flash the first one we're going to initiate a variable and we've done that in earlier videos
00:57But this time around we're going to initiate that variable inside of our for loop and the way we're going to do that
01:03We're just going to set an variable
01:04We're going to call it I and we're going to tell flashes from the type of an int because we know we are always
01:10going to use
01:11Full values we don't well true values
01:15More accurately
01:16Integers and we don't want to use any numbers that are between zero and one or such and we don't want to use any fractions
01:23So we know we're creating a variable. Well, which will be a type of integer and we want to set it
01:29To have some sort of initial value
01:32To do that. We're going to set a value of zero
01:35Alright, so we ready set our first step in our for loop
01:38To go to the next step before we could even close our bracket
01:41We're going to have to create an a separator and the create the way we're creating our separate is kind of similar the way to
01:47The way we broke a line and told flash that here we finished a line of executable code
01:54And we're doing that with the semicolon
01:57So we've set our first parameter our first set of our first
02:02base the initiation of a variable and the next step that we want to do is basically tell flash for how long should it
02:08Commit that loop and to do that. We're going to use another something that we've learned earlier, which was learning how to set
02:15different operators and in this operator we want to tell flash as long as I
02:20Doesn't match a certain value and even better off. We want to say it. Let's say as long as I is smaller than 10
02:28so for as long as I is smaller than 10 this for loop should continue looping and executing and
02:34Again, we're going to use a semicolon to let flash know that we just finished that
02:38second command that second line of information that our for loop needs and the third and last line of code that are
02:46For loop needs is to know
02:49Every time the loop completes every time a loop finishes
02:53What should that what should our for loop do to that?
02:56I value or to any other value in our scenario to keep things simple
03:00We want to take our eye and again, we're going to do something
03:03We've learned in an earlier video which basically we would just want to make our eye bigger in one value every time
03:09So basically here that I plus plus is the exact same thing as saying I plus equals one or I equals I plus one
03:17But it's just a standard in a nice clean way of just telling our for loop
03:21I plus plus which basically tells it take our zero in the beginning add a value of one to it and then continue
03:29Now let's close that bracket and we've done setting up our four values of setting up our for loop letting it know
03:36What are those parameters and our for loop should run now?
03:39And if we run it obviously we won't see anything because we're not really doing in anything inside of our for loop
03:45now similar to if commands we could just write ahead just
03:50Go down to make a little trace and say hello
03:55and if we do that and
03:58We run our application our flash should should then output ten times
04:02Hello, and we can see because flash is going to run from value of zero
04:06And then it's going to do zero and then one and then two and then three and let's see that in action
04:11So let me just add your I comma just so we could see the values as it's running
04:17And if we run it one more time and we scroll all the way up
04:21We're gonna see that it starts from zero and then one and then it loops again and two and three and four and five
04:26And so on and the I number grows each time
04:30Now as soon as it turns to nine it runs it one last time and when it ran that one last time it comes back up
04:36Before it's gonna well more accurately after it executed that nine it added
04:42One to our I so I turned to ten and when I turned to ten that value of I smaller than ten is not valid anymore
04:50Thus our for loop completed and it left that for loop
04:55now
04:57Traditionally, we don't really want to use a one line in our for loop and mainly not when you're just beginning to code
05:02And it's really one of those things that when you just get comfortable
05:05You might do that. I know that every formal book every formal documentation will tell you
05:10Avoid it and because it's confusing and it's and quite true that it's very confusing when you you're not used to the syntax
05:17so we want to make sure that you always use that open bracket and
05:21Close bracket just the same way as we did with our if statements in the same way that we've done it with our functions
05:26Basically telling flash that that for loop begins here and ends here
05:30Making sure that for us. So it's a visually that we know where it starts and where it ends
05:35Now by doing that it enables us to add more than one line of code
05:40Thus we could go inside of our for loop and add more than just one line and let's say for example
05:45We want to just add a trace
05:47Just a simple trace and thus every time the loop will run it will it will run two traces
05:54Alright, so let me run that one more time. So if we run it one more time
05:58we're gonna see that it's gonna run every time that
06:02Those two different traces until it hits that barrier of I equals 10 and now that I equals 10
06:07It's already not smaller than 10
06:10All right. So that's the basics of the syntax now
06:14Things that you want to notice and you want to make sure that you don't commit is you want to make sure that you don't create
06:19an endless loop
06:21Now before we're gonna look into endless loops
06:23Let's see what happens if we actually add a value for example
06:25We didn't have to start from zero
06:27Nor did we have to always add one if we wanted to we could have said to our I to be plus equals three and
06:34If we run an application one more time
06:37Now our application will actually run only four times because it's gonna start with zero
06:41Then it's gonna add three to it
06:43Then it's gonna add another three and another three and then when it hits nine
06:46It's gonna add three after that which equals twelve and twelve is bigger than ten
06:52Thus our for loop will note will only run four times and it will complete as soon as it ran four times
06:59Now in the other hand we could have if we wanted to we could have subtracted a value
07:02But before we do that, let's look at what would happen if we would set our I value to start from a different value
07:08We didn't have to start from zero. We could have started if we wanted to from minus 20
07:13All right. Now if I started it for minus 20
07:16What would happen that's still valid because we would start at minus 20 and every time we would add three to that value
07:23Thus as we run through our application from minus 20 17 and so on and so on until it hits 10
07:29Right because it equals 7 that works and it adds another three
07:33But it equals 10 now and now because it equals 10 our for loop
07:37Will cease will end to operate. All right, so let's set that back again to zero
07:43because it's quite a standard and you'll see in most scenarios most developers will start from zero and
07:4999% of the time you're going to always add plus one
07:52But that doesn't mean you have to sometimes in some scenarios you're gonna have scenarios where you need to change that around
07:59So you might have an operation that runs in this way, but let's see a different operation
08:04So this is a definitely a valid loop and a very standard loop
08:08Let's look at another loop if we would run our for loop
08:11But this time around instead of setting our I value to be zero. Let's set our IV
08:17Vr. I value to be a hundred and now we want to make sure that our I let's say is bigger than zero
08:23Right because now worry we don't want to put a value of a smaller
08:26Notary because it wouldn't be valid as soon as we would run it it would cease to run
08:31So we want to make sure that our eye is we're starting from a hundred as long as our eye is small is bigger than zero
08:37We want to let's say subtract five from our eye
08:42And again, I'm opening a curly bracket and closing a curly bracket and now that I've done that
08:47I could have a little trace here. So let me put here a trace that traces that our eye and
08:53Let's just trace out that I now what should happen here is as soon as our loop will run
08:58It's going to run first a hundred and then it's going to run down and subtract the value every time of five
09:04Until it reaches a scenario where I is no longer bigger than zero and let's see how that happens
09:10So if we run our application one more time, we're going to see it five is still true
09:14But after it subtracts from five another five it equals zero thus our I equals zero would not be true anymore
09:22Now let's see how we could create an endless loop
09:24Which is obviously saying you don't want to create because and to that point now that we're working with loops another note
09:29That is really important to note is
09:32Please always save
09:34Every single every time you make even a small change just save
09:38Before you run your application because you might create an endless loop and your flash will crash or something might happen and you will
09:45Lose your information
09:47So let's see how we could create an endless loop
09:49So if I'm going to run my eye again, and let's go to something that's relatively standard I equal zero
09:54And let's say I is smaller. Well instead of smaller. Let's say I actually said I equal zero
10:00All right. Now if I set my eye to equal zero and I'm going to make my eye
10:07Bigger equals three
10:11First let's run this this one and let's see what happens trace
10:15Am I called and am I called is called only one time. Why is that?
10:28First of all because our soon as our application runs
10:30It's it's already zero it runs our application at the value of zero because it's a true value right I equals zero
10:37It's called
10:38But then it does not call again because our eye is actually from now on bigger than zero
10:42Thus our for loop will cease to execute and when I say they will cease to execute by the way that means it's going
10:48Go over to the next line of code so after the loop
10:54And what does that mean?
10:55So after the loop is going to be called wise after the loop being called because after this for loop is executed after this
11:02Is completed and we are when we exit our for loop
11:07When we exit our far loop then our line of code continues in the same traditional way that we've seen so far
11:12Which was line by line?
11:14Continuing to implement a code. It doesn't leave the application
11:18It just leaves that for loop when it completes that for loop all right now
11:23Let's see how we could actually break our application
11:25So in this scenario, we said if I equals it well first we're starting from zero
11:29And then we're saying as long as I equals zero
11:32What would happen if we would say as long as I is bigger than zero?
11:36If we would say as long as I is bigger than zero our application would never complete in theory
11:41Right because that code will always be true our I value is always positive
11:46We're always adding a positive value to it thus our for loop will be stuck in saying that's called an endless loop
11:52So if we run our application one more time, and it's good that I saved it now because it might crash my flash and we
11:58run our application our
12:00Application is running and you could see that you know nothing is really going on
12:05What's going on here right and?
12:12That's a point of confusion flesh, and it looks quite nice here that it's not breaking our application
12:17But let's see let's try to go over this one more time and now we go now we see our problem okay first of all I
12:24Went under the assumption that we're always in a positive value, but that's not true right because we're starting from zero
12:30And as soon as our for loop started
12:33What happened we got we didn't we never we never went through the for loop because zero is not bigger than zero
12:40That's our for loop never executed so for me to correct that mistake
12:44I made I would set that or more accurately to break my application
12:48I would have to tell flash well as long as I is bigger or equal to zero or
12:53To set my I to be initially to be one
12:57Now if I run my application just one more time we will get that endless loop
13:02And you can see flash is not really responding
13:05And there's a little wheel going on there and flash is going crazy because so many commands are happening
13:10And there's no end to them and a
13:15For loop as this runs at some point flash is going to generate some sort of error or crash
13:19It really depends on the scenario, but hopefully it will give us some sort of an error
13:23And that's what you want to avoid when you're working with for loops
13:28And it's it happens especially when you're starting to work with for loops
13:31And that's what's so convenient about loops about the for command is that everything is outlined in the beginning of your for loop
13:39Those three different things that you have to make sure that they run correctly
13:43Now we went through all all of the items that are related to the for loop syntax itself, and we've looked into
13:51Where could we break the for loop and what is an endless loop?
13:55Now in the next videos we're actually going to try and see how could we actually use for loops besides repeating something?
14:02Mandain such as a trace command now. I'm not going to wait until flash
14:07until flash actually stops looping
14:10We're going to pause this video and when we start over again
14:13We're going to start from scratch, and we're going to look into seeing different usages of our for loop

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!