Ruby Tutorial 8 - While Loops & Full If-Statements
Hey guys, this is Maads, and with another Ruby programming tutorial. In this video, I'm going to be introducing something new called a loop, and I'm also going to be showing you how to simplify our if statements that we made in the last tutorial.
But before I do that, let me just review what we made in the last video. So let's go ahead and run it. It's basically a program where if we guess a number less than five, it says "too low." If we guess a number greater than five, it says "too high," and if we guess a number five, it says "that's right." This program is great, except that we have to rerun it every time we want to guess a new number. In terms of making a program that someone might actually use, this is pretty impractical. You want the program to stay open until it serves its purpose.
That's why, in Ruby and in most programming languages, there's actually something called a loop, which lets you run a block of code again and again until an expression turns out to be false, so until a comparison is false. In this case, the comparison we could use is B is not equal to A. That way, we could keep on having the user guess more and more numbers until they guess the right number, and then the program will terminate.
So let me just go ahead and show you what a loop would look like, and then I'll get into getting our actual expression to work. Typically, we'd want to put a loop around all of the code we want to run again and again. So I'll put it right here at the second line, and let's just go ahead and say "while true." Now, at the end of the file, I'll put an "end," and now I'll indent everything in the while loop, just like we do with if statements. We also do this with while loops.
As you can see, everything between the while and the end is in the while loop, and these are doubly indented now because they're in a while loop and an if statement. That's fine. Basically, what Ruby will do when it sees this is it'll go here, see this while, and say, "Oh, is the comparison true?" In this case, it is because I just typed the word true here, so this will always be true. Because it's true, it'll run all of this code right here.
When it gets to the end, it'll jump back up to the while loop right up here and say, "Is this condition still true?" If it is, it'll run the stuff again, then get down to the end and jump back up. Let's say this condition was false; if I put false here, it would see the condition was false and jump immediately to the end of the while loop and to the next thing after the while loop. But for the sake of interest, let's just make it always true.
As you can imagine, what this will do is it'll keep on having the user guess a number again and again, even if they guess the number correctly. So let me just go ahead and run this. I guess 4; it says "too low." I guess 6; it says "too high." I guess 5; it says "that's right," and I can keep on guessing. You know, I just guessed five like 15 times, but this is actually the simplest kind of while loop, in my opinion, just a "while true" loop. It'll keep on running forever. By the way, to quit a Ruby program—even if it doesn't quit on its own—just hit Ctrl+C, and it'll stop.
But anyway, let's go ahead and actually get a comparison here. So, you might already have guessed—All we really have to do here is "while B not equal to A," you know "while B != A." This will come out to be true as long as B isn't A, and it'll come out to be false when they actually guess the number correctly. But there's a slight problem with this, and it's something that I haven't introduced yet but I'm going to introduce it right now, and that is variable scope.
There’s more than one reason why I have you indent code. You know, it's helpful to realize, "Oh, all this code is inside of an if statement," and, you know, a while loop. All this code is inside of an if statement. But it's also to keep track of variable scope, you know? What I mean by that is, let's say, well, right out here, I set A, you know, and I don't have anything indented right here, so A is pretty much a universal variable. It'll be valid everywhere.
But B doesn't get assigned until it's inside of the while loop. Because B is indented once after it gets out of that indentation level, B becomes nothing again. B will be a fresh variable every time through this while loop, and that is because it's declared inside of the while loop. Likewise, if I declared C as 10 here and I put C in here, that would not work. You know, you have to declare a variable in a scope and then use it in that scope or anywhere deeper.
So I could declare A here, and I could use it in here, and I could use it in here. But I can't declare something in here, you know, in here, and use it out here. Because of this, after the end, B will become nothing again. So this while loop can't look at B if B is declared inside of the while loop. To fix this problem, all we have to do is assign B its first value outside of the while loop. Now B is actually at the same scope as A. It's like pretty much a universal scope, and when we assign it in here, we're just assigning a universal variable B that will exist outside of the while loop scope.
Now, the idea of scope might seem kind of confusing right now, but it actually gets a lot more natural as you keep on programming. You'll start to appreciate it; it really does help organization to not have a variable dangling throughout your entire program when you have thousands of lines of code. So scope is actually very helpful.
But let's go ahead and run this and see what it looks like. Let's go ahead, and I guess 4; it says "too low." I guess 6; it says "too high." I guess 5, and it says "that's right," and then it quits. That's because I entered five right here. You know, it doesn't run this; it runs this. It prints "that's right." It gets to the end, jumps back up to the while, and then because B is A, this turns out to be false, and then it jumps to the end.
You know, if I put something here like "program done," it would print out after they guessed it correctly, but that's unimportant. So that is how to do a while loop, and this is actually very helpful. You'll find that you'll be using loops a lot in the future when you're programming, and we'll be using them throughout the rest of these videos. So it's important to understand what a while loop is and how it works.
Now for the next thing I'm going to do, which is just to show you how to simplify our if statements from the last video a little bit. If you look at our if statements, they're all individual if statements, but they happen to be exclusive. You know, B cannot be less than A and greater than A. These two things will never happen at once, and it can't be equal to A and less than or greater than A as well. So, you know, this will never run along with any of these things. Really, these are exclusive. Only one of them is going to run at once, and because of that, we only need one if statement.
An if statement doesn't just have to be if and then an end; an if statement can have a couple of different kinds of tokens in the if statement that control the flow. So let me just go ahead here and replace this end and the if of the next if statement with elsif, and I'll do that here as well. This is all one if statement because here's the if and here's the end, and everything in between is actually part of the same if statement, but it's a slightly different syntax than what we're used to.
So let me just walk you through what will happen when Ruby encounters an if statement like this. It'll see, "Ooh, if let's check this expression." You know, right here, let's say this comes out to be true; it'll run this line of code and run everything. Then when it sees an elsif, it's like, "Ooh, I better get out of here," and jumps down to this end. But let's say this comes out to be false; it would jump immediately from this to the elsif and check this expression. Let's say this came out to be true; it would run this, it would run everything up to the elsif, it would say, "I better get out of here," and it would jump to the end.
And let's say this came out to be false; it would jump to this last elsif, it would say, "Is B equal to A?" Let's say it is; it would run everything up to the end. If B isn't equal to A—which would never happen because it can't be less than A; it can't not be less than A and not be greater than A and not be equal to A—that's just not possible. So we're pretty certain this would be true if these two aren't true.
If it gets out here, it's going to look at this, but let's say it was false just for the sake of argument. Instead of running this code, it would just jump immediately to the end. So that might have been slightly confusing to you, but basically, just think of this in English: if this is true, do this; else if this is true, do this; and else if this is true, do this. So it's just basic English. You know, it'll run this if the first thing is true; otherwise, it'll run this, and otherwise, it'll run this. So it'll only run one of these at once, and it'll run whichever one is true first.
So if this one is true first, it'll run it. It doesn't care about these. You know, if this one is true and this one isn't, it'll run it. If none of these are true, it'll run nothing, but it'll only run one of these things that's indented inside of the statement. So that's just how elsif works, and there's one other kind of token that's important to understand in an if statement.
So there's if, there's elsif, there's end, but there's also just plain old else. So if I put an else here, what this will basically do is, if this doesn't run and this doesn't run, it'll just automatically run this. There's no expression here. You know, this is equivalent to else if true, then you know, it'll always run this if the other two are not true.
Once again, it's basic English: if this, do this; otherwise, do this; otherwise, do this. So anyway, let's just go ahead and save this and run it. Let's go ahead and guess a number, guess another number, guess a third number, and that's right, and it ends.
So what I've shown you in this video is how to do a loop based on a comparison, and I've also shown you how to turn multiple if statements into one if statement as long as those multiple if statements are exclusive—that is, only one of them is going to run at once. But anyway, thanks for watching! Feel free to leave any comments in the description. You know, I was sort of confused the first time I saw if statements like this, so if you have a question about that, feel free to ask, and I will get back to you. But anyway, thanks for watching, subscribe, and goodbye.