text stringlengths 0 897 |
|---|
 |
{chapterHead: "Day 6: If and Break", startingPageNum:53} |
{width: "50%"} |
 |
Q> In the end, there can be only one `else` block. |
Q> Or none. That's cool too. |
Q>— The Highlander |
A> **Chapter Objectives** |
A> - Learn how a computer program can do different things depending on what values it currently has. |
A> - Explore the various forms of the `if` statement. |
A> - Learn how to short-cut or exit a loop. |
In the last chapter, we learned how to compare values, and applied that new power to the `while` loop. But, it turns out, that's a little bit like learning to cook and then only doing so for breakfast. It's a perfectly valid use of your skill, but misses the most common application. In programming, comparing values ... |
{i:"`if`"} |
An `if` statement does just what it sounds like: it tells the computer to do something only *if* some condition is true. Let's look at a simple example: |
{caption:Our first `if` statement} |
```miniscript |
age = val(input("Enter your age: ")) |
if age > 23 then |
print "Wow, you're old!" |
end if |
print "OK, got it." |
``` |
Line 1 of this example should be familiar to you; it displays a prompt, waits for the user to type something and press Return, converts this to a number using `val`, and stores it in a variable called `age`. |
The new bit is on line 2. It says that *if* (and *only* if!) age is greater than 23, *then* the computer should actually do line 3. If not, then the computer should just skip on down to the `end if` line. |
Note how, just as with `while` and `for` loops, we indent the body of the `if` block (everything between `if` and `end if`) for clarity. But again, that indentation is like a comment; it's for the human readers of the code, and ignored by the computer. But that doesn't mean you shouldn't do it! |
Also notice that there are two new keywords on line 2: `if` and `then`. An `if` statement always has both of these keywords, working together. The actual condition lies in between. |
Type in the above code and run it several times, testing it with different ages. (It's totally OK to lie about your age when testing a program.) |
## Doing Something Else |
The above example prints an extra message when an age over 23 is entered. You may have wondered how to print a different extra message for younger users. You could do it with another `if` block, like so: |
{caption:Life without `else`} |
```miniscript |
age = val(input("Enter your age: ")) |
if age > 23 then |
print "Wow, you're old!" |
end if |
if age <= 23 then |
print "Not so old, are you?" |
end if |
print "OK, got it." |
``` |
That works, but it's a pain; we have to think about what the opposite of the original comparison is (changing `> 23` to `<= 23` in this case). Worse, if we ever change the initial `if` statement, we'll have to remember to change the second one accordingly. That's just inviting mistakes. Programming is all about maki... |
Fortunately, there's a much better way to express what we're trying to do here, and that is the `else` statement. The word `else`, on a line all by itself, identifies code that the computer should run when the condition in the `if` statement is *not* true. |
{caption:`else` to the rescue} |
```miniscript |
age = val(input("Enter your age: ")) |
if age > 23 then |
print "Wow, you're old!" |
else |
print "Not so old, are you?" |
end if |
print "OK, got it." |
``` |
This is much better! Now when the computer gets to line 2, it's going to check the age, and then print one message or the other, but never both. |
## Checking Multiple Possibilities |
Sometimes there are multiple possibilities you want to check for, and run code corresponding only to the one that is true. You can do this using `else if` blocks. |
Extending the previous example, suppose we want the computer to comment on several different age ranges. |
{caption:Being snarky to all ages} |
```miniscript |
age = val(input("Enter your age: ")) |
if age > 60 then |
print "Distinguished indeed." |
else if age > 23 then |
print "Older than dirt! (Dirt's 23.)" |
else if age > 17 then |
print "Be sure to vote!" |
else if age > 12 then |
print "Ah, the teenage years." |
else if age < 1 then |
print "I don't think that's right." |
else |
print "Just a young'un!" |
end if |
print "Seriously though, thanks for telling me." |
``` |
In this example there are actually four `else if` blocks, but you can have any number of these (including none at all). Here's how it works: the computer first checks the condition on the `if` statement. If that's true, then it runs the code in that very first block (line 3 in the example above), and then jumps on do... |
To review, a complete `if` statement consists of: |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.