-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloops.rb
75 lines (58 loc) · 1.38 KB
/
loops.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
array = [100, 11, 25, 36, 47, 5]
for item in array
item = item + 2
puts "The current item + 2 is #{item}."
# # code to be executed
end
array2 = ["hi", "you", "ruby", "programming"]
for elem in array2
puts elem
end
for i in 1..5
puts "Value of local variable is #{i}"
puts array[i]
end
index=0
while index<array.length
puts "Value of current element is #{array[index]}"
index+=1
end
n = 0
while n < 4
puts n
n += 1
end
30.times {puts "I will not throw paper airplanes"}
75.times { |i| puts "hello #{i}" }
array = [1,2,3,4,5]
(array).each do |item|
item = item + 4
puts " add +4 to the #{item}"
end
(1..4).each do |element|
puts "The Value of the local variable element is #{element}"
element=element/20
puts element
if element%2==0
puts "the element is even!"
end
end
range= 1..4
range.each do |element|
puts "The Value of the local variable element is #{element}"
end
# Array of integers
[1, 2, 3, 4].each do |element|
puts "The Value of the local variable i is #{element*65}"
end
# Array of strings
string_array=["a", "b","c", "d"]
string_array.each do |item|
puts "The value of i is #{item.upcase}"
end
string_array.each {|item| puts "The current array item is #{item}"}
array = [100, 11, 25, 36, 47, 5]
array.each do |item|
item = item + 2
puts "The current item + 2 is #{item}."
end