Answers for "for loop"

2

FOR Loop

For Loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times.
A for loop is useful when you know how many times a task is to be repeated.
Syntax : 
for(initialization; Boolean_expression; update) {
   // Statements
}
This is how it works : 

The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables and this step ends with a semi colon (;).
Next, the Boolean expression is evaluated.  If it is true, the body of the loop is executed. If it is false, the body of the loop will not be executed and control jumps to the next statement past the for loop.
After the body of the for loop gets executed, the control jumps back up to the update statement. This statement allows you to update any loop control variables. This statement can be left blank with a semicolon at the end.
The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats (body of loop, then update step, then Boolean expression).  After the Boolean expression is false, the for loop terminates.
Posted by: Guest on August-31-2021
4

for loop

// this is javascript, but can be used in any language 
// with minor modifications of variable definitions

let array = ["foo", "bar"]

let low = 0; // the index to start at
let high = array.length; // can also be a number

/* high can be a direct access too
	the first part will be executed when the loop starts 
    	for the first time
	the second part ("i < high") is the condition 
    	for it to loop over again.
    the third part will be executen every time the code 
        block in the loop is closed.
*/ 
for(let i = low; i < high; i++) { 
  // the variable i is the index, which is
  // the amount of times the loop has looped already
  console.log(i);
  console.log(array[i]); 
} // i will be incremented when this is hit.

// output: 
/*
	0
    foo
    1
    bar
*/
Posted by: Guest on December-16-2020
1

For loop

for( initialize; test; increment or decrement)

{

//code;

//code;

}
Posted by: Guest on August-22-2021
1

for loop

for i in range (some_number):
	#code goes here
Posted by: Guest on March-20-2021
0

for loop

for (let i = 0; i < substr.length; ++i) {
    // do something with `substr[i]`
}
Posted by: Guest on April-20-2021
0

for loop

for (int i = 0; i <= 5; i++)
{

}
Posted by: Guest on October-08-2021
1

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.
  
  	}
  
  }

}
Posted by: Guest on October-05-2020
2

for loop

for (int i = 1; i <= 10; i++) {
	printf("%d\n",i);
}
Posted by: Guest on August-29-2021
1

for loop

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

for fruit in fruits:
  print(fruit)
Posted by: Guest on September-25-2020
0

for loop

double serviceCharge = 0;  if (cost >= 150)  
{  
   serviceCharge = 0.15 * cost;  
}
Posted by: Guest on May-20-2021

Python Answers by Framework

Browse Popular Code Answers by Language