Ruby Loop

Looping is an important concept in any programming language, and Ruby offers several ways to loop through your code. Here are some common looping constructs in Ruby:

1. while loop:

In a while loop, as long as the condition is met (i.e., it returns true), the code within the loop will keep executing:

counter = 0
while counter < 5
  puts "Counter is at #{counter}"
  counter += 1
end

2. until loop:

The until loop is the opposite of the while loop. It keeps executing the code within the loop until the condition becomes true:

counter = 0
until counter == 5
  puts "Counter is at #{counter}"
  counter += 1
end

3. for loop:

A for loop is used to iterate over a range or collection:

for i in 0..4
  puts "Number is #{i}"
end

4. loop method:

Ruby also provides a loop method for an infinite loop. You need to use a break statement to exit this loop:

counter = 0
loop do
  puts "Counter is at #{counter}"
  counter += 1
  break if counter == 5
end

5. .times iterator:

The .times iterator will run a loop a specified number of times:

5.times do |i|
  puts "This is loop number #{i}"
end

6. each iterator:

The each iterator is used to loop over collections, such as arrays or hashes:

["apple", "banana", "cherry"].each do |fruit|
  puts "The fruit is #{fruit}"
end

Remember that it's important to control your loops properly, especially infinite loops, to avoid crashes or freezes. If you're using a while loop or a loop method, always make sure there's a condition that will stop the loop at some point.

  1. Using the each loop in Ruby: The each loop is a common way to iterate through collections like arrays.

    array = [1, 2, 3, 4]
    array.each do |element|
      puts element
    end
    
  2. Ruby while loop example: The while loop continues iterating while a specified condition is true.

    i = 0
    while i < 5
      puts i
      i += 1
    end
    
  3. Nested loops in Ruby: Loops can be nested to perform more complex iterations.

    3.times do |i|
      3.times do |j|
        puts "#{i}, #{j}"
      end
    end
    
  4. Infinite loops in Ruby: Be cautious with infinite loops; they run indefinitely.

    loop do
      puts "This is an infinite loop"
    end
    
  5. Looping through arrays in Ruby: Arrays can be traversed using various loop constructs.

    array = [1, 2, 3, 4]
    for element in array
      puts element
    end
    
  6. Iterating over hashes in Ruby: Iterate through key-value pairs in a hash.

    hash = { name: 'John', age: 30 }
    hash.each do |key, value|
      puts "#{key}: #{value}"
    end
    
  7. Ruby loop break and next statements: Use break to exit a loop and next to skip to the next iteration.

    5.times do |i|
      break if i == 3
      next if i == 1
      puts i
    end
    
  8. Looping through ranges in Ruby: Ranges can simplify loop iterations.

    (1..5).each do |num|
      puts num
    end
    
  9. Looping through characters in a string in Ruby: Strings can be treated as enumerables for loop iterations.

    string = "Hello"
    string.each_char do |char|
      puts char
    end
    
  10. Comparing loop constructs in Ruby: Different loop constructs offer flexibility based on the situation. Choose the one that fits your needs.

    # Use each for arrays and enumerables
    array.each { |element| puts element }
    
    # Use while for condition-based iterations
    i = 0
    while i < 5
      puts i
      i += 1
    end
    
    # Use loop for infinite loops
    loop { puts "This is an infinite loop" }