1for(int i = 0; i < some_number; i++){
2 //inside loop do whatever you want
3}
1for( initialize; test; increment or decrement)
2
3{
4
5//code;
6
7//code;
8
9}
1// this is javascript, but can be used in any language
2// with minor modifications of variable definitions
3
4let array = ["foo", "bar"]
5
6let low = 0; // the index to start at
7let high = array.length; // can also be a number
8
9/* high can be a direct access too
10 the first part will be executed when the loop starts
11 for the first time
12 the second part ("i < high") is the condition
13 for it to loop over again.
14 the third part will be executen every time the code
15 block in the loop is closed.
16*/
17for(let i = low; i < high; i++) {
18 // the variable i is the index, which is
19 // the amount of times the loop has looped already
20 console.log(i);
21 console.log(array[i]);
22} // i will be incremented when this is hit.
23
24// output:
25/*
26 0
27 foo
28 1
29 bar
30*/
1for (let i = 0; i < substr.length; ++i) {
2 // do something with `substr[i]`
3}
4
1For Loop
2A 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.
3A for loop is useful when you know how many times a task is to be repeated.
4Syntax :
5for(initialization; Boolean_expression; update) {
6 // Statements
7}
8This is how it works :
9
10The 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 (;).
11Next, 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.
12After 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.
13The 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.
1// Unity for loop coroutine (repeat something every x seconds for x time)
2
3Coroutine doThisCoroutine; // (Create a coroutine for stopping)
4int duration = 5 // (Duration, whatever you want it to be)
5float waitTime = 1 // Wait time to iterate in seconds, usually 1
6
7void Awake(){
8
9 DoThisCoroutine = StartCoroutine(DoThis()); // Start the coroutine
10
11}
12
13IEnumerator DoThis(){
14
15 while (enabled){ // While the behavior is enabled,
16
17 for (int x = 0; x < Duration; x++){ // From zero until 5 incrementing by 1...
18
19 print("Doing This!"); // Do whatever...
20
21 yield return new WaitForSeconds(waitTime) // Every second.
22
23 }
24
25 }
26
27}
28
1 double serviceCharge = 0; if (cost >= 150)
2{
3 serviceCharge = 0.15 * cost;
4}
5