Ruby Programming Language

Loops

By

First published on August 23, 2019. Last updated on February 13, 2020.


Loops allow us to repeat the same steps of code as many times as we desire without having to type that code multiple times.

A danger of loops that that a poorly coded loop can last forever. So we always wish to include a condition in the loop that will cause it to stop. The a condition can be for the loop to run a specific number of times, or to keep running until a specified event occurs.

In the code below, the word while create the condition.

[code lang=”ruby”]

x = 0

while x < 5
x = x +1
puts x
end
[/code]


COURSE


Content is copyright the author. Layout is copyright Mark Ciotola. See Corsbook.com for further notices.