Loops are used in a number of programming languages to perform a similar task multiple times. For example, if we wanted to print out the name of every employee in a list, we may want to use a loop. Or if we wanted to capitalize every string in a list, we may also want to use a loop.
In this tutorial, we are going to break down the basics of Ruby while
loops. These types of loops can be used to execute specific code until a certain condition is met.
Loop Refresher
The most basic way to create a loop in Ruby is to use the loop
method. The loop method takes a block of code, which is defined within curly braces, and will execute any code within that block. The loop will keep going until you press Ctrl + C
(Cmd + C on Mac)
to execute a break, or insert a break
statement in the code.
Here’s an example of a loop in Ruby:
loop do puts "Hello world!" end
When we run our code, Hello world!
will be printed until we interrupt the program:
Hello world! Hello world! Hello world! Interrupt: …
Indeed, we have created an infinite loop. Each time our loop executes, our program goes through a loop iteration
, and it will not stop until we execute an interrupt. We can use a break
statement to stop our loop when an action is executed. Here’s an example of a loop with a break statement:
i = 0 loop do i += 1 break end puts i
Our program returns: 1
. In this case, our program stops running after the loop has executed once because there is a break statement in place. If we wanted our program to run five times, we could enclose our break in an if
statement like this:
i = 0 loop do i += 1 if i =< 5: break end end puts i
Our program returns the following:
1 2 3 4 5
As you can see, our program stops as soon as it has been executed five times. But there is a more efficient way to write this loop. By using a while
loop, we can make our code cleaner and simpler.
Ruby While Loop
A while loop is a loop statement that will be run when a boolean expression is true. For example, a while loop may be run until a counter reaches 10, or until another condition is met. The while loop will stop as soon as the boolean expression is equal to false.
Here is an example of a while loop that will count up to five and print the count as it goes:
i = 0 while i < 5 puts i i += 1 End puts "The counter is complete."
Our code returns the following:
0 1 2 3 4 5 The counter is complete.
Let’s break down our code. On the first line, we define a counter variable, i
. Then, on the next line, we create a while loop that will run until our counter is equal to five. As soon as our counter reaches 5
, the boolean expression will become false
, and our program will stop.
Then, on the next line, our program adds one to our counter, and then the program prints out our counter.
Finally, as soon as the loop has been executed five times, the loop stops because i
becomes equal to five
, and so our conditional is false. But before i
is equal to five, our conditional statement is true which means our loop runs. Thus, after i
reaches five our final statement, The counter is complete.
is printed.
Do While Loop
Do/while loops work in a similar way to while loops, but with one key difference. The code in a do/while loop is executed one time before a conditional check is run. Here’s an example of a do/while loop that will be executed until a user enters BREAK
:
loop do puts "Enter BREAK to stop the program. Enter anything else to keep going." response = gets.chomp if response == "BREAK" break end end
Here’s the output of our code:
Enter BREAK to stop the program. Enter anything else to keep going. NOBREAK Enter BREAK to stop the program. Enter anything else to keep going. NOBREAK Enter BREAK to stop the program. Enter anything else to keep going. BREAK
As soon as we typed in BREAK
, our program stopped.
Until Loop
Until loops, the opposite of the while loop, are not as common in practice but can be useful in some situations. Here’s an example of an until loop:
i = 0 until i > 5 puts i i = i+1 End puts "The program has been run."
Our code returns the following:
0 1 2 3 4 5 The program has been run.
As you can see, our program runs until i
is greater than 5
, after which point the program breaks and moves onto the next line of code.
Conclusion
While loops allow you to perform an action while a certain expression is equal to true. In this tutorial, we have discussed the basics of loops in Ruby. In addition, we outlined how to use a while loop, a do/while loop, and an until loop, and discussed where they may be useful. Now you’re ready to start using loops like a Ruby expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.