Looping let's you access each element in an array or a hash. For this tutorial we'll iterate through arrays.
var fruit = ['Bananas', 'Apples', 'Pomegranates', 'Oranges'];
function printFruit(element, index, array) {
console.log(element + " are really good!");
}
fruit.forEach(printFruit);
Output =>
Bananas are really good!
Apples are really good!
Pomegranates are really good!
Oranges are really good!
fruit = ['Bananas', 'Apples', 'Pomegranates', 'Oranges'] fruit.each do |element| puts element + " are really good!" end Output => Bananas are really good! Apples are really good! Pomegranates are really good! Oranges are really good!
For loops are used to execute a block of code a specified number of times.
var fruit = ['Bananas', 'Apples', 'Pomegranates', 'Oranges'];
for (var i = 0; i < fruit.length; i++) {
console.log(fruit[i] + " are really good!");
}
Output =>
Bananas are really good!
Apples are really good!
Pomegranates are really good!
Oranges are really good!
fruit = ['Bananas', 'Apples', 'Pomegranates', 'Oranges']
for i in 0..3
puts fruit[i] + " are really good!"
end
Output =>
Bananas are really good!
Apples are really good!
Pomegranates are really good!
Oranges are really good!
These loops are similar to regular for loops. You can specify what the index will start at. As it goes through the elements, the index will increase.
var fruit = ['Bananas', 'Apples', 'Pomegranates', 'Oranges'];
for (var i = 2; i < fruit.length; i++) {
console.log(fruit[i] + " are really good!");
}
Output =>
Pomegranates are really good!
Oranges are really good!
fruit = ['Bananas', 'Apples', 'Pomegranates', 'Oranges']
for i in 2..3
puts fruit[i] + " are really good!"
end
Output =>
Pomegranates are really good!
Oranges are really good!
While loops run a block of code and indeterminate nubmer of times. Every time it checks a condition to see if it's true. If it is, the code is run. If not, it skips the code block and continues the rest of the program. It's good to use when you don't know how many times you want to repeat the action.
var fruit = ['Bananas', 'Apples', 'Pomegranates', 'Oranges'];
while (fruit.length > 0) {
console.log(fruit.pop() + " are really good!");
};
Output => Oranges are really good!
Pomegranates are really good!
Apples are really good!
Bananas are really good!
fruit = ['Bananas', 'Apples', 'Pomegranates', 'Oranges']
while !fruit.empty? do
puts fruit.pop + " are really good!"
end
Output =>
Oranges are really good!
Pomegranates are really good!
Apples are really good!
Bananas are really good!
When the program hits a break statement, it terminates the loop. Any code after the break statement will not be run, because it jumps out of the loop.
var fruit = ['Bananas', 'Apples', 'Pomegranates', 'Oranges'];
for (i = 0; i < 4; i++) {
if (i == 2) break;
console.log(fruit[i] + " are really good!");
}
Output =>
Bananas are really good!
Apples are really good!
fruit = ['Bananas', 'Apples', 'Pomegranates', 'Oranges']
for i in 0..4
break if i == 2
puts fruit[i] + " are really good!"
end
Output =>
Bananas are really good!
Apples are really good!
When the program hits a continue statement, it jumps to the top of the loop and checks the conditional if there is one. Any code after the continue will not be run in the iteration.
var fruit = ['Bananas', 'Apples', 'Pomegranates', 'Oranges'];
for (i = 0; i < 4; i++) {
if (i == 2) continue;
console.log(fruit[i] + " are really good!");
}
Output =>
Bananas are really good!
Apples are really good!
Oranges are really good!
fruit = ['Bananas', 'Apples', 'Pomegranates', 'Oranges']
for i in 0..4
next if i == 2
puts fruit[i] + " are really good!"
end
Output =>
Bananas are really good!
Apples are really good!
Oranges are really good!