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
extra credit. You did the extra credit right?
- What do you think the if does to the code under it? An
ifstatement 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-statementtells your script, "If this boolean expression is True, then run the code under it, otherwise skip it."
- Can you put other boolean expressions from Ex. 27 in the
ifstatement? Try it. Yes you can, and they can be as complex as you like, although really complex things generally are bad style.
- What happens if you change the initial variables for people, cats,
and dogs? Because you are comparing numbers, if you change the
if-statementswill 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
you can use.
Type this one in and make it work too.
What You Should See
$ ruby ex30.rb We should take the cars. Maybe we could take the buses. Alright, let's just take the buses. $
- Try to guess what
- Change the numbers of
busesand then trace through each
if-statementto see what will be printed.
- Try some more complex boolean expressions like
cars > people and buses < cars. Above each line write an English description of what the line does.