---
title: "Loops"
content_type: tutorial
url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b?language=en"
language: en
container_type: course
container_title: "Beginner Scripting"
container_url: "https://learn.unity.com/course/beginner-scripting.md?language=en&version=2019.3"
standalone_markdown_url: "https://learn.unity.com/tutorial/loops-z2b.md?language=en&version=2019.3"
previous_title: "IF Statements"
previous_url: "https://learn.unity.com/course/beginner-scripting/tutorial/if-statements?language=en&version=2019.3"
previous_markdown_url: "https://learn.unity.com/course/beginner-scripting/tutorial/if-statements.md?language=en&version=2019.3"
next_title: "Scope and Access Modifiers"
next_url: "https://learn.unity.com/course/beginner-scripting/tutorial/scope-and-access-modifiers?language=en&version=2019.3"
next_markdown_url: "https://learn.unity.com/course/beginner-scripting/tutorial/scope-and-access-modifiers.md?language=en&version=2019.3"
difficulty: beginner
duration_minutes: 5
unity_version:
  - "2019.3"
  - "2019.2"
  - "2019.1"
  - "2018.4"
  - "2018.3"
  - "2018.2"
  - "2018.1"
  - "2017.4"
  - "2017.3"
  - "2017.2"
  - "2017.1"
  - "5.x"
  - "4.x"
supported_unity_versions:
  - name: "2021.3"
    url: "https://learn.unity.com/tutorial/loops-z2b.md?language=en&version=2021.3"
  - name: "2019.3"
    url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b.md?language=en&version=2019.3"
  - name: "2019.2"
    url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b.md?language=en&version=2019.2"
  - name: "2019.1"
    url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b.md?language=en&version=2019.1"
  - name: "2018.4"
    url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b.md?language=en&version=2018.4"
  - name: "2018.3"
    url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b.md?language=en&version=2018.3"
  - name: "2018.2"
    url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b.md?language=en&version=2018.2"
  - name: "2018.1"
    url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b.md?language=en&version=2018.1"
  - name: "2017.4"
    url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b.md?language=en&version=2017.4"
  - name: "2017.3"
    url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b.md?language=en&version=2017.3"
  - name: "2017.2"
    url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b.md?language=en&version=2017.2"
  - name: "2017.1"
    url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b.md?language=en&version=2017.1"
  - name: "5.x"
    url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b.md?language=en&version=5.x"
  - name: "4.x"
    url: "https://learn.unity.com/course/beginner-scripting/tutorial/loops-z2b.md?language=en&version=4.x"
last_updated: "2026-09-30T20:35:08.000Z"
---

# Loops

Use For, While, Do-While, and For Each loops in C# to repeat actions in your code efficiently.

How to use the For, While, Do-While, and For Each Loops to repeat actions in code.

Previous: [If Statements](https://learn.unity.com/tutorial/if-statements?projectId=5c8920b4edbc2a113b6bc26a)

Next: [Scope and Access Modifiers](https://learn.unity.com/tutorial/scope-and-access-modifiers?projectId=5c8920b4edbc2a113b6bc26a)

## 1. Loops

[Video](https://connect-mediagw.unity.com/h1/20220104/videos/baa7cab6-13a9-4355-8b6d-a6c0f8c9b3dd_U_Beginner_5_Loops.mp4)

## **ForLoop**

```csharp
using UnityEngine;
using System.Collections;

public class ForLoop : MonoBehaviour
{
    int numEnemies = 3;
    
    
    void Start ()
    {
        for(int i = 0; i < numEnemies; i++)
        {
            Debug.Log("Creating enemy number: " + i);
        }
    }
}
```

## **WhileLoop**

```csharp
using UnityEngine;
using System.Collections;

public class WhileLoop : MonoBehaviour
{
    int cupsInTheSink = 4;
    
    
    void Start ()
    {
        while(cupsInTheSink > 0)
        {
            Debug.Log ("I've washed a cup!");
            cupsInTheSink--;
        }
    }
}
```

## **DoWhileLoop**

```csharp
using UnityEngine;
using System.Collections;

public class DoWhileLoop : MonoBehaviour 
{
    void Start()
    {
        bool shouldContinue = false;
        
        do
        {
            print ("Hello World");
            
        }while(shouldContinue == true);
    }
}
```

## **ForeachLoop**

```csharp
using UnityEngine;
using System.Collections;

public class ForeachLoop : MonoBehaviour 
{   
    void Start () 
    {
        string[] strings = new string[3];
        
        strings[0] = "First string";
        strings[1] = "Second string";
        strings[2] = "Third string";
        
        foreach(string item in strings)
        {
            print (item);
        }
    }
}
```
