how to use for loop code example
Example 1: loop through an array javascript
let array = ['Item 1', 'Item 2', 'Item 3'];
// Here's 4 different ways
for (let index = 0; index < array.length; index++) {
console.log(array[index]);
}
for (let index in array) {
console.log(array[index]);
}
for (let value of array) {
console.log(value); // Will log value in array
}
array.forEach((value, index) => {
console.log(index); // Will log each index
console.log(value); // Will log each value
});
Example 2: python loops
#x starts at 1 and goes up to 80 @ intervals of 2
for x in range(1, 80, 2):
print(x)
Example 3: for loop c
for(int i = 0; i<n ;i++) //i is the number of iterations that occur
{ //n is the total number of iterations
//code here
}
Example 4: for loop
for (let i = 0; i < array.length; i++) {
}
Example 5: for loop
for(int i = 0; i < some_number; i++){
//inside loop do whatever you want
}
Example 6: 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
*/