|
Looping and FOR/NEXT |
Loops are one of the most powerful concepts in programming. They allow you to run the same block of code over and over again without rewriting it. This is essential for everything from counting to processing lists of data. Let's explore how loops work in Sheerpower.
DO/LOOP
The simplest loop in Sheerpower is the DO/LOOP. It
repeats the code between the DO and LOOP keywords forever.
If you run an infinite loop, you can stop it by pressing ALT+B
or closing the console window.
An infinite loop isn't very useful on its own. We need a way to control it.
UNTIL and EXIT DO
Let's create a simple guessing game. The program needs to keep
asking for the magic word until the user guesses
it correctly. The UNTIL statement is ideal for this.
DO/LOOP with a Condition
Unlike many other languages, Sheerpower allows you to place
multiple WHILE (run while true) and UNTIL (run until
true) conditions anywhere inside a DO/LOOP. This lets
you create complex, multi-exit loops without messy IF statements.
DO/END DOSometimes you need to try something, check if it worked, and either continue or try again with helpful feedback. Traditional loops handle this awkwardly because the validation logic gets separated from the user interaction.
DO/END DO Solution:This construct runs once by default, but gives you clean control over whether to repeat with feedback:
This pattern is ideal for any "try until successful" scenario: file operations, user input, network connections, or data validation.
FOR/NEXT
When you know exactly how many times you need to repeat an
action, the FOR/NEXT loop is the perfect tool.
It's designed specifically for counting.
i = 1
Sets the starting value of the loop counter. This may be any numeric expression.
The starting value is required.
to 10
Defines the target value the counter moves toward.
The loop runs while the counter has not yet passed this value in the direction of the step.
The target value can be any numeric expression.
If omitted, the loop has no automatic end condition.
step 1
Specifies how the counter changes after each iteration. The step may be positive or negative and may be any numeric expression.
If omitted, the step defaults to 1.
Example: A Rocket Launch Countdown
ITERATE and REPEAT
Sometimes you need to handle special cases *inside* a loop.
ITERATE FOR skips the rest of the
current loop run and moves immediately to the next item.
Example: Processing a To-Do List
REPEAT FOR
REPEAT FOR runs the current pass of a
FOR loop again from the top. The counter is not stepped and
its limit is not tested, so the same item is handled again — a
reading that was not ready, or an answer that has to be asked for again.
Example: Reading a Sensor Again
Output:
Something in the pass must change before REPEAT FOR (here,
the reading), or the same pass repeats forever. In a
FOR EACH loop, REPEAT FOR visits the same row
again.
ITERATE DO
ITERATE DO repeats a DO loop by
jumping to the bottom, its LOOP line: the rest of the current
pass is skipped, and a LOOP WHILE or LOOP UNTIL
test on that line still runs. REPEAT DO repeats from the top
instead, and skips that test.
Example: Adding Up Sales, Skipping Refunds
Output:
The last amount is a refund. ITERATE DO still runs the
LOOP UNTIL test, so the loop ends there. With
REPEAT DO in its place the test would be skipped, and the
loop would go on to entry 6, past the end of the array
(Subscript out of bounds).
A DO WHILE or DO UNTIL test at the top is checked
again after either statement. In a DO/END DO block the
bottom is END DO, which does not loop back, so
ITERATE DO leaves the block; use REPEAT DO to run
the block again.
Each statement acts on the nearest loop of its own kind, even from inside
a loop of the other kind: an ITERATE DO inside a
FOR loop that is inside a DO loop goes to the
DO loop's LOOP line. A WHILE or UNTIL in the
middle of a body tests for the nearest DO the same way: when it
ends that loop it leaves any FOR loop it is in as well. It
takes no name. To reach an outer loop instead, name it (section 6).
| Statement | Goes to | Then |
|---|---|---|
iterate for | the bottom: the NEXT line | steps the counter and tests its limit |
repeat for | the top, for the same pass | no step, no limit test |
exit for | the statement after NEXT | the counter keeps its value |
iterate do | the bottom: the LOOP line — or END DO, which ends a DO/END DO block | runs its WHILE / UNTIL test, then any test at the top |
repeat do | the top: the DO line | runs a DO WHILE / UNTIL test; skips the LOOP line's |
exit do | the statement after LOOP or END DO | no test runs |
Every statement above acts on the innermost loop of its kind. To leave,
skip or repeat an outer loop from inside an inner one, put the
outer loop's name after the statement. A FOR loop is named
by its counter and a FOR EACH loop by its cluster — the
names their NEXT lines already use — and a DO
loop gets a name with DO NAMED. Named jumps replace the flags
that are otherwise set in an inner loop and tested after it.
Example: Skip a Whole Row
Output:
iterate for r leaves the inner FOR c loop and goes
on with the next row, so rows 1 and 3 are added (50 + 130).
Example: Skip a Cluster Row from an Inner Loop
Output:
DO NAMED
do named and a name start a named DO loop, and its
closer repeats the name — loop order, or
end do confirm for a block — just as NEXT
repeats a FOR loop's counter. A WHILE or
UNTIL goes after the name: do named scan while x < 10,
loop scan until done?. Then exit do order,
iterate do order and repeat do order reach that
loop from anywhere inside it.
Example: Answers for Each Customer (the answers come from
a list here, so the example runs on its own; with line input
it works the same way)
Output:
A bare repeat do in the skip case would have
repeated the inner confirm block — the wrong loop.
| Statement | Goes to |
|---|---|
exit for r | the statement after next r |
iterate for r | the next pass of FOR r (step, then the limit test) |
repeat for r | the same pass of FOR r again |
exit for orders | the statement after next orders |
iterate for orders | the next row of FOR EACH orders |
exit do scan | the statement after DO scan's closer |
iterate do scan | the bottom of DO scan: its LOOP line |
repeat do scan | the top of DO scan |
A named statement does exactly what its bare form does, aimed at the named loop; every loop in between is left. The rules, all checked by the compiler:
exit, iterate or
repeat must match the loop: FOR for a
FOR or FOR EACH loop, DO for a
DO loop.WHILE, UNTIL and ELSE cannot be
names.DO named total
and a variable total do not meet. Two DO loops
one after the other may share a name; a DO inside an open
DO of the same name may not.
A jump — named or not — can also leave other blocks on its way
out. It ends each WHEN EXCEPTION block it leaves, as
END WHEN would, and it may leave a USE part:
exit do there is allowed as well. A jump that leaves an
ADD TABLE block, or a COLLECT or
EXTRACT block, cancels it, as CANCEL ADD and
CANCEL EXTRACT would: the half-built record is not written, and
the collection is empty. To keep what was built so far, use
EXIT ADD or EXIT EXTRACT first. A jump out of an
ADD CLUSTER block keeps the row: a cluster row is added at
ADD, with whatever fields were set before the jump. After a
jump out of an EXTRACT or COLLECT block, the record
or row it was on stays current, so a routine that finds a record and
leaves with EXIT ROUTINE hands that record to its caller.
DO/LOOP when you don't know how
many repetitions you need.DO/END DO for a single-pass
block that you can exit or repeat, great for validation.FOR/NEXT when you need to
loop a specific number of times.EXIT, ITERATE
and REPEAT to leave a loop, go on to its next
pass or run a pass again; add a name to reach an outer loop.|
Hide Description
|
|
|
Enter or modify the code below, and then click on RUN |
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |