text
stringlengths
0
897
- an `if` *condition* `then` block
- zero or more `else if` *condition* `then` blocks
- zero or one `else` block
- an `end if` statement
Notice that while you can have any number of `else if` blocks, you can have no more than one `else` block, and this must always come last.
Finally, be aware that just like `while` and `for` loops, `if` blocks can be nested; you can put an `if` inside another `if`. Or inside a loop. Or put a loop inside an `if`. Combine these however you like... just remember to be consistent about your indentation, so you don't get confused about what's going on.
## Single-Line If
{i:"single-line `if`;`if`, single-line"}
A fairly common need is to do a simple check with just one simple effect. This often comes up in input validation, that is, making maximum sense out of a human's input (humans are notoriously unreliable). Suppose you've got code that asks users for a percentage, but you just know some eager user is going to try to gi...
{caption:Using three lines where one would do}
```miniscript
effort = val(input("Enter effort, in percent: "))
if effort > 100 then
effort = 100
end if
print "OK, calculating with " + effort + "%..."
```
There's nothing wrong with this. It works fine. But it will eventually become a little annoying to have to write (or read) three lines for such a simple correction. For this reason, MiniScript supports a special single-line form of the *if* statement. It looks like this (see line 2):
{caption:Single-line `if`}
```miniscript
effort = val(input("Enter effort, in percent: "))
if effort > 100 then effort = 100
print "OK, calculating with " + effort + "%..."
```
This does exactly the same thing as the previous example; it's just shorter, and easier to read and write.
To make a single-line *if* statement, simply put the (single line) of code you want to happen if the condition is true on the same line right after `then`. That's it. If you have more than one line of code to do when the condition is true, you'll need to use the standard block form instead.
You can, however, also include an `else` clause in the single-line *if*. This is less commonly needed, but might come up for example when finding the right way to pluralize a word:
{caption:Counting geese}
```miniscript
count = val(input("How many geese? "))
if count == 1 then n = "goose" else n = "geese"
print "Thanks for the " + count + " " + n + "!"
```
Type that in and try entering different numbers — in particular, be sure to try both 1 and any value other than 1. You'll see that the print statement at the end always selects the right form of the word "goose." You could certainly do the same thing with a block-form `if`/`else` statement, but it would take five lin...
## Breaking a Loop
Let's return now to the subject of loops, introduced yesterday. Once a loop begins, must it carry through mindlessly to completion? Is it a slave to its `for` sequence or `while` condition, condemned, like Bill Murray in *Groundhog Day*, to repeat itself over and over until the loop is fully satisfied?
{i:"`break`"}
Well, no. There is a way out: the `break` statement. This statement tells MiniScript to break out of the current loop, jumping to the next line past the closest `end for` or `end while` statement.
{caption:"C'mon, 1!"}
```miniscript
while true
roll = 1 + floor(rnd * 6)
print "I rolled: " + roll
if roll == 1 then break
end while
print "Finally rolled a 1!"
```
Look at the funny condition on this `while` statement — `while true`. That would be an infinite loop, because of course `true` can never be `false`.
{i:"infinite loop"}
{i:"loop, infinite"}
infinite loop
: a loop that repeats forever, or until something forcibly halts the program
But this loop does not actually repeat forever, because we've provided an escape clause in line 4: `if roll == 1 then break`. This is using the single-line `if` statement you just learned about to check whether the randomly selected value is a 1. If it is, then we execute the `break` statement, which jumps past the n...
{i:"`continue`"}
There is another, related statement called `continue`. Instead of skipping past the nearest `end while` or `end for` statement, it skips to the next iteration of the loop, as if that `end` line had been reached normally. For example, suppose you're writing code for a user who's afraid of the number 13, and whatever p...
{caption:Triskaidekaphobia}
```miniscript
sum = 0
for i in range(1,20)
if i == 13 then continue
print i + ". " + "*" * i
sum = sum + i
end for
print "That's a total of: " + sum
```
This is an ordinary `for` loop, but on line 13, we check for the special case of `i == 13`, and when that's true, we `continue` — that is, we skip the rest of the loop body and continue with the next iteration of the loop (when `i` is 14 in this case).
This is a pretty silly example, but in real code the need for this sort of thing occurs fairly often. You might have a long `while` loop that does a lot of processing, but starts by asking the user for an input. You could check this input and if it's no good, skip the rest of the loop and continue with the next itera...
{caption:Common while-loop input handling}
```miniscript
while true
inp = input("Enter a value (0 to exit): ")
if inp == "0" then break
if val(inp) < 1 or val(inp) > 100 then
print "Please keep your values between 1 and 100."
continue