Loops
Tutorial
·
Beginner
·
+10XP
·
5 mins
·
(161)
Unity Technologies

How to use the For, While, Do-While, and For Each Loops to repeat actions in code.
Languages available:
1. Overview
In programming it is often necessary to perform the same operation multiple times. This is commonly achieved using a loop. There are multiple types of loop and in this tutorial we will be looking at:
- For loops
- Foreach loops
- While loops
- Do-While loops
There are also some special keywords that can help manipulate loops. This tutorial will also cover how to use them.
2. Before you begin
New to Unity?
If you’re new to Unity, welcome! The Unity Essentials learning pathway has been designed to help you get set up and ready to create in the Unity Editor. We recommend you complete this pathway before continuing with Beginner Scripting.
Review the Unity Editor basics
If you need to refresh your memory of the Unity Editor basics, you can take a moment to review Explore the Unity Editor at any time.
3. For loops
For loops are probably the most common type of loop. They are used to iterate a fixed number of times. Their syntax is best explained through an example.
A for loop always starts with the keyword for. This is followed by parentheses. Within these parentheses there are 3 statements separated by 2 semicolons between them.
- The first statement is executed once when the loop starts and is typically used to create a local variable used to count through the iterations of the loop. In the above example the statement is:
int i = 0;
- The second statement must evaluate to a boolean. It is checked before each iteration through the loop. If it evaluates to true then another iteration of the loop will happen. If it evaluates to false then the loop will finish. This can be thought of as “for so long as X is true, the loop will continue.” In the above example, the condition is:
i < iterationCount;
This means that so long as the variable i is less than iterationCount then the loop will continue.
- The last statement is what happens after each loop. This is typically to modify any variable introduced for iterating, in this example, the variable i. In this example, the last statement is:
i++
This means that after each iteration of the loop, i will increase by 1.
The statements in parentheses are followed by a code block surrounded by curly braces. Whatever code is within this block will be run for each iteration.
This can be a bit overwhelming and like there is a lot to remember. Fortunately the overwhelming majority of for loops use something very similar to the above example. They introduce an iteration variable, typically called i; they check that i is less than some number; and then they increment i by one.
4. Foreach loops
Foreach loops are similar to for loops but work specifically to iterate over a collection such as an array or List. Here is an example of a foreach loop.
The syntax for a foreach loop is the keyword foreach followed by parentheses. Within the parentheses there is the declaration of a variable which is the element of the collection for that particular iteration of the loop. Following the parentheses is a code block surrounded by curly braces. Whatever code is within the block will be run for each element of the collection.
In the above example the code within the loop will execute 3 times, the first time the variable called target will be equal to Vector3.zero, the second time it will be equal to Vector3.one and lastly it will be equal to Vector3.up.
5. While loops
While loops are ones that continue iterating until the provided statement is false. Here is an example:
The syntax for a while loop is the keyword while followed by parentheses. Within the parentheses there is a condition that evaluates to true or false. So long as the condition is true, the while loop will continue repeating. They do not have provided syntax for counted iteration and so are commonly used when the exact number of iterations are unknown.
However, this can cause issues when their conditional statement is not carefully constructed. If the statement never evaluates to false, then the loop will never exit and cause execution to hang. In the above example, if nothing within the loop changes the active state of the GameObject then the loop will never exit.
6. Do-While loops
Do-while loops are very similar to while loops. The difference between them is that the content of the loop is always executed at least once. The condition is then checked and so long as it evaluates to true, the loop continues. Here is an example of a do-while loop:
As with the while loop, you must make sure that the code within the code block of the loop must change the condition to false so that the loop can exit.
7. Keywords for loops
Sometimes it can be efficient or useful to adjust what happens in the iterations of a loop. In order to do this, you can use two keywords: break and continue.
Break
The break keyword is similar to the return keyword but works for loops instead of methods. As soon as the break keyword is encountered, the loop will finish and execution will continue from after the loop. Here is an example:
In this example, there is a collection of a custom class called Target. The code is looking to find one of the targets that is active. It creates a variable called selectedTarget to store whatever it finds. It then iterates through the collection and checks each target. If the target it is checking has its isActive field set to true then selectedTarget will be set to that target and then the loop will exit using the break keyword.
Continue
The continue keyword is similar to the break keyword in that it stops an iteration through the loop. However, it then continues execution from the end of the loop, checking any conditions and then potentially performing another iteration of the loop. Here is an example:
In this example, the code is looking for all the targets that are active so that they can be detonated. The loop starts by using the iterator variable i to select each target in the collection. It then checks if the target is not active. If it is not active then the continue keyword is encountered. This will take execution to the end of the loop where the iterator variable i will be incremented. Then the loop will check if i is still less than targets.Length and if it is, start another iteration. If the target was active then the continue keyword would not be encountered and so the Detonate method would be called on it.
8. Summary
In this tutorial you learned about one of the most essential and powerful tools for programming: loops. Becoming familiar and comfortable with loops is very important for efficient programming. Make sure to carefully consider what type of loop you might need for each situation and if there is any code you could skip over for efficiency using the break and continue keywords.