# For Loop Statement in After Effects

In this post, we will over For Loop in After Effects. So without any further ado, let’s dive right in!

Alright, so we are in After Effects and for this example, I have a For Loop Expression applied to the Source Text of
my UKRAMEDIA.COM text layer.

So For Loop has three statements within itself and these Three Statements determine how often this expression is run.

for (statement1; statement2; statement3) {

expression to be executed

}

So for example, the first statement is something that is executed before the loop starts. A lot of times you see a variable and it equals a number.

A lot of times you see i equals 0:

for (i = 0; statement2; statement3) {

expression to be executed

}

What that does is just basically determines when your loop starts. So if i is 0, then your loop is going to start at 0. By the way, a variable can be anything.

My variable is i in this example but it can be really anything that is JavaScript safe.

Alright, so the second statement is where you define the condition for running the loop.

For example, we know that i is 0 and so we can replace the second statement2 and we can say as long as i is less than let’s say 5, we will just keep running this loop.

So as long as i is less than 5, we are going run this expression and as soon as it becomes 5 or greater then we stop the expression.

for (i = 0; i < 5; statement3) {

expression to be executed

}

Now the third statement is executed each time after the loop has been executed. A lot of times you see something like this:

for (i = 0; i < 5; statement3) {

expression to be executed

}

You see i++, which means it incrementally will increase by 1. Let me show you what I am talking about here.

First, I am going to set a variable n and I am going to say n, you are going to equal to an empty string like so:

n = “”;

for (i = 0; i < 5; statement3) {

expression to be executed

}

Then I am going to say n, you are going to += to i. So you are going to be this i number

n = “”;

for (i = 0; i < 5; statement3) {

n += i;

}

Now if I run this, i is going to become 0 in the first loop. So that is why we see 0.

Then when it is done, it is going to go to the second loop and in the second loop, i is going to become 1.

Then the third loop is going to become 2. That is why we see 2 and then 3 and 4. When i becomes 5, you can see that it stops because again 5 is not less than 5 and so essentially that is what it does.

Now you might be wondering what is this plus equal means?

Plus equal is an assignment operator and it allows you to add something to a variable and then store the result in that variable. It is kind of essentially the same thing as saying n, you are going to equal to n + i.

n = “”;

for (i = 0; i < 5; i++) {

n = n+i;

}

So if I let go, you can see it is the exact same thing

But I am going to undo this and just keep it as what it was before:

n = “”;

for (i = 0; i < 5; i++) {

n += i;

}

I was telling you about how i = 0. That is like the starting point of your loop, so now we start at
0 and so if I change this number to 2, you can see that it will start at 2 instead.

n = “”;

for (i = 2; i < 5; i++) {

n += i;

}

I hope this makes sense. Now let’s take it a bit further here. So I am going to change i back to 0 again and then instead of saying n+=i,  let’s do something else. I am going to say let’s add a string “UM”

n = “”;

for (i = 0; i < 5; i++) {

n += “UM”;

}

So if I let go, it is going to run UM five times.

So I can add to that I can say n += “UM” + i; and then it is going to add a number right after the UM.

n = “”;

for (i = 0; i < 5; i++) {

n += “UM” + i;

}

Next, we can say + = ” “ and it will give a space in between our numbers like so:

n = “”;

for (i = 0; i < 5; i++) {

n += “UM” + i + ” “;

}

So that is cool but let’s try something else. Let’s do a random expression here. So I am  going to say: n += random();

n = “”;

for (i = 0; i < 5; i++) {

n += random();

}

You should now see a bunch of numbers.

Now we need to round it all up. So I am going to say Math.round(); and then I’ll put random() inside my parentheses like so:

n = “”;

for (i = 0; i < 5; i++) {

n += Math.round(random());

}

If I let go, now we are getting something.

So we are getting some random numbers. So inside of random(); I am going to say give me a random number between 0 and 9 like so:

n = “”;

