Sheerpower Logo

Looping and FOR/NEXT


Repeating Actions with Loops

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.

1. The Basic Loop: `DO/LOOP`

The simplest loop in Sheerpower is the DO/LOOP. It repeats the code between the `DO` and `LOOP` keywords forever.

! This is an infinite loop. It will never stop! ! You would have to manually stop the program. do print "Help, I'm stuck in a loop!" loop

An infinite loop isn't very useful on its own. We need a way to control it.

2. Controlling the Loop: `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 perfect for this.

Flowchart: A `DO/LOOP` with a Condition

Start
Code to Repeat
Condition Met?
↝ (Loop Back)
magic_word$ = "Sheerpower" guess$ = "" do line input "Guess the magic word: ": guess$ loop until lcase$(guess$) = lcase$(magic_word$) print "You guessed it!"

Sheerpower's Unique Feature: Flexible Conditions

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.

3. Validation Block: DO/END DO

Sometimes 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.

The Problem with Traditional Approaches:

! Messy approach - duplicates the validation condition do line input "Enter a number between 1 and 10: ": num if num < 1 or num > 10 then print "Invalid input. Please try again." end if loop until num >= 1 and num <= 10 print "Looks good"

The DO/END DO Solution:

This construct runs once by default, but gives you clean control over whether to repeat with feedback:

do input "Enter a number between 1 and 10: ": num if num < 1 or num > 10 then print "Invalid input. Please try again." repeat do ! Try again end if print "Thank you" end do

Why This Can Be Easier to Maintain:

  • No duplicated conditions - validation logic appears only once
  • Clear success path - the "Thank you!" message is right where it belongs
  • Embedded feedback - error messages happen immediately after detecting the problem
  • Single responsibility - the block handles one complete interaction cycle

This pattern is perfect for any "try until successful" scenario: file operations, user input, network connections, or data validation.

4. The Counting Loop: `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.

1. The Initializer

i = 10

Where does the count start?

2. The Condition

to 1

Where does the count end?

3. The Step

step -1

How do we count? (e.g., up by 1, down by 1)

Example: A Rocket Launch Countdown

print "Starting countdown..." for i = 10 to 1 step -1 print i; "..." delay 1 ! Pause for 1 second next i print "Blast off!"

5. Advanced Loop Control: `ITERATE FOR`

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

dim chores$(3) chores$(1) = "Take out trash" chores$(2) = "DONE" chores$(3) = "Feed the cat" for i = 1 to size(chores$) ! Skip chores that are already marked as "DONE" if chores$(i) = "DONE" then iterate for print "Completing chore: "; chores$(i) next i
Summary: You have learned the essential looping tools in Sheerpower.
  • Use DO/LOOP when you don't know how many repetitions you need.
  • Use DO/END DO for a single-pass block that you can exit or repeat, great for validation.
  • Use FOR/NEXT when you need to loop a specific number of times.
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.