Ruby - Loops

Greetings!!!
Loop does exactly what you think it would. It loops through a bit of code over and over and over again until you tell it to stop. And the way that we define a loop in Ruby is by simply having loop space and then word do, then the code that we want to repeat will be on a new line after that and then on a new line once that's done, we would have the word end. Everything from do to end would be called a code block and we are going to be talking more about code blocks a little later on and going into depth with them, because they're a really useful thing.

This really the first time we've encountered them, so just make a mental note this is our first code block. Loop will take a code block and just loop through that block over and over and over again. In fact, if we were to go to IRB and just type in something that was a really simple look like this, it would just keep looping and it's called an infinite loop because it doesn't stop. It just loops till infinity. So we really need a way to have a control over our loop, because we don't want it to go forever, we just want it to repeat a few times until maybe a certain condition is met. So the controls we will use on loops are going to be break, next, redo, and retry.

The most useful by far is going to be break and next and I am going to be focusing on those here. Break is where we will use to terminate the whole loop and that's what you'll use a lot, because you will basically use a conditional to say well, if x > 10, then break out of this loop.We are done, we are doing looping. The second of those commands, next, just says immediately jump to the next loop, start the loop over again, but don't do anything that's below me. Whereas break says quit immediately, next says loop immediately. Now redo redoes that same loop over again and retry starts the whole loop over from the beginning.
It really is going to apply more to some more complex loops we will look at later on. So let's focus on break and next and try those out. So let's open up IRB with a simple- prompt and let's start out by saying x = 0. Now we are going to do a loop, loop do, and in our loop we are going to want to say x is going to increment by 2. Now we will want to break out of our loop if x >= 20, but if it's not, then we are just going to output the value of x. So think about for a second what this is going to do and then go ahead and hit Return after end, and you will see that the loop starts. x = 0 at the very beginning.

As soon as the loop starts, x becomes 2 which is still less than 20, so we don't break and then it outputs 2 and then it just loops through that over and over again incrementing x until x finally becomes 20 and then it breaks out of the loop. It never outputs the number 20, because it never gets that far. As soon as it gets to the break line, it breaks immediately and then we are out of our loop and our code is done and everything quits. Then let's try the same thing, but with next. Let's set x back = 0 again, because it's going to be equal to 20 at the end of that loop.
So x = 0. Let's do loop and we will do the same thing x += 2, break also just so we don't have an infinite loop and have the same conditions, but now let's put in next if x = 6 and then we will still go ahead and output x at the end, puts x, and then finally end. So take a look and see what you think that'll do and then hit Return. So we got the same list except for the number 6 is missing. 2, 4 and then it jumped to 8.

That's because when it went through the loop and x = 6 after it incremented, it got to the line and said next if x = 6 and it immediately started the loop over and it never got to that puts x statement. So, therefore, we ended up just skipping it. So that gives a good illustration I think of what next does. So let's take a look at some more complex loops. Specifically, I want to introduce you to while and until and these are basically loops that just have that Boolean break condition built into them. So the first one is a while and then are Boolean condition, do our loop and then end.
Now there is no do, notice that. That's a little bit different. There is an implied code block there, but there is not the do end that we have when we have a loop. So while something is true, keep doing this loop. Now the reverse of that is until something is true. It works the same way as if and unless, you'll remember that unless is the same thing as if not. Well, until is while something is not true. But other than that it works exactly the same way, and which one you decide to use really just depends on what you are trying to accomplish and which one just feels more natural and fits with the English language syntax of what you are trying to do.

So to give you the concrete example of how that while loop works, let's take another look at that loop that we just worked on an irb by using the while syntax. x = 0, while x > 20, x is going to increase by 2 and we are going to output x and then we will end. So what we have done is we have taken that break if statement and made it part of the loop structure itself. That's what our Boolean test up at the top. So let's try typing that in. Let's say x = 0 now while x > 20.

Now notice I turned it around. Before we were working with x >= 20, now I am doing while it's < 20. Same thing just flipped around. So while x > 20, x += and then we will puts x and end. So all I have done is remove the break. I am not going to worry about the next for now. We are just looking at the simple example using break. So let's try it out look at the list that it gives you. Notice up here, the last number was 18 and that was true up here as well. Last number was 18.
Now my last number is 20. So that's an important thing to notice about while loops. It matters with the value x is at the start of the loop.Before when we are doing our break, it mattered what was happening inside of the loop. We had the break after x incremented. It went ahead and looped anyway. Here it doesn't start the process, if this Boolean is true. So it's like evaluating at the very top. In fact, the equivalent loop statement would look like this. Now notice that what's different about this is that I've moved that break statement from below the x += to above it.

That's what's different about it. So at the very top of the loop is the very first thing that gets evaluated. So just be careful about that when you're using while. Now while and until also work the way if does and we can use them inline. So for example, we could have x = 0 puts x +=, while x < 100. So we can just do it all in one simple line. If it is just a real simple statement that we want to repeat, this will work for us. Then I have given a second example here where I am using until. I have got y = 3245, then we simply puts y /= 2. Remember the divided by equal works the same ways =+ does.

So y /= 2 until y >= 1. So it will just keep getting smaller and smaller and smaller, putting out the results until finally, the value is <= 1. Now, this is an interesting one for you to review some of the things that we talked about before because the numbers that it outputs might not be the numbers that you would immediately expect unless you remember back to those pitfalls we have talked about. So that's really all there is to working with Ruby's loops. The loops are really primitive and really simple even when we're able to use the while and until forms. They are still very, very simple. The real power in Ruby is with another looping structure which is called iterators.

Comments

Popular posts from this blog

Ruby - Conditionals unless - case