text
stringlengths
0
897
A `for` loop assigns a variable — often called `i` in our examples — to each element of a list or string, in turn. It stops looping after it has done this with all elements. It's equivalent to:
{caption:Life in a world without `for` loops}
```miniscript
myList = [1, 2, "hey", "you"]
index = 0
while index < myList.len
i = myList[index]
print i
index = index + 1
end while
```
...but as you can see, the `for` loop is considerably shorter and easier:
{i:"`for` loop"}
{caption:"`for` loops make life easy"}
```miniscript
myList = [1, 2, "hey", "you"]
for i in myList
print i
end for
```
{i:"`range` function"}
In previous chapters, we used `for` loops with the `range` function, which I glossed over without properly explaining. But now it's time to revisit that. `range` is an intrinsic function that takes three parameters: a starting value, an ending value (which defaults to zero), and a step size (which defaults to one). ...
{caption:"Examples of the `range` function", hpad:8}
| `range(1, 10)` | `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]` |
| `range(10, 1)` | `[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]` |
| `range(1,10,3)` | `[1, 4, 7, 10]` |
| `range(1,10,4)` | `[1, 5, 9]` |
| `range(10,1,4)` | `Runtime Exception: range() error` |
| `range(10,1,-4)` | `[10, 6, 2]` |
| `range(10)` | `[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]` |
It's quite possibly the most commonly used function in MiniScript, apart from print, and in some code it surpasses even that. So take some time with the REPL to play with it. Type in each of the examples in the left column on the previous page, and you should see the output shown on the right.
Notice that `range` can count both ways: if the start value is less than the end, it counts up; otherwise it counts down. Also notice that the start value is always included as the first element of the resulting list, but the end value might not be included at all, if it is not hit by the combination of start and step...
Experiment a bit more and make sure that `range` is a tool you are comfortable using. Now that you understand this, you can really understand all those `for` loops we glossed over before. When we write something like
```miniscript
for i in range(10, 1)
print i + "..."
wait
end for
```
...that `for` statement is really just iterating over the list returned by `range`. We could have instead written
```miniscript
for i in [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
print i + "..."
wait
end for
```
and this would do the exact same thing.
D> In programming, there are often many ways to do the same thing. You should always pick whichever way seems clearest and easiest to read and understand. This is usually (but not always) the way that is shortest.
{i:`indexes`}
We'll end the day with one more tip related to lists and `for` loops. It sometimes happens that you need to iterate over not the elements of a list, but the indexes of those elements. For example, suppose you want to write a loop that converts every element of a list of strings to upper case. You might do it like th...
{caption:"Uppercasing a list, the slightly less easy way"}
```miniscript
data = ["Billy", "and", "the", "boingers"]
for i in range(0, data.len - 1)
data[i] = data[i].upper
end for
data
```
(We left out `print` on the last line because I assume you're typing this into a REPL, where simply naming a variable is enough to print its contents.) So this isn't too bad; `i` here will go from 0 to the last index (which is one less than the length of the list), and then we use that to reassign `data[i]` with the u...
But there is a better way to do it, and that is the `.indexes` method. This returns a list of all the valid indexes for a list or string (or map, which you'll learn about in a couple days). If you did the above in your REPL, then type `data.indexes` now to see what it returns in this case. The `.indexes` method is c...
{caption:"Uppercasing a list, the slightly easier way"}
```miniscript
data = ["Billy", "and", "the", "boingers"]
for i in data.indexes
data[i] = data[i].upper
end for
data
```
Same loop, but slightly shorter and clearer code.
{pageBreak}
A> **Chapter Review**
A> - You installed command-line MiniScript for your platform.
A> - You learned how to do use a REPL to work with code interactively.
A> - You discovered that your knowledge of the MiniScript language may apply in very different environments.
A> - You explored the list datatype, and in the process, gained deeper understanding of `for` loops.
{chapterHead: "Day 9: Slice and Scripts", startingPageNum:93}
{width: "50%"}
![](Chapter9.svg)