- 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)