[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]

Last time introduced iterative processing related to arrays. This time, the array is also good. Let's focus on the loop processing that is also used for range objects, etc.

Simplified chart

Iterative processing

Method/grammar Number of repetitions
for ~ in ~Sentence Element of object
while statement While the specified condition is true
until statement While the specified condition is false
times method Arbitrarily specified
loop method infinite

Techniques for iterative processing

grammar function
next skip
redo Redo
break Suspension

Methods / grammars that change the number of repetitions

for ~ in ~ Repeat for each element (range)


for num in 1..5 do
  p num
end

Return value


1
2
3
4
5
=> 1..5

while Repeat until the condition is false


num = 0

while num <= 12 do
  p num
  num += 3
end

Return value


0
3
6
9
12
=> nil

until Repeat until the condition is true


num = 16

until num <= 12 do
  p num
  num -= 2
end

Return value


16
14
=> nil

times Repeat the specified number of times

Simple loop


num = 0

5.times do
  p num
end

Return value


0
0
0
0
0
=> 5

Loop while changing the value


num = 0

5.times do |num|
  p num
  num += 1
end

Return value


0
1
2
3
4
=> 5

loop Repeat indefinitely unless you stop with break


num = 0

loop do
  p num
  num += 1
  if num >= 3
    break
  end
end

Return value


0
1
2
=> nil

Techniques during loop processing

next Can be skipped


num = 0..5

6.times do |num|
  next if num == 1
  p num
end

Return value


0
2
3
4
5
=> 6

redo Can be redone


num = 0..5

6.times do |num|
  num += 1
  p num
  redo if num == 1
end

Return value


1
2
2
3
4
5
6
=> 6

break Can be interrupted

num = 0..5

6.times do |num|
  break if num == 3
  p num
end

Return value


0
1
2
=> nil

Recommended Posts

[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
I tried to summarize the basic grammar of Ruby briefly
I tried to summarize the methods used
I tried to summarize the Stream API
[Ruby] I tried to summarize the methods that frequently appear in paiza
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
I tried to summarize the state transition of docker
[Each, map ...] I compared the array processing tonight [ruby]
I tried to increase the processing speed with spiritual engineering
I tried to summarize the basics of kotlin and java
I tried to summarize iOS 14 support
I tried to explain the method
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried to summarize the words that I often see in docker-compose.yml
I tried to summarize what was asked at the site-java edition-
Special Lecture on Multi-Scale Simulation: I tried to summarize the 5th
Special Lecture on Multi-Scale Simulation: I tried to summarize the 8th
I tried to summarize the methods of Java String and StringBuilder
Special Lecture on Multi-Scale Simulation: I tried to summarize the 7th
I tried migrating Processing to VS Code
I tried to summarize Java lambda expressions
[Processing × Java] How to use the loop
I tried to implement the Iterator pattern
What is Docker? I tried to summarize
I tried to build Ruby 3.0.0 from source
I tried to summarize the stumbling points when developing an Android application
I tried to summarize the key points of gRPC design and development
[Ruby] I tried to diet the if statement code with the ternary operator
[Introduction to Java] I tried to summarize the knowledge that I think is essential
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to make full use of the CPU core in Ruby
I tried to summarize about JVM / garbage collection
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
java I tried to break a simple block
[Must see !!!] I tried to summarize object orientation!
[Ruby basics] I tried to learn modules (Chapter 1)
I tried to set tomcat to run the Servlet.
I tried to break a block with java (1)
I want to get the value in Ruby
Ruby: I tried to find out where Nokogiri goes to see the encoding himself
I tried to get the distance from the address string to the nearest station with ruby
I tried to summarize again the devise that was difficult at first sight
[JavaScript] The strongest case when I tried to summarize the parts I do not understand
I tried to summarize the points to consider when acquiring location information with the iOS application ③
I tried to organize the cases used in programming
I tried to decorate the simple calendar a little
[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with Ruby!
I tried to reduce the capacity of Spring Boot
I tried to summarize various link_to used this time
I tried to summarize the points to consider when acquiring location information with the iOS application ②
Sazae-san's rock-paper-scissors I tried to verify the theoretical value and the measured value of the probability of the same hand 5 consecutive times with Ruby
I tried to implement the Euclidean algorithm in Java
I tried to solve the Ruby karaoke machine problem (there is an example of the answer)
I tried to solve the Ruby bonus drink problem (there is an example of the answer)
[Rails 6.0, Docker] I tried to summarize the Docker environment construction and commands necessary to create a portfolio
[For Swift beginners] I tried to summarize the messy layout cycle of ViewController and View
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
[JDBC] I tried to access the SQLite3 database from Java.
[Swift] I tried to implement the function of the vending machine