part 5: loops in fig
this is really the first chapter about “program blocks.”
so far, weve covered one type of block– a snippet of inline python:
python y = (5 + 12) * 3 # 51; different than: y = 5 + 12 * 3 # 41; (order of ops) fig
this block begins with the python command, and the end is marked with the fig command.
the defining characteristic of a block is probably that it starts and ends with a pair of commands.
in fig, you can end any block with the fig command. but semantically it makes the most sense when it ends a python block, because python means: “here is some python code” and fig means: “get back to the fig code.”
all loops are program blocks, also known as “command blocks.”
between the start and end of the loop blocks are the lines of code that will “loop.”
fig has three kinds of loop: for, while and forin.
the simplest loop is the while loop. lets make a single line of code to print a zero:
now lets start a while block by putting it on the line before the one we just wrote:
while x print
we need to mark the end of a loop, or fig will loop every line that comes after the while command:
while x print fig
there is nothing wrong with this block; its perfectly formed. however, while blocks have their own end marker, which you can optionally use in place of the standard fig command. it does exactly the same thing, but helps note which block it ends:
while x print wend
so you can end a while block using the fig command, or you can end it using wend (it stands for while-end.) the choice is up to you, they are interchangeable.
the while command exists in basic, python and even c. in basic and python, you can use while true: or while 1: to keep looping until the loop breaks with break or (in basic) exit while.
since for more than half a decade, ive used while 1 in python instead of worrying about setting up the loop with a condition, fig is designed this way for simplicity.
while x print wend
prints 0 (or an array stored in x, where applicable) repeatedly until the user breaks with ctrl-c on the keyboard.
besides ctrl-c, the other way to break out of a while loop is using the break command (same as in python,) but without a conditional (covered in the next chapter) a break command will either prevent the loop itself, or prevent the lines inside it from running:
while x "this only runs once-- no loop" print break wend
the break command stops the loop right after it runs the code inside it once, so it is as if the while command isnt there at all; only the line that prints.
while break x "this line doesnt run at all" print wend
moving the break command to before the other line inside the loop exits before the lines after it can even run– so this entire block does nothing other than waste a tiny amount of time.
in order to make a while loop do something useful, you probably want to put the break command inside a conditional block. we will get to those in the next chapter.
a forin loop will loop through the items in an array, running the same code inside the block for each one. these items can be lines of a file, lines in a website, letters of a string, pieces of a string made into an array by split– any array.
lets do words in a string, by splitting the string into an array by spaces:
names "ady susan kerry morgan lori" split names " " forin p names now p print fig
fig will split the string by ” ” to make an array called names, and print each one:
we end the forin loop using fig again, but just as while has wend for (optional) semantic use, forin has nextin (or next). use whichever you prefer.
forin p names now p print nextin
if you want to loop through numbers, you can use a for loop. a forin loop will run the same code repeatedly, once per array item– and a for loop once per number in a range– for has 4 parameters:
- the variable for will set with the value of the current item (just like a forin loop)
- the number to start with
- the number to stop at
- the “step”; the number to increase on each loop
in other words:
for v start stop step
suppose we want to bring variable size from 5 to 12, doing every whole number in that range:
for size 5 12 1 now size prints " " prints next
will output: 5 6 7 8 9 10 11 12
odd numbers from 33 to 17?
for s 33 17 -2 now s prints " " prints next
33 31 29 27 25 23 21 19 17
one quirk of python is that it insists on integer (whole number) steps in numeric ranges. in python (which is what fig translates its code to,) a for (range) loop isnt going to do a decimal step.
for v start stop step # step has to be integer now v print next
however, if you use a constant for the step instead of a variable, fig will let you do a float step:
start 5 stop 10 for v start stop 2.5 # step is constant 2.5 now v prints " " prints next
outputs: 5.0 7.5 10.0
this builds a different kind of loop (actually a custom while loop) in the python translation, instead of a genuine for loop.
in the next chapter, while loops will become more useful when they can start and stop based on conditions. certainly you can use them anyway, in any situation where using ctrl-c on the keyboard to stop looping is suitable.
- license: creative commons cc0 1.0 (public domain)