Getting loopy yet?

For loops! Not "for" everything!

For loops are a fairly simple loop that can be very useful but is not used very commonly among rubyist. They are somewhat primitive and don't offer a lot of flexibility but they share a fairly common syntax with many languages so are useful to know and definitely have their place in the world of ruby! Using for loops, you are able to repeat a command as many times as you would like, and can also vary its input depending on how many times you run it. Some common features of a for loop include:

  1. for: indicates the beginning of the loop
  2. variable: references the current iteration of the loop
  3. in: special ruby keyword mostly used in for loops
  4. 1..20: the range, low and high number that make up the range of numbers that you will be running through the loop, sequentially
  5. do: indicates the beginning of the block of code to be executed repeatedly
  6. end: ends the code block that started with do

            for varible in 1..10 do
              puts "I love writing ruby!"
            end
          

Times loops! Not always the right time!

Times loops are very similar to for loops and most rubyist's prefer to use them in this situation. They are often more compact than 'for' loops but it boils down to personal preference most of the time. The times loop has a different syntax than the for loop using a dot syntax (.times) instead of a keyword syntax, this has to do with how the times loop works under the hood, but is used in the same way as for/while/until loops. They can be used on any variable containing a number or on a number itself. Some common features of a while loop include:

  1. variable/number: references how many times to run the loop
  2. .times: specifies to use the times loop
  3. do: indicates the beginning of the block of code to be executed repeatedly
  4. action: block of code to be repeated (puts, etc.)
  5. end: ends the code block that started with do

            10.times do
               puts "I love writing ruby!"
            end
          

While loops! While not always the best, a great option!

While loops are one of the next common looping methods seen across programming languages. While loops execute a block of code as long as or while a condition is true. They will continue running until a false condition is given. The thing about while loops is that at some point they must evaluate to 'false' or else an infinite loop will form and your computer's console will crash and you will have to reset it. Beware of the power of the while loop! Some common features of a while loop include:

  1. variable: set a value for the loop to begin at
  2. while x > y: set values to begin at and to end at, while x is greater than y, continue looping
  3. x += 1: set an incrementer for the loop
  4. action: while this condition is true do this (puts, etc.)
  5. end: ends the code block that started with while

            a = 0
            while a < 25
              x += 1
              puts "Millions of peaches, peaches for me!"
            end
            

Until loops! Hopefully not until it hurts!

Until loops are almost identical to while loops except they work conversely. They will continue executing a block of code as long as it is false, until the condition is true. They will continue to loop until the condition is met or true. Yet again if no true condition is ever reached your loop will go until infinity and nowhere. Until loops are just as powerful as while loops but just say instead up do this while this is true, they say do this until this is true. Some common features of an until loop include:

  1. variable: set a value for the loop to begin at
  2. until x > y: set values to begin at and to end at, until x is greater than y, continue looping
  3. x += 1: set an incrementer for the loop
  4. action: until this condition is true do this (puts, etc.)
  5. end: ends the code block that started with until

            b = 0
            until b == 25
               b += 1
               puts "Millions of peaches, peaches for me!"
            end
            

Each loop: Each of us is important!

Each loops are one of the most commonly used loops in the world of ruby and probably one of the most useful. Each loops will take a list of variables like an array or hash, and perform the given task or block of code to each element in the list. Since most computer tasks use lists of variables and want you to do something to each element in that list each loops are very important to be familiar with. The syntax is quite different than the other loops we have covered but with a little practice it will be easier than you think. Each loops do use the dot syntax similiar to .times loops though so that should be easy to work with. Some common features of each loops include:

  1. array/hash: a list of variables/numbers/strings to iterate through
  2. .each: specifies to use the each loop
  3. do: indicates the value of the current variable that the loop is looking at
  4. |x|: could be anything, assignment for the current variable
  5. action: each element do this (puts, etc.)
  6. end: ends the block of code that started with .each

            family = ["Mark", "Kayla", "Lisa", "Ken"]
            family.each do |x|
              puts "I love #{x}"
            end
            

Which one is for right for your situation?

Choosing the right loop can be a difficult task in programming. As you can see in the examples I gave you, a lot of tasks can be accomplished using more than one loop. A lot of the choice is about whichever loops you feel more comfortable with, and whichever loops make more sense to you. Other than that if you are wanting to iterate over a list of numbers that you already have you will want to go with an each loop to iterate over each element within the list or array. If you want to do something and you know how many times you would like to do it then a for loop is a great option. If you are not sure how many times you would like to do an action but want to rely on a variable instead, then a times loop is a great alternative, both the for loops and times loops are great for looping through a list of numbers sequentially. While and until loops are great to do something a specified amount of times while being able to control the incrementer and not have to do it sequentially. As I said before a lot of different loop options can be used to acheive the same results but these are just a few tips to help you narrow down your options.