Gettin Loopy

Nichole Shelton and Soren Peterson


Looping through each element

Looping let's you access each element in an array or a hash. For this tutorial we'll iterate through arrays.

JavaScript

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!

Ruby

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 loop

For loops are used to execute a block of code a specified number of times.

Javascript

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!

Ruby

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!

Indexed for loop

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.

Javascript

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!

Ruby

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 loop

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.

Javascript

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!

Ruby

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!

Break

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.

Javascript

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!

Ruby

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!

Continue

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.

Javascript

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!

Ruby

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!