Loops in Javascript vs Python vs Ruby
Published: 2023-06-08 17:55:32

Loops are programming constructs that allow you to repeat a block of code multiple times. They are used to automate repetitive tasks, iterate over collections of data, or execute a certain code sequence until a particular condition is met. Loops help in writing more efficient and concise code by reducing redundancy.

In general, loops consist of three main components:

**Initialisation:** It involves initializing the loop control variable(s) with an initial value. This step is typically performed before the loop starts.

**Condition:** The loop executes the code block repeatedly until a specific condition is evaluated as false. The condition is checked before each iteration. If the condition is true, the loop continues; otherwise, it terminates.

**Update:** After each iteration, the loop control variable(s) are updated to a new value. The update step ensures progress towards the termination condition, allowing the loop to eventually exit.

The three most common types of loops are:

**For Loop:** The for loop is used when the number of iterations is known or can be determined in advance. It consists of an initialisation, condition, and update statement within its syntax.

**While Loop:** The while loop is used when the number of iterations is uncertain, and the loop continues until a specific condition becomes false. It evaluates the condition before each iteration.

**Do...While Loop:** The do...while loop is similar to the while loop, but it executes the code block at least once before evaluating the condition. It ensures that the code inside the loop runs before the condition is checked.

Below we have a Javascript implementation

```
// Example: Iterate over an array
const fruits = ["apple", "banana", "cherry"];
for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}

// Example: Iterate over a range of numbers
for (let i = 1; i <= 5; i++) {
  console.log(i);
}

// Example: Print numbers from 1 to 5
let count = 1;
while (count <= 5) {
  console.log(count);
  count++;
}

// Example: Ask for user input until a valid number is entered
let userInput;
do {
  userInput = prompt("Enter a number:");
} while (isNaN(userInput));

// Example: Iterate over object properties
const person = {
  name: "John",
  age: 30,
  city: "New York",
};

for (let key in person) {
  console.log(key + ": " + person[key]);
}

// Example: Iterate over an array
const fruits = ["apple", "banana", "cherry"];
for (let fruit of fruits) {
  console.log(fruit);
}
```
Below we have a Python implementation
```
# Example: Iterate over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

# Example: Iterate over a string
message = "Hello"
for char in message:
    print(char)

# Example: Iterate using range
for i in range(1, 6):  # Iterates from 1 to 5 (inclusive)
    print(i)

# Example: Print numbers from 1 to 5
count = 1
while count <= 5:
    print(count)
    count += 1

# Example: Ask for user input until a specific condition is met
user_input = ""
while user_input != "quit":
    user_input = input("Enter a value ('quit' to exit): ")
    print("You entered:", user_input)

# Example: Break and continue statements
for i in range(1, 10):
    if i == 5:
        break  # Terminate the loop when i equals 5
    if i % 2 == 0:
        continue  # Skip even numbers
    print(i)
```
Below we have a Ruby implementation

```
# Example: Iterate over an array
fruits = ["apple", "banana", "cherry"]
for fruit in fruits do
    puts fruit
end

# Example: Iterate using range
for i in 1..5 do
    puts i
end

# Example: Print numbers from 1 to 5
count = 1
while count <= 5 do
    puts count
    count += 1
end

# Example: Print numbers from 1 to 5 using until loop
count = 1
until count > 5 do
    puts count
    count += 1
end

# Example: Iterate over an array using each
fruits = ["apple", "banana", "cherry"]
fruits.each do |fruit|
    puts fruit
end

# Example: Execute code a certain number of times using times
5.times do |i|
    puts i + 1
end
```