Sunday, 7 September 2014

Loops in Ruby

# #For loops
# while
# Until

str= "testing"
i= 1
while str=="testing"
  if i==5
    break
  end
  puts "Yes Testing"
  i= i+1
end

puts "While condition"
while i<10
  puts i.to_s
  i=i+1
end
puts "begin while condition similar to do..while in other programming languages"
#While condition begin..while
begin
  puts i.to_s
  i= i-1
end while i>=5

puts "until condition"

puts "until condition eg.."
until i>10
  puts "iterate loop as long as condition remain false..= "+ i.to_s
  i= i+1
end


puts "begin .. until loop similar to the do..until in vbscript"

x=15
begin
  puts "i value if i>10:.. "+ x.to_s   #condition is true.. so until won't be executed but this loop will be executed atleast once
  x= x-1
end until x>10

puts "For loop egs:"

for i in (1..5)
  puts i.to_s
end

for i in (2..10).step(2)
  puts "increment i by 2 = #{i}"
end

puts "decrement for loop eg:"

for i in (7).downto(0)
  puts "for loop decrement: #{i}"
end

# error: step can't be negative (ArgumentError)
# for i in (10..1).step(-2)
#   puts "decrement i by 2 = #{i}"
# end

puts "each do loop egs: "
(1..5).each do |x|
  puts "each do loop value= #{x}"
end

# ++++++++++++++++++++++++++++++++++++++++
(2..10).step do |i|
  puts "i in step do #{i}"
end

(1..12).step(3) do |i|
  puts "i in step do #{i}"
end

1.step(15, 2) do |x|
  puts "increment by 2 where x is: #{x}"
end

15.step(1, -3){
  |x| puts ".step decrement by 3 where x is= #{x}"
}

i=6
i.downto(2) do |i|
  puts "downto do loop where i is : #{i}"
end

('a'..'e').each do |al|
  puts al
end

(1..5).step(2).each do |x|        #when the loop increments, we give range first
  puts "each do loop value= #{x}"
end

10.step(1, -2) do |x|
  print x
end

# ++++++++++++++++++++++++++++++++++++++++



Output:

Yes Testing
Yes Testing
Yes Testing
Yes Testing
While condition
5
6
7
8
9
begin while condition similar to do..while in other programming languages
10
9
8
7
6
5
until condition
until condition eg..
iterate loop as long as condition remain false..= 4
iterate loop as long as condition remain false..= 5
iterate loop as long as condition remain false..= 6
iterate loop as long as condition remain false..= 7
iterate loop as long as condition remain false..= 8
iterate loop as long as condition remain false..= 9
iterate loop as long as condition remain false..= 10
begin .. until loop similar to the do..until in vbscript
i value if i>10:.. 15
For loop egs:
1
2
3
4
5
increment i by 2 = 2
increment i by 2 = 4
increment i by 2 = 6
increment i by 2 = 8
increment i by 2 = 10
decrement for loop eg:
for loop decrement: 7
for loop decrement: 6
for loop decrement: 5
for loop decrement: 4
for loop decrement: 3
for loop decrement: 2
for loop decrement: 1
for loop decrement: 0
each do loop egs:
each do loop value= 1
each do loop value= 2
each do loop value= 3
each do loop value= 4
each do loop value= 5
i in step do 2
i in step do 3
i in step do 4
i in step do 5
i in step do 6
i in step do 7
i in step do 8
i in step do 9
i in step do 10
i in step do 1
i in step do 4
i in step do 7
i in step do 10
increment by 2 where x is: 1
increment by 2 where x is: 3
increment by 2 where x is: 5
increment by 2 where x is: 7
increment by 2 where x is: 9
increment by 2 where x is: 11
increment by 2 where x is: 13
increment by 2 where x is: 15
.step decrement by 3 where x is= 15
.step decrement by 3 where x is= 12
.step decrement by 3 where x is= 9
.step decrement by 3 where x is= 6
.step decrement by 3 where x is= 3
downto do loop where i is : 6
downto do loop where i is : 5
downto do loop where i is : 4
downto do loop where i is : 3
downto do loop where i is : 2




No comments:

Post a Comment