TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Unity Loops

Unity Loops with Introduction, Installing, GameObject, First Unity Project, Unity 2D, Sprite Unity, Loops, If Statement, Data Types, Swith Statements, Unity UI, Unity Asset Store etc.

<< Back to UNITY

Loops

Loops in programming are the way of repeating actions. Let?s look at three different kinds of the loop, a For loop, a While loop, and a Do While loop.

While Loop

The ?while loop? is used to perform an action, while a condition is met.

To make a while loop, start with the keyword ?while? followed by brackets. Within the brackets, you must write a condition. Whenever the condition is true, the code within the loop block will be executed:

Syntax:

while(condition){
     //loop block
}

Example:

Let?s see an example. In this example, we have a variable called PlayerLives. We have a total four life of a player. While the number of life of a player is more than zero, the player can play the game. Means, this loop will continue four times because the player has four lives. And after that, the Player will lose the game.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WhileLoop : MonoBehaviour
{
        int PlayerLives = 4;

    void Start()
    {
        while (PlayerLives > 0)
        {
            Debug.Log("You have only " +PlayerLives +" Life");
            PlayerLives--;
        }
        Debug.Log("You lost the Game");
    }
}

Output:

While Loop

Do While Loop

The do-while loop is almost identical to the While loop, with one major difference. While loop tests the condition before the loop body, however, do-while loop tests the condition at the end of the body. This difference means that the body of the do-while loop is guaranteed to run at least once. Here is the syntax of the do-while loop:

Syntax:

do{
 //body of a loop 
} while(condition);

Example:

In this example, we can see that we have a Boolean variable called shouldContinue. This variable is set to false. Next, we have the Do while loop. We start with the keyword do follow by open and closed braces, whatever the code is between these braces makes up the body of the loop. After the body ?while keyword? is there followed by the condition. In this case, the loop would only continue while the variable shouldContinue is equal to true.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoWhileLoop : MonoBehaviour
{
    void Start()
    {
        bool shouldContinue = false;

        do
        {
            print("Welcome to the Game");

        } while (shouldContinue == true);
    }
}

Output:

Do While Loop

For Loop

?For Loop? is probably the most common and flexible loop. The ?for loop? works by creating a loop with a controllable number of iterations. Functionally it begins by checking conditions in the loop. After each loop, known as an iteration, it can optionally increment a value.

The syntax for this has three arguments. The first one is iterator; this is used to count through the iterations of the loop. The second argument is the condition that must be true for the loop to continue. Finally, the third argument defines what happens to the iterator in each loop.

Syntax:

for(int i = 0; i<10; i++){
//loop block
}

Example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ForLoop : MonoBehaviour
{
    int numEnemies = 3;

    void Start()
    {
        for (int i = 0; i < numEnemies; i++)
        {
            Debug.Log("Creating enemy number: " + i);
        }
    }
}

Output:

For Loop

For Each Loop

The foreach loop is very simple and easy to use. It has the simplest syntax. The foreach keyword followed by brackets is used in this loop. You must specify the type of data you want to iterate inside the brackets.

Pick a single element variable name, give a name to this variable whatever you want. This name is used to access this variable inside the main loop block. After the name, write in a keyword, followed by our List variable name.

Syntax:

foreach(Type element name in myCollectionVariable){
	//block of code
}

Example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ForEachLoop : MonoBehaviour
{
    void Start()
    {
        string[] names = new string[3];

        names[0] = "JavaTpoint";
        names[1] = "Nikita";
        names[2] = "Unity Tutorial";

        foreach (string item in names)
        {
            print(item);
        }
    }
}

Output:

For Each Loop




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf