Ruby - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Ruby Loops

Ruby Loop

shape Introduction

This chapter demonstrates about the Ruby Loop which can be used to execute same block code in several times, following are the concepts covered in this chapter.
  • Simple Loop
  • Types of Loops

Simple Loop

shape Description

In Ruby Loop the simple way to approach to make a loop by Ruby is utilizing keyword loop. Blocks are taken with loops, those are signified by { ... } or do ... end. Loop can be execute any code with in a block until user physically intercede with Ctrl + c or embed a break statement within the block, which can be forced to stop the loop and the execution will proceed after loop. The code below demonstrates the basic simple loop program as shown. [ruby] loop do puts "This will keep printing until you hit Ctrl + c" end [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below. In Ruby Loop User can control the loop execution by using the break keyword. The code below demonstrates the using of the break statement as shown below. [ruby] i = 0 loop do i += 1 puts i break # this will cause execution to exit the loop end [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below. In Ruby Loop break keyword is used exit from the loop at any point of code so after the break code will not execute. When user used the break keyword it will not exit from program only it will exit from loop. The code below demonstrates the using of break statement. [ruby] i = 0 loop do i += 2 if i == 4 next # skip rest of the code in this iteration end puts i if i == 10 break end end [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below.

Types of Loops

shape Description

In Ruby have different types loops which are used to perform different operations and are listed below. While While loop evaluates the given parameter into the Boolean value which can be either true or false. In which boolean expression get false result then while loop will doesn't execute and execution will continuous after the while loop. The snippet below demonstrates to executing the while statement as shown below. [ruby] x = gets.chomp.to_i while x >= 0 puts x x = x - 1 end puts "Done!" [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below. Do/While In Ruby Loop Do/while is same as the while loop but only difference is with in the loop code and will be executed only once which prior to the conditional check to verify if the code executed. Conditional check placed at the end of the do while loop which is exactly opposite to the beginning of the loop. The code below demonstrates the Do/While loop as shown below. [ruby] loop do puts "Do you want to do that again?" answer = gets.chomp if answer != 'Y' break end end [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below. until In Ruby Loop Until loop is opposite to the while loop in order to phrase the problem user need to substitute it in a different way the code below demonstrates the until loops as shown below. [ruby] x = gets.chomp.to_i until x < 0 puts x x -= 1 end puts "Done!" [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below. For For loops can be used on collection of elements which consist definite end and finite number of elements which starts with reserved word and follows variables in the reserved word and some collection of element. The code below demonstrates the for loops as shown below. [ruby] x = gets.chomp.to_i for i in 1..x do puts i end puts "Done!" [/ruby] Result By running the above code in command prompt, the output can be obtained as shown in the image below.

Summary

shape Key Points

  • For loop starts with the for key word.
  • Until is opposite to the while.
  • User can control the loop execution by break key word.