Loops and Iterators in ruby

The loops execute the same code again and again. Assume you want to print your name 100 times then you need not to write print statement 100 times. Here you can use loop for executing same code 100 times.

while statement:

In while loop, we define a condition. Until condition is fulfilling, the code inside the loop will execute again and again.

  1. # initialize a variable
  2. a = 0
  3. # while statement with condition
  4. while a < 3
  5. # code inside while loop
  6. puts 'harish'
  7. # modifiing condition
  8. a+=1
  9. # finish loop with end
  10. end

Output:

  1. harish
  2. harish
  3. harish

In this example, we are defining a variable 'a' and putting a condition on it. Until 'a' is less then 3, while loop will print 'harish'.

while modifier:

There are two types of modified statements. First is one line while statement. The syntax is => expression while condition.

  1. # initialize a variable
  2. a = 0
  3. # modified while statement with condition
  4. a = gets.to_i while a < 3

This program will take input from user until user do not enter a number larger than or equal to 3.

Syntax of second type statement is:

  1. begin
  2. # code
  3. end while condition

If you are coming from C, C++ or java programming language then this type of statement is 'do while statement' in these languages. The code inside the begin block will execute once before checking the condition. In other words, this statement execute code first and checks condition after it for next iteration.

  1. # initialize a variable
  2. a = 0
  3. # modified while statement with condition
  4. begin
  5. puts a
  6. a+=1
  7. end while a <3

until statement:

Until statement similar to while loop but only difference is that it executes when condition is false.

  1. # initialize a variable
  2. a = 0
  3. # until statement with condition
  4. until a > 3
  5. # code inside until loop
  6. puts 'harish'
  7. # modifiing condition
  8. a+=1
  9. # finish loop with end
  10. end

Above until statement will execute until 'a' is not grater then 3.

Output:

  1. harish
  2. harish
  3. harish
  4. harish

until modifier:

It is same as while modifier but code executes when condition is false.

  1. # initialize a variable
  2. a = 0
  3. # modified until statement with condition
  4. a = gets.to_i until a > 3

Until 'a' is not greater then 3, this code will take a value for 'a'.

Example of second type is:

  1. # initialize a variable
  2. a = 0
  3. # modified until statement with condition
  4. begin
  5. puts a
  6. a+=1
  7. end until a > 3

Output:

  1. 0
  2. 1
  3. 2
  4. 3

for statement:

  1. # initialize an array
  2. array = [12,32,3,132,123,34]
  3. # define for loop statement
  4. # local is a local variable, accessible only in for loop
  5. for local in array
  6. # code inside for loop
  7. puts local
  8. # end for loop
  9. end

Output:

  1. 12
  2. 32
  3. 3
  4. 132
  5. 123
  6. 34

This statement takes an array and define a local variable. This variable is assigned next value of the array every iteration starting from first.

break, next and redo statements:

These statements are used to disturb the regular execution of the loops and iterators.

  1. break : It is used to stop the execution of the loop.
  2. next : It is used to skip current executing iterator only. It is same as 'continue' in C, C++ and Java.
  3. redo: When this statement executes the loop again starts execution from starting.
  1. # initialize an array
  2. array = [12,32,3,132,123,34]
  3. a = 0
  4. puts 'use of break statement:'
  5. # use of break statement
  6. while a < 10
  7. a+=1
  8. puts a
  9. if a == 5
  10. # skip loop if a = 5
  11. break
  12. end
  13. end
  14. a = 0
  15. puts 'use of next statement:'
  16. while a < 10
  17. a+=1
  18. # don't print 5
  19. if a == 5
  20. next
  21. end
  22. puts a
  23. end
  24. a = 0
  25. puts 'use of redo'
  26. # first time value of a is 0
  27. while a < 5
  28. a+=1
  29. # if a = 3 then repeat the loop
  30. # value of a will not change i.e. second time value will be 3 not 0
  31. if a == 3
  32. redo
  33. end
  34. puts a
  35. end

Output:

  1. use of break statement:
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. use of next statement:
  8. 1
  9. 2
  10. 3
  11. 4
  12. 6
  13. 7
  14. 8
  15. 9
  16. 10
  17. use of redo
  18. 1
  19. 2
  20. 4
  21. 5

each and collect iterators

Each iterator is same as for loop.

  1. array = [12,23,34,45,56,67,7]
  2. # each iterator also defines a local variable
  3. array.each do |local|
  4. # print each value of array one by one
  5. puts local
  6. end

Collect iterator return iterators with same elements.

  1. array = [12,23,34,45,56,67,7]
  2. b = array.collect
  3. for i in b
  4. puts i
  5. end

Output:

  1. 12
  2. 23
  3. 34
  4. 45
  5. 56
  6. 67
  7. 7

[000webhost_bottom_banner]



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Ruby is a pure object oriented, general purpose,interpreted and server side scripting and programming language. Ruby ...
Introduction to Ruby
In this step by step free tutorials we will learn about printing, comments, puts, print, begin, end and EOF methods f...
Getting Started with Ruby
In this tutorial of free step by step free tutorials series we read about variables, constant, printing in staring an...
Variables in Ruby

Login or Sign up to leave comment.