Ruby Loops
Loops are used to repeat a task a number of times. A loop runs a set of instructions or functions a given amount of times as long as a condition is met. The most common loop in Ruby is the for loop. Other loops are while, until, and do...while.
For Loops
This loop is utilized when you know beforehand how many times you want to repeat the task.
for i in 1..5 doputs "Message number #{i}"end
- The
forkeyword indicates the beginning of a loop. iis our variable.- The
inkeyword indicates the range of numbers you want to loop over. - The
dokeyword indicates the instructions you want to repeat. - The
endkeyword indicates the end of the loop.
The output would be:
Message number 1Message number 2Message number 3Message number 4Message number 5
While Loops
The for loop is used when you know how many times you want to repeat the task, the while loop is used when you don’t know how many times you want to repeat the task.
i = 1while i <= 5 doputs "Message number #{i}"i = i + 1end
- The
whilekeyword indicates the beginning of a loop. i <= 5indicates that the loop will continue as long asiis less than or equal to5.- The
dokeyword indicates the instructions you want to repeat. - The
endkeyword indicates the end of the loop.
The output would be:
Message number 1Message number 2Message number 3Message number 4Message number 5
Do…While Loops
The do...while loop is used when you don’t know how many times you want to repeat the task. The while loop is used when you want to check a condition before the first loop. do...while loops are used when you want to check a condition in the loop body after executing the statements. the do...while loop will run at least once.
i = 1loop doputs "Message number #{i}"i = i + 1if i == 6breakendend
- The
loopkeyword indicates the beginning of a loop. if i == 6is the condition that will break the loop.- The
dokeyword indicates the instructions you want to repeat. - The
endkeyword indicates the end of the loop. - The
breakkeyword indicates the end of the loop.
The output would be:
Message number 1Message number 2Message number 3Message number 4Message number 5
Until Loops
The for loops, while loops, and do...while loops run until a condition is no longer true. The until loops run until a condition is true.
i = 1until i == 6 doputs "Message number #{i}"i = i + 1end
- The
untilkeyword indicates the beginning of a loop. i == 6is the condition that will end the loop when true.- The
dokeyword indicates the instructions you want to repeat. - The
endkeyword indicates the end of the loop.
The output would be:
Message number 1Message number 2Message number 3Message number 4Message number 5
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Ruby on Codecademy
- Learn to program in Ruby, a flexible and beginner-friendly language used to create sites like Codecademy.
- Beginner Friendly.9 hours
- Learn the basics of building applications with this convenient and powerful web development framework.
- With Certificate
- Intermediate.6 hours