Loops
Loops are a type of program flow that repeat blocks of code. The ability to repeat a block of code allows us to do things such as processing collections of data. Typically, loops will run until a condition is satisfied. For instance, a loop may be run until it has run a certain number of times, or a loop may be run until it has processed all the items in a collection of data. Let's look at the following types of loops in Ruby:
while/do
loopsuntil/do
loopsdo/while
loops
The while/do Loop
Another foundational concept for program flow is being able to repeat sections of code until a condition is met. These are called loops and in Ruby there are many ways to create loops. The most basic structure of a loop contains two things:
- The condition that will be evaluated to determine whether to repeat the code
- The block of code to be repeated
Here is a simple block using the while
keyword:
while true do puts Time.now end...