This Will Throw You For A Loop

Billy Helm
4 min readSep 17, 2020

Growing up in the city, when someone starts talking about loops my brain immediately thinks of a highway. This helps me envision how a loop in Ruby works as well. There are many different types of loops and when you are first learning it can be difficult to understand how they work. Once you master them however you can be a mean driving Ruby machine. Using loops can help eliminate repetitive code and make complex tasks simpler.

Example of while loop
Example of while loop output
Example of do while loop

While Loops

The first kind of loop in Ruby is called a while loop. A while loop will take a conditional statement and keep looping until this statement becomes false. While loops are good to use when you do not know how many times to integrate through a program. Imagine driving down the highway while the gas tank is still full. Once the gas tank is empty (false) then it is time to exit. You need to remember to be careful with a while loop, however, because if the condition never comes back false it will endlessly loop. The do while loop is very similar to the while loop but the conditional statement is placed at the end of the loop. This is done so that no matter what, the code inside of the loop will be run at least one time. The example of the do while loop will go through the do statement and print out “We have gas, let's keep going”. The if statement will then return false ending the loop and the next line printed will be “time to get gas”.

Example of until loop

Until Loops

If you’re looking for the opposite of a while loop then you won’t be disappointed. An until loop is similar but will continue to loop through the code until the conditional statement returns true, at which point the loop will end. Let’s say you’re driving down the highway and you’re not sure how much further until your exit. You would keep driving until you saw your exit sign which in this example is 39. Once you see your exit sign you take the ramp and are now out of the loop. The example above would iterate 5 times, adding 1 to the exit number every time. On the 6th iteration, the conditional statement would return true and the program would exit the loop.

Example of for loop
Example output of for loop

For Loops

But what if you know exactly how many iterations you need to get your desired output? This is where a for loop can come in handy. Back in the world of driving down a freeway, this would be the equivalent of know how many exits you need to go even though you might not know the exact name. Street names can sometimes be confusing but it’s easy to remember that your parents live exactly 5 exits down the highway from where you live. On that 5th exit, you better get off the highway or you might miss dinner. The for loop and eliminate extra lines of code by automatically keeping a counter for you. In the example “a” is the counter and starts with a value of 27. After the first iteration, the for loop knows to increase the value of “a” to 28. It continues to do this until the value of “a” is 31. When “a” is 31 it iterates through the code one last time before exiting the loop and announcing “That was a quick 5 exits”.

Conclusion

The loop examples shown here are simplified examples to give you a good base understanding of when and how to use each one. Conditional statements inside of these loops and be much more complicated allowing for more functionality in your program. The code that is run inside of the loop can also be more than basic put statements. When you are ready to start building even more powerful loops, if statements along with other loops themselves can be nested inside of the executable code. Understanding how these loops work is a key principle for the next lesson where we will dive into how enumerables work.

--

--

Billy Helm

Full Stack Software Engineer with a background in Petroleum Engineering