Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Uday DodiyaRank #239 · 10.3K reputationPosted Oct 27, 2022, 12:53 AM EDT
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.
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.
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); } } }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:
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--; } } }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:
using UnityEngine; using System.Collections; public class DoWhileLoop : MonoBehaviour { void Start() { bool shouldContinue = false; do { print ("Hello World"); }while(shouldContinue == true); } }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.
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); } } }Ijas AhamedRank #708 · 3.3K reputationPosted Oct 26, 2022, 2:21 PM EDT
Loops in programming are the way of repeating actions.
Abhishek YadavRank #217 · 11.3K reputationPosted Oct 27, 2022, 4:40 AM EDT
Loops in programming are the way of repeating actions.