for loop in a for loop code example

Example 1: for loop

for (let i = 0; i < array.length; i++) {

}

Example 2: for loops

MyString = ""
for i in range(5):
  MyString += "hi"
print(MyString)

Example 3: for loop

fruits = ['apple', 'orange', 'peach']

for fruit in fruits:
  print(fruit)

Example 4: for loop

// Unity for loop coroutine (repeat something every x seconds for x time)

Coroutine doThisCoroutine; // (Create a coroutine for stopping)
int duration = 5 // (Duration, whatever you want it to be)
float waitTime = 1 // Wait time to iterate in seconds, usually 1

void Awake(){
  
  DoThisCoroutine = StartCoroutine(DoThis()); // Start the coroutine
    
}
    
IEnumerator DoThis(){
  
  while (enabled){ // While the behavior is enabled,
  
    for (int x = 0; x < Duration; x++){ // From zero until 5 incrementing by 1...
    
      print("Doing This!"); // Do whatever...
  	
      yield return new WaitForSeconds(waitTime) // Every second.
  
  	}
  
  }

}

Tags:

C Example