Sheerpower Logo

B.4  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. The Single-Pass Loop for Clean Control Flow

SheerPower also offers a powerful DO/END DO structure. This block of code runs only once by default, but gives you the ability to `EXIT DO` early or `REPEAT DO` to run the block again. It is perfect for input validation.

do line input "Enter a number between 1 and 10: ": num ! Check if the input is valid if num >= 1 and num <= 10 then print "Thank you!" exit do ! The input is valid, so we exit the block. else print "Invalid input. Please try again." repeat do ! The input is invalid, so we repeat the block. end if end do

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.