Menu Close

Articles

Ruby: Repetition

This is the second most important statement in a programming language. With decisions and repetitions you can resolving a problems. There are languages that do not have these statements: XML, HTML, and CSS.  We say these languages are not “Turing complete“. 

While loop

A repetitive statement is often called loop. Diverse kind of loops are controlled by different methods. A loop can repeat forever if you are not careful. This may be an error made by beginners. If you make this error, do not be intimidated. Just press Ctrl+C to kill your process.

Example:

While loop is executed until a control condition become false.

Note: A lot is happening in this short example:

  1. I have used “while” keyword in ways to create loops, 
  2. First is actually a block with “while” augment,
  3. Second is the proper loop controlled by variable “n”,
  4. There are two early exit points: abort() and exit(),
  5. The “print” statement is similar to “puts” except it does not add new line.

Homework: open this example live and run-it 2 times: while loop

  • first time use a correct number: 5,
  • second time use 9: it will ask again,
  • no enter 0: this will abort the program.

Until loop

The until statement is similar to the while statement in functionality. Unlike the while statement, the code block for the until loop will execute as long as the control condition evaluates to false. When this condition become true the loop will stop.

Example:

This example should look familiar by now. It is similar with previous one.

Note: In my opinion “until” is redundant, but in Ruby you can do same thing in many ways. In previous example you can observe the loop display numbers in reverse order and the last comma is not display.

Homework: run this example: until loop

Single line:

You can create a loop in a single statement. Your program will look very strange for someone who do not understand Ruby. The next code fragment will read input values until the value is right:

Note: Previous example accept input and validate against range (1..5). If the value you enter is in range the statement terminates otherwise is waiting for the next input until you get it right.

For loop

This kind of loop is sometimes called iteration. It will repeat a statement or a group of statements of specific number of times. It is controlled by a range of values. Same loop can also iterate over items of a collection.

Example:

Note:

  1. Range notation include both limits,
  2. Range can start from negative number,
  3. Reverse range do not exist actually,
  4. Keyword “do” is optional but is a good practice to use it.

Homework: open example live and run it: for range

Array traversal

You can use “for” loop to visit all elements from an array. Actually this kind of loop is very similar with previous. The only difference is the loop is controlled by the collection. When all elements of collection are visited the loop will stop.

Example:

Note: In this example:

  • we use a range and method “to_a” that will create and initialize an array object,
  • statement “print” is intelligent, it can print an array using notation: [ 0,  1, 2, …],
  • inside the loop we print the elements one by one separated by space.

Homework: Open this example live and run it: for-each

Interruptions

If you are familiar with other computer languages you should know any loop statement can be interrupted earlier. This is done with additional keywords that can be used with conditionals to control nested loops or complex situations:

  • break: stop execution of inner most loop,
  • next: jumps to the next iteration of the most internal loop,
  • redo: restarts this iteration of the most internal loop,
  • retry: can be used to reset the iterator of most internal loop.

Example

Notes:

  1. operation (x % 2) will return zero if 2 is divisor of x,
  2. this program do not require input and will produce same output every time is run.

Output: To verify the output you must run it: next iteration

Next reading: methods