Locked lesson.
About this lesson
How to use if/else/elsif statements in Ruby.
Exercise files
Download this lesson’s related exercise files.
Conditional Statements.docx58.8 KB Conditional Statements - Solution.docx
59.1 KB
Quick reference
Conditional Statements
Conditional Statements allow you to compare things and make decisions based on the outcomes.
When to use
Use these whenever you need to choose between two or more things.
Instructions
A basic if statement looks like this:
if num > 10
puts "Your number is greater than 10"
end
A basic IF/ELSE Statement looks like this:
if num > 10
puts "Your number is greater than 10"
else
puts "Your number is not greater than 10"
end
A basic IF/ELSIF Statement looks like this:
if num > 10
puts "Your number is greater than 10"
elsif num == 9
puts "Your number is 9!"
else
puts "Your number is not greater than 10"
end
Hints & tips
- If/Else statements let you make decisions in your program
- Use your comparison operators in your conditional statements
- 00:04 In this video I want to talk about comparison statements or
- 00:06 as they're actually called conditional statements.
- 00:09 I like to refer to them as comparison statements because we're going to use our
- 00:12 comparison operators.
- 00:14 But they're technically called conditional statements.
- 00:16 And they allow us to run conditions, to do tests and make decisions in our code.
- 00:22 So they look just like this, this is the format.
- 00:24 If some condition, do something, and then end.
- 00:27 So commonly these are called if statements.
- 00:30 We're going to look at if statements, if/else statements, and
- 00:32 if/else/if statements in this video.
- 00:34 To do this, we go if, let's say,
- 00:39 name = John, puts Hello John, end.
- 00:45 So if we come up here and let's go, puts Enter,
- 00:51 no let's do print, print Enter your name.
- 00:56 And then name = gets.
- 00:58 We need to put a .chomp at the end of this gets.
- 01:01 I didn't mention the chomp in the last video when we talked about gets,
- 01:04 but chomp removes the line break that gets put on the end of each gets.
- 01:09 So when I type in John, it actually looks like this, John with a line break.
- 01:15 Remember when puts puts a line break behind everything,
- 01:18 print does not, that's why it's all on one line here.
- 01:21 Same thing with gets.
- 01:22 So we just put a chomp on there.
- 01:23 Now if we save this and run it again, enter your name, John, says, Hello John.
- 01:28 If we run it again, enter your name, Bill, nothing happens.
- 01:31 So look at the flow of this program.
- 01:33 If this condition is true, do this code, otherwise don't do anything.
- 01:39 That's an if statement.
- 01:40 We can do other things as well.
- 01:42 We can do else statements.
- 01:44 So if this is true, do this, else do something else.
- 01:47 And an else statement is very easy.
- 01:49 We just type in else and
- 01:52 then whatever block of code we want we put in right here.
- 01:55 So let's put puts, I don't know you, right?
- 02:02 So if we save this and run it, if you enter your name,
- 02:05 John, it says Hello John, and it ends.
- 02:07 Once this happens, the whole program ends if this is true.
- 02:11 If not, it drops down to the else statement and it runs the else statement.
- 02:16 So let's run this program again, Enter your name, Bill.
- 02:20 I don't know you, right?
- 02:21 So that's if, that's else, we can also do else if,
- 02:25 which usually goes up here, so it's just E-L-S-I-F, elsif.
- 02:29 Now we can run another condition,
- 02:33 we can say if name = Bill puts Hiya Bill.
- 02:39 So looking at the flow of our program now,
- 02:42 it's going to start out if the name is John, it'll do this and then end.
- 02:45 Otherwise, it'll drop down and do this.
- 02:48 Is the name Bill?
- 02:49 If so, it'll do this, and then end.
- 02:52 If none of these are true,
- 02:54 then it'll execute this else here at the bottom that says, I don't know you.
- 02:57 So if we save it and run it, if we go, John, Hello John, and then it ends.
- 03:02 If we go Bill, Hiya Bill, and then it ends.
- 03:06 If we see Tim, I don't know you.
- 03:08 So those are conditional statements.
- 03:10 They're very, very useful.
- 03:12 You are going to use these all the time.
- 03:13 I mean almost probably the most important thing in all programming languages.
- 03:17 These if statements, if/else statements, they're just used constantly.
- 03:21 All programs, always, forever, you're going to use these.
- 03:24 So kind of get used to this format.
- 03:26 It's just if, else, and then it always ends here at the bottom.
- 03:30 Anything inside here, this block of code.
- 03:33 And we can do lots of lines of code here,
- 03:36 we can put whole programs in here, doesn't really matter.
- 03:39 It only happens if this line is true.
- 03:42 If it's not true, and we can do anything.
- 03:44 We don't have to do a string.
- 03:46 We can say if number is greater than 1, do this.
- 03:51 If number is not equal to.
- 03:54 We can use all of our comparison operators in this line,
- 03:57 in this conditional statement right here.
- 03:59 So those are conditional statements, very useful.
- 04:01 In the next video, we're going to look at multiple conditional comparison operators.
Lesson notes are only available for subscribers.