text stringlengths 0 897 |
|---|
D> MiniScript uses the value 1 to mean true, and 0 to mean false. |
But `==` is just one comparison operator. There are half a dozen: |
{i:"operators, comparison;comparison operators"} |
{caption:"Comparison operators"} |
| `==` | equals | |
| `!=` | not equal | |
| `>` | greater than | |
| `>=` | greater than or equal to | |
| `<` | less than | |
| `<=` | less than or equal to | |
You can use any of these to compare values, wherever they might come from. Here's an example that decides if you're old enough to see a scary movie. |
{caption:"Movie Decider 1"} |
```miniscript |
print "Now showing: Chainsaw Zombie Nightmare VII" |
age = val(input("How old are you? ")) |
print "My decision: " + (age >= 18) |
``` |
This program prints the name of the movie, asks for your age, and then compares your age to 17. It prints a 1 if your age is 18 or older, otherwise it prints a 0. |
That works, but it's not a very elegant way of displaying the result, is it? Let's make it a little better by making use of string replication from Day 2. A string multiplied by 1 is just that string itself; but a string multiplied by 0 is nothing (an empty string). We can use that to print a custom message: |
{caption:"Movie Decider 2"} |
```miniscript |
print "Now showing: Chainsaw Zombie Nightmare VII" |
age = val(input("How old are you? ")) |
print "Hmm..." |
ok = age >= 18 |
print "OK, you can go." * ok |
``` |
Try that one a few times, entering different ages. Notice how we are storing the result of the age comparison in a variable, `ok`, on line 4, and then multiplying that by the message on line 5. |
If you enter 18 or older, this program works fine. But if you enter a smaller number, it doesn't print anything after "Hmm..." Let's improve that with the help of the `not` operator. |
{i:"`not` operator;operator, `not`"} |
D> The `not` operator changes 1 to 0 (true to false), and vice versa. |
This is the first operator we've seen that operates on a single value, instead of appearing between two values. You put `not` before the value you want to operate on. We can use that to finish our movie decider. |
{caption:"Movie Decider 3"} |
```miniscript |
print "Now showing: Chainsaw Zombie Nightmare VII" |
age = val(input("How old are you? ")) |
print "Hmm..." |
ok = age >= 18 |
print "OK, you can go." * ok + "I'm sorry, you're too young." * (not ok) |
``` |
D> When a line in a code listing is too long, the end of it is printed in this book on the next line, identified with "..." as shown in the last line above. But when you type it in, just keep typing — it will wrap or scroll over as needed. |
This is not the only way to print out alternate messages; we'll learn a more natural way tomorrow. But it works and is a nice way to review both string replication, and the fact that `true` and `false` are represented as `1` and `0` in MiniScript. |
## Looping with `while` |
{i:"`while` loop"} |
So far every program we've written has started with the first line, executed each line in order to the end, and quit. A decent start, to be sure, but computers are so much more powerful when they can do the same operation multiple times. |
Now that you know how to compare values, you're ready to learn the first of two ways to make the computer repeat itself. This is the `while` statement, which defines that a group of lines should repeat while some condition is true. It looks like this: |
{caption:"Doublings"} |
```miniscript |
x = 1 |
while x < 1000 |
print x |
x = x * 2 |
end while |
print "And that's high enough!" |
``` |
Type this code into the Try-It! interface and see what it does. In plain English, this starts by assigning 1 to x. Then the loop begins. Lines 3-4 are repeated as long as (or "while") x is less than 1000. Those lines print x and then double its value. |
When the `while` condition (`x < 1000`) is no longer true, then the loop exits, continuing with the line right after `end while`. |
Of course this can work with any sort of condition. Try this variation: |
{caption:"Doubling Spam"} |
```miniscript |
x = "spam " |
while x.len < 60 |
print x |
x = x * 2 |
end while |
print "...that's enough spam for me!" |
``` |
This is almost the same as the previous program, except instead of numbers, we are doubling strings; and our `while` condition now relies on `x.len` (the length of x). |
Let's revisit our movie-decider program. Suppose it's your job to tell not just one youth, but a whole busload of youths, whether each of them is allowed to see the movie. Because you are becoming a programmer, you quickly think to automate this tedious process. |
{caption:"Multi Movie Decider"} |
```miniscript |
print "Now showing: Chainsaw Zombie Nightmare VII" |
age = val(input("How old are you? ")) |
while age > 0 |
print "Hmm..." |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.