루비Ruby 반복문Loop statement

for문

for i in 1..10
  for j in 1..i
    print "*"
  end
  puts ""
end

a = [1,2,3,4,5]

for i in a
  print i,"  ", a[i], "\n"
end

until문

num = 100
until num < 10 do
  num-=1
  puts num
end

while문

num = 1
while num < 10 do
  num+=1
  puts num
end

next, redo, break, retry

for i in 1..100
  if i % 2 == 0 then
    print i
    next
  end
  if i > 80 then
    break
  end
end

for i in 1..100
  if i % 2 == 0 then
    print i
    i=101
    redo
  end
end

for i in 1..100
  if i % 10 == 0 then
    print i
    retry
  end
end

Leave a Reply

Your email address will not be published. Required fields are marked *

 characters available