for (i = 0; i < 5; i++) {

n += Math.round(random(0, 9));

}

So now we are getting an interesting feel here.

So that is kind of cool, You can definitely see how useful this can be and by the way, we can keep going with this. We can copy for (i = 2; i < 5; i++) { and paste it above like so:

n = “”;

for (i = 0; i < 5; i++) {

for (i = 0; i < 5; i++) {

n += Math.round(random(0, 9));

}

We have to change our variable to something else because we already have one in here. So I am going to use j because it kind of looks like i.

n = “”;

for (j = 0; j < 5; j++) {

for (i = 0; i < 5; i++) {

n += Math.round(random(0, 9));

}

So we can run a For Loop within the For Loop. So we say this For Loop for (j = 0; j < 5; j++) { is going to run this For Loop for (i = 0; i < 5; i++) { plus or you know whatever the variable comes out of that and n is going to plus equal to a new line.

n = “”;

for (j = 0; j < 5; j++) {

for (i = 0; i < 5; i++) {

n += Math.round(random(0, 9));

}

n+=”\n”;

}

If I let go right now, you can see that it kind of puts it on a different line so you can definitely see how useful that is going to be.

You can change the numbers to something like 3 and 12.

n = “”;

for (j = 0; j < 3; j++) {

for (i = 0; i < 12; i++) {

n += Math.round(random(0, 9));

}

n+=”\n”;

}

And you should get something like this:

I hope so far it is making sense to you. I know For Loop is a bit difficult to understand at first but let me show you another practical example for this. However, before we get to our next example, I want to keep going with this statement 3.

I want to show you more what it can do, so right now we are saying that i is going to increase incrementally by 1. That is why we see UM0 UM1 UM2 UM3 and UM4.

So with each loop it increases by 1 because we see this i here n+=”UM” + i + ” “;

n = “”;

for(i = 0; i < 5; i++){

n+=”UM” + i + ” “;

}

Now it’s not much different than saying it this way. So if you say += 1:

n = “”;

for(i = 0; i < 5; i+=1){

n+=”UM” + i + ” “;

}

If you let go right now, it is the same thing.

So we are saying i increase by 1, now what if I wanted for it to increase by 2? So right now it is UM0 then the next loop would be plus UM2 so it would skip UM1 so then it would be UM4 because it would also skip UM3. How would I do this? Easy!

All I have to do is change the number 1 to 2.  Like so:

n = “”;

for(i = 0; i < 5; i+=2){

n+=”UM” + i + ” “;

}

Now it will increase by 2 instead of 1.

You can also increase it by 4 and so on. So if you increase it by 4, it will go from 0 to 4.

n = “”;

for(i = 0; i < 5; i+=4){

n+=”UM” + i + ” “;

}

So you kind of get the idea of how useful this can be.

Alright, so for this next example, we are going to use the Splits and charAt() methods.

So if you do not know anything about these two, definitely check out tip number 35 and tip number 37 where I go over the CharAt() and the Split Method in After Effects.

So essentially what this expression thisComp.layer(“WORDS”).text.sourceText.split(” “)[6].chartAt(0) is applied to the Source Text of the LETTERS text layer.

So it is applied to the LETTERS text layer and it is grabbing the WORDS text layer and all these words (One Two Three Four Five Six Seven) inside of our composition.

Then it splits all these words into an index and then it is grabbing the index number 6, which happens to be Seven. Remember, the index count starts with 0. So we count 0, 1, 2, 3, 4, 5, 6 and it is grabbing that 6th word, which happen to be Seven.

Then it is grabbing the first letter of that word, so that is why we see capital S above our numbers.

So now what I want to do is instead of grabbing one word, I want to grab all of them. Also, I want to print the first letter of each word instead of just one. That is where a For Loop comes in handy!

First, I am going to create a variable n that will equal to an empty string like so:

n = ” “;

And then we are going to do our For Loop. So I am going to define our variable n and say for (i=0;)  and the second one is going to be the condition.

So I am going to say as long as i is less than 5 and then we are going to run hisComp.layer(“WORDS”).text.sourceText.split(” “)[6].chartAt(0) expression.

So as long at 5 is less than 5, this expression thisComp.layer(“WORDS”).text.sourceText.split(” “)[6].chartAt(0) is going to be working.

n = ” “;

for (i = 0; i < 5; )

thisComp.layer(“WORDS”).text.sourceText.split(” “)[6].chartAt(0)

Then after that, we are going to say increase incrementally by 1 with  i++ and we need to open curly bracket { to run this expression and then we will close the curly bracket } like so:

n = ” “;

for (i = 0; i < 5; i++ ){

thisComp.layer(“WORDS”).text.sourceText.split(” “)[6].chartAt(0)

}

Also we are going to add something else before thisComp.layer(“WORDS”).text.sourceText.split(” “)[6].chartAt(0).

We are going to say n you are going to plus equal to thisComp.layer(“WORDS”).text.sourceText.split(” “)[6].chartAt(0).

n = ” “;

for (i = 0; i < 5; i++ ){

n += thisComp.layer(“WORDS”).text.sourceText.split(” “)[6].chartAt(0)

}

If we run this right now, you can see we just have duplicates of the same thing because essentially what it is doing is
running this line: thisComp.layer(“WORDS”).text.sourceText.split(” “)[6].chartAt(0) over and over five times.

That is why we see it five times. Now we want this index [6] to change, so the first time is going to run, we want it to be 0 and then the second time it runs, we want the second word and third and so on.

In order to do that, we are going to change this index [6] of the word that it is grabbing to [i] because i is changing each
time.

n = ” “;

for (i = 0; i < 5; i++ ){

n += thisComp.layer(“WORDS”).text.sourceText.split(” “)[i].chartAt(0)

}

Okay so if I let go now, you can see that it is grabbing every single one until it runs this for (i = 0; i < 5; i++ ){ condition.

So how do I set this up to where instead of me manually setting how many words I want it to grab, I want it to always see all of my indexes. How do I make that happen? Let me show you!

So I am going to select 5 and I will pick-whip to the Source Text of our WORDS text layer.

n = ” “;

for (i = 0; i < thisComp.layer(“WORDS”).text.sourceText; i++ ){

n += thisComp.layer(“WORDS”).text.sourceText.split(” “)[i].chartAt(0)

}

And we will also do the split by saying split all the text into index split(” “).  I want it to split where space is ” “.

n = ” “;

for (i = 0; i < thisComp.layer(“WORDS”).text.sourceText.split(” “); i++ ){

n += thisComp.layer(“WORDS”).text.sourceText.split(” “)[i].chartAt(0)

}

So now it is going to split all of them into separate indexes and then I want to see all of those indexes. So I am going to say give me the length.

n = ” “;

for (i = 0; i < thisComp.layer(“WORDS”).text.sourceText.split(” “).length; i++ ){

n += thisComp.layer(“WORDS”).text.sourceText.split(” “)[i].chartAt(0)

}

So it is going to tell me how many indexes there are. Essentially, it will give me all of them. If I let go, you can see that it is working quite well.

I hope you found this post useful. If you would like to learn more about Expression and how it can speed up your workflow in After Effects, check out my Learn After Effects Expressions Course.

#### Sergei Prokhnevskiy

Sergei Prokhnevskiy is a Charlotte, NC based motion graphics designer, an online tutor, an entrepreneur, and Adobe MAX Master Speaker. Sergei is known for his quick and practical After Effects tips and tricks on YouTube, where he generated well over 6 million views on his tutorials. He has authored an online video course on After Effects Expressions and developed scripts for After Effects. He is also a former senior motion graphics designer at Fox Sports where he worked on graphics for NFL, Super Bowl, MLB, World Series, College Football, NASCAR, Women’s World Cup, and NASCAR Race Hub. He is very passionate about learning and teaching others.