This will be a memo for learning. This time I would like to summarize various repetitive sentences.
In the "for" statement, the value is fetched in order from the object specified in advance and repeated.
for counter variable in repeat range do#do is optional
Iterative processing
end
Example) Pull-down list from 1 to 10 years old in HTML
puts "<select name='age'>"
for age in 1..10
puts "<option>#{age}age</option"
end
puts "</select>"
Repeat the process as long as the conditional expression is true.
#Loop processing by while
#Initialize counter variable
while conditional expression do#do is optional
#Iterative processing
#Update counter variable
end
Example)
i=1. //Initialization of counter variables
while i <=10 //Repeat 1 to 10
puts i #Iterative processing
i=i+1 #Update counter variable
end
if conditional expression
Processing when the conditional expression is satisfied
else
Processing when the conditional expression is not satisfied
end
number = 1
if number == 1
puts "The number is 1"
else
puts "The number is not 1"
end
[Execution result]
The number is 1
if conditional expression
Processing when the conditional expression is satisfied
elsif conditional expression 2
Processing when conditional expression 2 is satisfied
else
Processing when none of the conditional expressions are satisfied
end
With each minute, the elements of the array can be taken out in order and processed.
Array.each do |Variable name|
#The process you want to execute
end
names = ["Masato","Okachan","Saitou"]
names.each do |name|
puts "Name is#{name}is"
end
[Execution result]
The name is Masato
The name is Okachan
The name is Saitou
There may be some mistakes, but I would appreciate it if you could point out that.
Recommended Posts