DEV Community

Oscar Ore
Oscar Ore

Posted on

1

Looping: It's all about Repetition!

  • Understanding Looping is a key part of learning how to program. Looping occurs when you tell the program to do a task or something a certain number of times. The decisions are completely up to you. Pretty neat right? There are many types of Looping methods out there but the key takeaway here is that looping allows us to abstract away the actual mechanics of enacting the same lines of code a certain number of times. Here is an example of a loop using the .times method. Check it out:

8.times do
puts “Hey guys! My name is Oscar and I am going to repeat myself eight times.”
end

If you copy and paste the code snippet into IRB in your terminal and hit enter, you should see the following:

Hey guys! My name is Oscar and I am going to repeat myself eight times
Hey guys! My name is Oscar and I am going to repeat myself eight times
Hey guys! My name is Oscar and I am going to repeat myself eight times
Hey guys! My name is Oscar and I am going to repeat myself eight times
Hey guys! My name is Oscar and I am going to repeat myself eight times
Hey guys! My name is Oscar and I am going to repeat myself eight times
Hey guys! My name is Oscar and I am going to repeat myself eight times
Hey guys! My name is Oscar and I am going to repeat myself eight times

Top comments (0)