Deeper Look At The For Syntax
For loops are very flexible even though most of the time we will try to stick to the standard way of looping that we learned in "The For Loop Syntax" but now its time to break that loop apart to see what else can we do with the loop syntax.
Video Transcript
00:00To wrap up our basic discussion about the for loop and its different variations
00:06What I would want to do is show you a few different things other things you could do with the for loop a little bit
00:12More details into that syntax that we've seen before
00:15There's a couple things that I wanted you to notice and for one of them if we just create now a quick for loop
00:21And I'm just going to create a new variable. Well, let me just start up directly with creating a no
00:26Just creating that standard loop that we've created so far and let's say is smaller than 10 I plus plus
00:33And we've seen this loop already if quite a few times in different variations
00:40What else could we do first of all did we have to instantiate our variable directly in our for loop?
00:45The answer is no if I wanted to I could have instantiated that
00:49Variable it out of the loop if I wanted to I could have retrieved it from a method definition
00:55But so for the scenario. I'm setting up a hero could have been a value that came as a property of a method
01:01So because I said it already. This is what I have to do
01:04I just have to put a semicolon basically telling flash. I'm not really creating anything right now
01:09So let's see what would happen if I run this one more time one time
01:12we're going to see that everything is working the exact same way as it did before only thing I changed was that I took out the
01:19Definition outside of the for loop same thing if I wanted to I could have set the value of I inside
01:25without recreating a new variable
01:27alright, but that is um
01:30Kind of a similar scenario, so I'm just going to comment that out and show it as a commented out value
01:35Such as setting a value after you have already a set up variable such as if you're going to create a few loops
01:42All right, so that was the scenario where we have an external definition
01:47Let's see what other stuff we could do besides having an external definition
01:51Besides having an external definition. Let's look at the second parameter and see how we could modify that one
01:57So if again, I just grab that for loop and just create one standard one for a second
02:02That is similar to the way that we work so far
02:05Here we go so we have that basic for loop
02:08Let's look at the condition for a second does our condition have to be inside of that for loop
02:13No, is it recommended for it to be there very much so because it could become very confusing and that's one of the main
02:21Beauties of the for loop will see other for loops which are more simple
02:25But are much harder to manage versus the for loop, which is a very easy
02:30Type of looping system just because it has all the important things up in the beginning before you even start digging deep in
02:37Your code making it very easy to avoid
02:41Endless loops while other type of loops that we're going to see a little later on are
02:47Not as easy to to manage
02:50All right. So let's see how we could do that. So if I create a new variable outside is true. Let's call it just for now is
02:59Condition true
03:01I'm just going to set that just for a sake of example
03:05So I'm saying that if the condition is true and I'm basically instead of setting that up in here
03:09I'm just going to say is condition true
03:11What that would do is basically even create an endless loop, right because this is always true
03:16And if it's true, that means it's always going to go through
03:19So let me leave inside of that for loop
03:21Maybe change the logic out a bit and say if my I value is larger than five
03:27Then is true is condition true will be false just to make sure that we're not creating an endless loop
03:33Now if we run this application one time, we're going to see that it's going to run five times
03:38It's going to run from zero through four and then when it hits five
03:42It's going to set the value to be false or actually it's going to run six times, right?
03:46Because first it's going to trace that I and only after that it's going to set it to be false
03:50So let's see it should run from zero through five. Oh
03:53From zero through six. Let's see. Why is it running from zero through six?
03:56The reason is because our condition says if I is larger than five
04:00The six is is the first time that it's larger than five, but it's after we ready traced it out
04:06So you see the operation here actually added one extra step that wouldn't happen if our condition would be
04:13Actually triggered inside of our for loop before our loop began now, of course
04:18We if we wanted to we could have grabbed that line and put it up to happen before
04:22But that wouldn't change it because our loop would continue and run through
04:26All right. So now we saw how we could modify our second our second option inside of our for loop three different
04:34Things that our for loop expects now, let's try it again. Now, let's look at another option
04:40So if I again just grab our for loop one more time and just reset it to look like our original for loops
04:47just drag it down a bit and
04:50I'm just going to set your I is smaller than five or I is larger than five. So our loop will run and
04:57We'll get rid of this line
05:00All right, so we have I is larger than five and this is a completely standard for loop for us
05:07What I wanted to show is that you could add more definitions
05:10Let's say you wanted to initiate an I but you wanted to initiate other values as well
05:14And let's see when that would be a scenario what type of scenario would happen that would need that
05:19let's say for example
05:21you want to run a loop that would actually run from 0 through 10 and you wanted to output a
05:26Series of numbers that would show you the summary of the following digit. In other words
05:33Let's say we want our and this is obviously a very hypothetical option
05:37Let's say we want our for loop to actually create what to take an I value from I let's say we have an I am just
05:44Gonna put a comma here and add another value a type of integer as well, and I'm going to start it up at value 1
05:51now what I did right here is I
05:54instantiated two variables inside of my initial
05:57definition and
05:59I can at the same time do the same thing for my Jan and this scenario I equals 0 but J equals 1 and
06:06then I could do the same thing for my J. I could put a comma after my
06:10First stepper, and I could set it another another stepper, and it's completely legitimate
06:16Although this is very uncommon to see an object in in action script 3
06:20it's anything that you should be familiar with at some level because you might want to use it at some point or
06:25Probably you might see someone else using it, and you want to understand. What's going on there because as
06:32I've rarely ever used something like that even though. It's possible
06:37alright, and let's say what we wanted to do is we wanted to take the value of I plus J and trace that out and
06:42That would give us a series of numbers from
06:46basically
06:480 1 is
06:500 plus 1 equals 1 and then 1 plus 2 and then 2 plus 3 and 3 plus 4 and 4 plus 5 and
06:56So on and so on
06:57Now obviously, this is a very simple series, but if you want to create a mathematical series that takes the following number times
07:06you know I times J plus 3 minus 1 that's your series as you as if you remember from
07:13Mathematic lessons from high school of all these different random series now
07:18I don't know how valuable that would be in programming most of the time
07:22But you'll you might find a usage a real-life usage for a for loop in the scenario personally
07:27I don't think I've encountered a real-life scenario where I really needed something like that. I
07:32think writing in this way is probably a
07:35little bit confusing because it's so rare a
07:39Usage like this that I would probably avoid using it even though I would recommend knowing how to do it
07:44All right, so let's run this just to see how it works. We're going to see a lot of random numbers
07:50that is if we didn't make a mistake and let's see if we made some sort of mistake if
07:55We run this before we run this one more time. Let's see okay first of all. We're not getting an error. That's
08:01that
08:02Freezing our computer, so we must have your some sort of error
08:06That isn't related to that so let's see. What could it be first of all we have our instantiation of our eye that looks fine
08:13We have an instantiation of our J. That looks fine as well
08:18We have our eye is bigger than five and that's our problem
08:21I is not bigger than five right because it's starting from zero and that was our problem
08:26So we're going to make sure that I is smaller than five as long as I is smaller than five then our loop could run
08:32And if I run our application just one more time, and now we're getting the values that we are expecting which are completely
08:40Random, but they're not really what they are is I times J in the first round at zero and one so zero times one equals one
08:47plus three equals four minus
08:53One I made a mistake
08:55Okay, sorry zero times one is zero plus three minus one equals two right because zero times one is zero and
09:01the second time around because we're upgrading our I and J we're going to have one plus three but one plus two that G and
09:08One times two equals two plus three is five minus one is four and so on and so on
09:14Until it reaches that border where I then when I turns five it cannot run any further
09:20Alright, so we've seen a lot of different
09:23details into
09:26Options that we have with the for loop syntax I
09:29I would have to add a caveat to that that most of the times you really wouldn't want to use any of these options
09:36But they exist and you might encounter them if you download someone else's source code or maybe yourself
09:42You just really love one of these type of variable for loops and no matter how many
09:48Notices different documentation say not to use them
09:51And I'm a big supporter of if this really makes sense to you and it makes your coding
09:57Easier and better than use it obviously I would add some
10:01Documentation around it if it gets really confusing because when you look at this it gets a little bit confusing even if you don't want to add
10:08documentation
10:09Maybe you just want to break a line, you know just to make it more legible
10:13so even just doing something like this will make your for loop a little bit more legible and
10:19Even though it looks weird
10:20It's more legible because then people could see that okay
10:23This is the first definition in the second one the third one and it's a little bit easier to see okay
10:27We get what's going on here. All right, and there's no problem with with adding an enter
10:34Nothing will break
