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.

For loop example applied to ukramedia.com text later in After Effects

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

}

Three for loop statements in After Effects

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

}

i equals 0 in after effects for loop example

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

}

i is less than 5 for loop in after effects

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.

i is 0 in the first loop in after effects

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.

i is giong to be 1 in after effects

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.

For Loop in After Effects Ukramedia

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

n plus i after effects for loop

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;

}

for loop starts at 2 after effects

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.

Running UM five times in After Effects for loop

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;

}

Add plus i to UM in After Effects For Loop

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 + ” “;

}

0 to 4 in after effects for loop

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.

Random For Loop in After Effects

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.

math.round(random()) for loop in after effects Ukramedia

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.

random numbers between 0 and 9 for loop after effects

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.

New Line For Loop in After Effects\

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:

3 and 12 in after effects for loop

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.

0 to 4 in after effects for loop

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.

same thing in After Effects for loop

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.

Skip by two in After Effects For Loop

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 + ” “;

}

Increase by 4 in after effects for loop

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.

the split and charAt() expression in After Effects
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.

Grabbing index 6 which is Seven in After Effects
Then it is grabbing the first letter of that word, so that is why we see capital S above our numbers.

Grabbing s in after effects

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.

Duplicates of the same thing in After Effects

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.

grabbing different letters in After Effects

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.

Select 5 and pickwhip to the source text in After Effect

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.

For Loop Ukramedia After Effects

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.

About the author:

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.

5 FREE TIME-SAVING TOOLS

Over 100,000 downloads & counting!

These 5 After Effects tools will seriously speed up your workflow and save you hours of wasted time. More than that, they could be the difference between being able to complete a project on time or not!