Count with FOR and FOR EACH
THE BASIC LANGUAGE
Use a numeric FOR when you know the first and last numbers. Use FOR EACH when the job is to visit the entries in a collection.
FUNCTION Run()
DIM total = 0
FOR n = 5 TO 1 STEP -2
total += n
NEXT
RETURN total
END FUNCTIONThe loop visits 5, 3 and 1. Both endpoints are included when the step lands on them. total ends at 9. STEP defaults to 1 and must be nonzero.
FUNCTION Run()
DIM names = ["Ada", "Ben", "Cy"]
DIM chosen = ""
FOR EACH person, i IN names
IF i = 1 THEN
CONTINUE
END IF
chosen = chosen & person
NEXT
RETURN chosen
END FUNCTIONThe first item has index 0. On index 1, CONTINUE skips Ben and advances to the next item. The result is AdaCy. For a dictionary, FOR EACH visits keys. Its optional index counts visits.
