text
stringlengths
0
897
ok = age >= 18
print "OK, you can go." * ok + "I'm sorry, you're too young." * (not ok)
age = val(input("Next! How old are YOU? (0 to exit): "))
end while
```
{i:"indentation"}
I'm sure you've noticed by now that the lines inside the `while` loop are indented. In MiniScript, indentation is like comments: it's there for the human readers of the code, and ignored by the computer. But that doesn't mean it's not important! Without indenting the body of the loop, it would be much harder to see ...
## Looping with `for`
{i:"`for` loop"}
MiniScript has exactly two ways to repeat code; one is the `while` loop. The other is the `for` loop. It looks like this:
{caption:"For Loop"}
```miniscript
for i in range(1, 10)
print i
end for
```
Like a `while` loop, a `for` loop repeats everything in the loop body (which by convention we indent, though the computer is just looking for the line that says `end for`). But rather than looping while some condition is true, a `for` loop assigns a sequence of values to a variable, and stops when that sequence is com...
{i:"`range` function"}
The example above uses the `range` function, which we haven't covered before. We'll delve into that more deeply in Chapter 8, but for now just think of it as a way to tell the `for` loop what range of numbers you want it to loop over. In the example above, `for i in range(1, 10)` makes the variable `i` go from 1 to 1...
You can easily make a `for` statement count down instead of up. Try this one:
{caption:"Countdown"}
```miniscript
for i in range(10, 1)
print i + "..."
wait
end for
print "Lift-off!"
```
{i:"`wait` function"}
Here I have snuck in another new function, `wait`, which just tells MiniScript to pause for 1 second (unless you specify some other number of seconds as an argument). Together with `range` counting from 10 down to 1, we have a classic rocket count-down.
The `for` loop can also iterate over the characters of a string. For a trivial example:
{caption:Hello World, Letter by Letter}
```miniscript
for i in "Hello world"
print i
end for
```
This prints each character of the string on its own line.
This is a good time to point out that `for` and `while` loops can be *nested*, that is, you can put one loop inside of another, as much as you like. Here's a simple example that prints out all the squares on a Chess board.
{caption:"Chess Coordinates"}
```miniscript
for col in "ABCDEFGH"
for row in "12345678"
print col + row
end for
end for
```
{i:"loop, inner vs. outer;outer loop;inner loop"}
We call the first loop (`col` in this case) the *outer* loop, and the one nested inside it (`row` in his example) the *inner* loop. For every iteration of the outer loop, the inner loop starts over and does its whole sequence again. Here we have eight iterations for columns A through H in the outer loop, and eight it...
iteration
: one time through the body of a loop
Of course you don't have to stop with just two loops; you could nest these as deeply as you like, though the number of iterations adds up quickly. For example, if the outermost loop iterates 10 times, the middle loop iterates 5 times, and the innermost loop iterates 8 times, the result would be (10 \* 5 \* 8) = 400 it...
## The MiniScript Quick Reference
{i:"MiniScript Quick Reference;Quick Reference;documentation, MiniScript Quick Reference"}
In Chapter 2, we referred to coding as an "open-book process." It's like a test where you're allowed to use your textbook. Not only that, but you're allowed to search the web, too! These are great ways to find answers, especially to questions you haven't seen before.
However, they're not always the *fastest* way to find answers, particularly for things you've seen before but just need to double-check your spelling or punctuation. For that, a concise cheat sheet works much better. Fortunately, MiniScript comes with exactly that: the **MiniScript Quick Reference**. This is a one-p...
If you haven't already done so, you should go to <https://miniscript.org> *right now*, scroll down to the "MiniScript Quick Reference" link, and click it. Save the file that appears to someplace handy like your desktop, and if you have a printer, print it out and tape it up next to your computer, or slip it into the c...
Although you're only on Day 5, you will find that you already understand much of what this page contains:
- The left column talks about general syntax, including comments; and also `while` and `for` loops, which you just learned. It also mentions `if` blocks, and `break` and `continue`, both of which we'll cover tomorrow.
- The middle column talks about numbers and strings, which you understand; plus lists and maps, which we'll get to on days 8 and 10.
- The right column includes some more advanced stuff we haven't gotten to yet, plus a list of all the important intrinsic functions, for those moments when you know there's a function to do something, but you can't quite remember what it's called.
D> There is only one important function that you won't find on this sheet, and that's `input`. That's because `input` is not actually part of the official MiniScript standard; if you use MiniScript in the context of a game, for example, it might not have this. But all the contexts we discuss in this book will have `i...
So this MiniScript Quick Reference serves not only as a handy jog for your memory, but also as a map of what you've learned, and what still lies ahead. In another week or so, you will understand pretty much everything on this sheet.
In fact, you might consider making an extra printout of this sheet, and highlighting things on it as we go (starting by highlighting everything we've already covered). That will give you a very visual indicator of your progress, as well as reinforce what you've learned. If you don't have a printer, you can probably d...
{pageBreak}
A> **Chapter Review**
A> - You learned how to use comparison operators to compare two values.
A> - You used `while` and `for` to repeat code multiple times.
A> - You acquired the MiniScript Quick Reference, which will be your favorite one-page document in the coming weeks.
{gap:100}
{width:"25%"}