Ruby Loops

Loops are used to iterate and execute group of statements under the certain number of times. There are many statement are available which are define loop statement and control the execution of loop. But loop execution are based on particular condition. When condition are true then loop execution are possible otherwise terminates loop execution. There are following statement are used to define loop statement such as while statement, until statement and for statement etc.

While Loop

While statement are used to execute looping statement under fixed number of times or it will targeting on a fixed condition which are control this loop execution.

while conditional do
  #code here
end

Expressional condition are check top section of this loop. And expression are result an produce a boolean value. When that is true then executed that looping instructions.

let see an example which are print a number from 1 to 5.

data = 1
while data<=5 do
    print "  #{data}"
    data+=1
end
 1  2  3  4  5

In this example there are prefixed number are start with 1 and this loop are execute 5 times. Because element variable are increment one by one. Suppose in case, we are need to find an elements which is exist in array. In this case loop execution are based on finding that element. So here not possible to verified that initially number of times loop are iterates execution. Let see an example, print all initially array elements when not found nil element value.

data = [1,2,3,5,11,true,nil,400,50] #define a array

element = 0

while data[element]!=nil and element<data.length do
    puts data[element]
    element+=1 #update element position
end
1
2
3
5
11
true

This while loop are similar to C,C++, Java and C# programming Language.

In ruby programming there are one more variant are available for this loop. Which are execute statement under condition. Speciality of this loop that are it will first execute loop statement and after that check loop execution condition. So this variant are provide to guarantee to execute at least one times loop statements. There syntax as follows.

begin
  #statements
end while condition

Let see an example to use this variant.

#case 1
status  = false
begin

  puts "Hello Tester"

end while status!=false

#case 2
data = 1
begin
    puts data
    #update value
    data+=1
    
end while data<=5
Hello Tester
1
2
3
4
5

This variant is similar to (do-while) loop in other programming language.

Until Statement

until statement are create a loop that conditional work based on logical false value. That are opposite to while loop.

until condition do
   #code here
end
data = 5
until data<=0 do
   puts data
   data = data-1
end
5
4
3
2
1

execute at least one

begin
  #statements
end until condition

For Loop

for loop are similar to while-loop but syntactically there is different.

for variable [, variable ...] in expression do
  #code here
end

Expression is based on specified collection like range, array elements, hash element etc. And for loop variable are targeting this collection element value. scope of this variable is local they are can't usable by outside that loop.

for number in 1..5 do
 puts number
end
1
2
3
4
5

Do while Loop

data = 0
loop do 

  data+=1
  puts data
  #control condition  
  if(data==5)
    break
  end
end

Loop Control Statement

Control statements of loop like break, next, redo are change the flow of loop executions. In this section view examples of those statements.

break Statement

This statement are terminate loop execution. Some of special case it can normally used.

level = 1

while true do

  puts "Play Level : #{level}"

  if level == 7
    puts "Trial Level End"
    break
  end

  level += 1
end
Play Level : 1
Play Level : 2
Play Level : 3
Play Level : 4
Play Level : 5
Play Level : 6
Play Level : 7
Trial Level End

next Statement

That is similar to continue statement in other programming language. This statement are used to back execution flow in loop test condition. That means skips the current execution flow and execute upcoming statement.

gems = [nil,nil,3,1,5]

result = 0
for i in gems
  
    if i == nil then 
      next
    end
    result = result + i
  

end

puts "Total Gems : #{result}"
Total Gems : 9

redo Statement

This statement are used to repeat current iteration in loop.

errors=5
for process in 0..3
  
    if errors > 0 then 
      puts "Error Status : #{errors}"
    errors=errors-1
      redo
    end 
  puts "Take Action : #{process}"

end
Error Status : 5
Error Status : 4
Error Status : 3
Error Status : 2
Error Status : 1
Take Action : 0
Take Action : 1
Take Action : 2
Take Action : 3

retry Statement

retry statement are restart loop execution at starting position (initial condition).

error = 4
3.times do |i|

  begin
    raise if error > 0
    puts "Happy #{i}"
  rescue
    puts "Error #{error}"
    error -= 1
    retry
  end
end
Error 4
Error 3
Error 2
Error 1
Happy 0
Happy 1
Happy 2

Some useful hint

Note that when conditional expression are not use any comparison operation. And this expression are not in boolean values in this situation ruby are assume to always true value.

count = 1
while 1 do
  puts "one"
  count +=1
  if count== 3
    break
  end
end

count = 1
while 0 do #0 expression value
  puts "two"
  count +=1
  if count== 3
    break
  end
end

count = 1
while "Hello" do #Hello expression value
  puts "three"
  count +=1
  if count== 3
    break
  end
end
one
one
two
two
three
three


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved