Site icon Teaching Newbies since 2014

Introduction to C# in Unity3D #9: For Loops ➿

For loop

For loop

Introduction to C# in Unity3D #9: Loops ➿
➿ Syntax:
for (initialization; conditional; iterator statement) {
    //code block 
Initialization: The initialization statement initializes a variable that will be local to a for loop this means that it cannot be accessed outside the loop. It initializes the starting value of the counter variable
➿ Condition: The conditional statement is a boolean expression that will return either true or false. If an expression evaluates to true, then it will execute the loop else the loop is exited from
➿ Iterator: The iterator statement will increment or decrement the counter variable which we initialized earlier
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeController: MonoBehaviour
{
    int num;
    // Start is called before the first frame update
    void Start()
    {
        for (num = 1; num <= 5; num++)
        {
            Debug.Log("The value of num is " + num);
        }
        Debug.Log("Exited the for loop");
    }
    // Update is called once per frame
    void Update()
    {

    }
}
Exit mobile version