Exercise 30: Else and If

In the last exercise you worked out some if-statements and then tried to guess what they are and how they work. Before you learn more I'll explain what everything is by answering the questions you had from Study Drills. You did the Study Drills right?

  1. What do you think the if does to the code under it? An if-statement creates what is called a "branch" in the code. It's kind of like those choose your own adventure books where you are asked to turn to one page if you make one choice, and another if you go a different direction. The if-statement tells your script, "If this boolean expression is true, then run the code under it, otherwise skip it."
  2. Why does the code under the if need to be indented two spaces? In Ruby you indent code under statements like if, else, and others so that other programmers know it is a "block" of code. Blocks can have other blocks in them and are ended with an end. There are other ways to make a block of code, but for if-statements this is the way.
  3. What happens if you don't end it with end? If you don't then Ruby doesn't know where your if-statement ends and where others might begin, so it will give you a syntax error.
  4. Can you put other boolean expressions from Ex. 27 in the if-statement? Try it. Yes you can, and they can be as complex as you like, although really complex things generally are bad style.
  5. What happens if you change the initial values for people, cats, and dogs? Because you are comparing numbers, if you change the numbers, different if-statements will evaluate to true and the blocks of code under them will run. Go back and put different numbers in and see if you can figure out in your head what blocks of code will run.

Compare my answers to your answers, and make sure you really understand the concept of a "block" of code. This is important for when you do the next exercise where you write all the parts of if-statements that you can use.

Type this one in and make it work too.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
people = 30
cars = 40
trucks = 15


if cars > people
  puts "We should take the cars."
elsif cars < people
  puts "We should not take the cars."
else
  puts "We can't decide."
end

if trucks > cars
  puts "That's too many trucks."
elsif trucks < cars
  puts "Maybe we could take the trucks."
else
  puts "We still can't decide."
end

if people > trucks
  puts "Alright, let's just take the trucks."
else
  puts "Fine, let's stay home then."
end

What You Should See

$ ruby ex30.rb
We should take the cars.
Maybe we could take the trucks.
Alright, let's just take the trucks.

Study Drills

  1. Try to guess what elsif and else are doing.
  2. Change the numbers of cars, people, and trucks, and then trace through each if-statement to see what will be printed.
  3. Try some more complex boolean expressions like cars > people || trucks < cars.
  4. Above each line write an English description of what the line does.

Common Student Questions

What happens if multiple elsif blocks are true?
Ruby starts at the top and runs the first block that is true, so it will run only the first one.

Buy DRM-Free

When you buy directly from the author, Zed A. Shaw, you'll get a professional quality PDF and hours of HD Video, all DRM-free and yours to download.

$29.99

Buy Directly From The Author

Or, you can read Learn Ruby the Hard Way for free right here, video lectures not included.