Exercise 30. Else and If

In the last exercise you worked out some if-statement 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 ...

Get Learn Ruby the Hard Way: A Simple and Idiomatic Introduction to the Imaginative World of Computational Thinking with Code, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.