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.
- # initialize a variable
- a = 0
-
- # while statement with condition
- while a < 3
- # code inside while loop
- puts 'harish'
- # modifiing condition
- a+=1
- # finish loop with end
- end
Output:
- harish
- harish
- 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.
- # initialize a variable
- a = 0
-
- # modified while statement with condition
- 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:
- begin
- # code
- 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.
- # initialize a variable
- a = 0
-
- # modified while statement with condition
- begin
- puts a
- a+=1
- end while a <3
until statement:
Until statement similar to while loop but only difference is that it executes when condition is false.
- # initialize a variable
- a = 0
-
- # until statement with condition
- until a > 3
- # code inside until loop
- puts 'harish'
- # modifiing condition
- a+=1
- # finish loop with end
- end
Above until statement will execute until 'a' is not grater then 3.
Output:
- harish
- harish
- harish
- harish
until modifier:
It is same as while modifier but code executes when condition is false.
- # initialize a variable
- a = 0
-
- # modified until statement with condition
- 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:
- # initialize a variable
- a = 0
-
- # modified until statement with condition
- begin
- puts a
- a+=1
- end until a > 3
Output:
- 0
- 1
- 2
- 3
for statement:
- # initialize an array
- array = [12,32,3,132,123,34]
-
- # define for loop statement
- # local is a local variable, accessible only in for loop
- for local in array
- # code inside for loop
- puts local
- # end for loop
- end
Output:
- 12
- 32
- 3
- 132
- 123
- 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.
- break : It is used to stop the execution of the loop.
- next : It is used to skip current executing iterator only. It is same as 'continue' in C, C++ and Java.
- redo: When this statement executes the loop again starts execution from starting.
- # initialize an array
- array = [12,32,3,132,123,34]
-
- a = 0
- puts 'use of break statement:'
- # use of break statement
- while a < 10
- a+=1
- puts a
- if a == 5
- # skip loop if a = 5
- break
- end
- end
-
- a = 0
- puts 'use of next statement:'
- while a < 10
- a+=1
- # don't print 5
- if a == 5
- next
- end
- puts a
- end
- a = 0
- puts 'use of redo'
- # first time value of a is 0
- while a < 5
- a+=1
-
- # if a = 3 then repeat the loop
- # value of a will not change i.e. second time value will be 3 not 0
- if a == 3
- redo
- end
- puts a
- end
Output:
- use of break statement:
- 1
- 2
- 3
- 4
- 5
- use of next statement:
- 1
- 2
- 3
- 4
- 6
- 7
- 8
- 9
- 10
- use of redo
- 1
- 2
- 4
- 5
each and collect iterators
Each iterator is same as for loop.
- array = [12,23,34,45,56,67,7]
-
- # each iterator also defines a local variable
- array.each do |local|
- # print each value of array one by one
- puts local
- end
Collect iterator return iterators with same elements.
- array = [12,23,34,45,56,67,7]
-
- b = array.collect
- for i in b
- puts i
- end
Output:
- 12
- 23
- 34
- 45
- 56
- 67
- 7