yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Ruby Tutorial 7 - Basic If Statements


3m read
·Nov 3, 2024

Hey guys, this is ma1 with another Ruby programming tutorial. In this video, I'm going to be showing you how to put the comparisons we learned in the last video to good use using something called if statements.

Now, an if statement basically just allows you to decide which parts of your code run based on the result of different comparisons. So, for instance, we could have it run a certain line of code if a is less than b, but it wouldn't run the line of code if a is greater than b. Uh, so it sounds pretty simple, and it is pretty simple.

To demonstrate it, I'm going to be basically just making a little program that will have a preset number that the user doesn't know. The user will then guess for that number, and it'll tell them if their guess is too high, too low, or just right. So obviously, the first thing we're going to need is a preset value, so I'll make a variable a, and I'll assign it five.

Then, we'll need the user to enter a number, so I'll say "guess a number," right? And then I'll say b equals gets.chomp.

Um, and now let's go ahead and actually get into the if statement itself. Now, you can probably figure out the comparison we would use to check, let's say if B is too low. Because all we need to do is say B less than a, and if this were to turn out to be true, that would mean that five is more than whatever the number that the user entered in, and because of that, their guess was too low.

Um, but how do we actually make it print something if this is true? Well, all you have to do for an if statement, the syntax is pretty simple. You just do if space then the comparison, which in this case is B less than a space then, and then on the next line, we'll put an end.

And now between these two lines, I'll put the puts "too low." So basically, what an if statement does is you just give it a comparison, and then you give it a block of code, and it'll only run that block of code if the comparison turns out to be true.

Uh, so in this case, the block of code is everything between the then and the end, which only is this line. You know, I could put another line in here. I could assign a variable, I could print more stuff, I could, you know, I could gets stuff.

Uh, it doesn't really matter. It's just everything up until this end will be part of the if statement, and it'll only run if this is true. Now because of that, I like to indent it. I like to put a tab before everything that's inside of this if statement just so I can visually keep track of what's inside the if statement and what's outside the if statement.

Because, you know, stuff out here would be not indented, so I'd realize, oh there's an end before there, so this would always run no matter what.

Um, versus this, it'll only run if this is true. So anyway, let's go ahead and save this, and I'll call it test.rb and I'll save it to my desktop.

Now let's go ahead and run it. I'll just drag it right into terminal, and I'll guess four, and it'll say "too low." Now if I guess six, it won't say anything because it didn't run this code because B wasn't less than a; B was greater than a.

So let's go ahead and make another if statement. If B is greater than a, then puts "too high." And now let's go ahead and run it again. Now I'll guess six, and it'll say "too high."

And I'll guess four, and it'll say "too low." And now let's do a third if statement. Let's say if a equals B, then puts "that's right." And I'll have another end there.

And now if I run it, if I guess four, it'll say "too low." If I guess six, it'll say "too high." And if I guess five, it'll say "that's right."

So an if statement is actually very basic when you just have an if a then and an end and some code between it. In the next video, I'm going to be showing you a more fancy way of simplifying these three if statements to turn it into one if statement, but that'll be for the next video.

So anyway, thanks for watching. Mids 101, subscribe, and goodbye.

More Articles

View All
TIL: You Might Be Related to Genghis Khan | Today I Learned
[Music] [Applause] [Music] So you probably heard of the name Genghis Khan or Jengus Khan, but you might not realize that something like one in 200 men in the world were genetically related to Genghis Khan. So he was obviously very, um, prolific. Yeah, bu…
Here's how to save $10,000 in 6 months.
[Music] Hey guys, welcome back to the channel. In this video, we’re going to be talking about how to save $10,000 in six months. This is a goal that I have set myself in the past; I’ve actually been able to achieve it quite a few times. So, in this video…
One-sided limits from tables | Limits and continuity | AP Calculus AB | Khan Academy
The function ( f ) is defined over the real numbers. This table gives select values of ( f ). We have our table here; for any of these ( x ) values, it gives the corresponding ( f(x) ). What is a reasonable estimate for the limit of ( f(x) ) as ( x ) appr…
This Teen Boxer Wants A Chance to Compete Wearing Her Hijab | National Geographic
There’s a prayer that we do that says, “If this is good for me, give it to me, and if it’s not good for me, then keep it away from me.” Every time I make that prayer, I’m like, “But what if God takes it from me?” My name is Amaya Zafar. I’m 16 years old …
Second derivatives (implicit equations): evaluate derivative | AP Calculus AB | Khan Academy
So we have a question here from the 2015 AP Calculus AB test, and it says, “Consider the curve given by the equation ( y^3 - xy = 2 ).” It can be shown that the first derivative of ( y ) with respect to ( x ) is equal to that. So they solved that for us. …
Weaving Tradition: How Tweed Keeps a Community’s Heritage Alive | Short Film Showcase
[Music] I’m Rebecca Hutton, and I’m a single Harris Tweed Weaver. My granny used to weave back in the day, and my great granny. Everybody used to weave; there was a weaving tradition at practically every household when I was growing up. The Harris Tweed …