Advertisement
karlakmkj

Different type of loops

Sep 1st, 2021
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.38 KB | None | 0 0
  1. # print from 1 to 50 (inclusive) using loops
  2.  
  3. # 1. while loop
  4. i = 1
  5. while i < 51
  6.   print i
  7.   i+=1
  8. end
  9.  
  10. # 2. using until keyword
  11. i = 1
  12. until i == 51 do  
  13.   print i
  14.   i+=1
  15. end
  16.  
  17. # 3. for loop with dots
  18. # 2 dots indicate to include the highest no. in the range. 3 dots mean go up to but DON'T include the highest no.
  19. for i in 1..50  
  20.   print i
  21.   i+=1
  22. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement