Sheerpower Logo
B.4  Looping and FOR/NEXT
loop-related features
  • do/loop -- beginning and end of an infinite loop block
  • do/end do -- beginning and end of a one iteration loop block
  • exit do -- get out of a loop block
  • repeat do -- repeat the code within the loop block
  • while xxx -- where xxx evaluates to TRUE or FALSE, loops while xxx is true
  • until xxx -- where xxx evaluates to TRUE or FALSE, loops until xxx is true
Unlike most other computer languages, both while and until statements can occur anywhere within a loop, and can even appear multiple times with different conditions.

The DO/LOOP statement in Sheerpower creates a loop that repeatedly executes a block of code until a specified condition is met.

do line input #kj_ch, eof eof?: rec$ if eof? then exit do ! Other code loop

The DO statement starts the loop, which reads lines from a file. The exit do statement exits the loop if the end of the file (eof?) is reached. Otherwise, the loop continues.

The DO/LOOP statement can include conditions.

counter = 0 do while counter < 10 print counter counter++ loop

This loop executes while the condition counter < 10 is true. It prints the value of counter and increments it by 1 until the counter reaches 10.

The WHILE statement is used to create a loop that continues as long as a specified condition is true. It can be used within a DO/LOOP block.

counter = 0 do while counter < 10 print counter counter++ loop

This loop starts with DO and continues as long as counter < 10 is true. It prints the counter value and increments it by 1 each iteration.

The UNTIL statement is used to create a loop that continues until a specified condition becomes true. It can be used within a DO/LOOP block.

counter = 0 do print counter counter++ loop until counter = 10

This loop starts with DO and continues until counter = 10. It prints the counter value and increments it by 1 each iteration.

The EXIT DO statement is used to exit a loop prematurely based on a condition.

do line input #kj_ch, eof eof?: rec$ if eof? then exit do ! Other code loop

In this example, the loop reads lines from a file. If the end of the file (eof?) is reached, the exit do statement exits the loop.

The REPEAT DO statement restarts the loop from the beginning.

do line input #kj_ch, eof eof?: rec$ if eof? then exit do if some_condition then repeat do ! Other code loop

In this example, the loop reads lines from a file. If a condition is met, the repeat do statement restarts the loop from the beginning.

A given DO/LOOP block can contain any number of WHILE or UNTIL statements. You can also create a one-time loop using DO/END DO, which typically contains EXIT DO or REPEAT DO statements.

do if some_condition then exit do ! Other code end do

This example demonstrates a one-time loop using DO/END DO. The loop will execute once, and if a condition is met, it will exit using the EXIT DO statement.

The FOR/NEXT loop
  • for varname = start_value to end_value / next varname
  • for varname = start_value to end_value step step_value / next varname
  • repeat for -- repeat the current iteration -- the varname is not adjusted
  • iterate for -- start the next iteration
  • exit for -- get out of this for/next block
Explanation of the FOR Loop

The FOR loop in Sheerpower is used to iterate over a range of values. The syntax FOR i = start TO end sets up a loop where the variable i starts at start and increments by 1 on each iteration until it reaches end. To specify an increment or decrement value, use the optional STEP option.

Examples of the FOR Loop in Sheerpower
for i = 2 to 30 print i next i

This example sets up a FOR loop where the variable i starts at 2 and increments by 1 on each iteration until it reaches 30. The print i statement prints the value of i on each iteration.

To specify an increment or decrement value, use the optional STEP option.

for i = 10 to 20 step 4 print i next i

This example sets up a FOR loop where the variable i starts at 10 and increments by 4 on each iteration until it reaches 20. The print i statement prints the value of i on each iteration. The loop will print the values 10, 14, and 18.

To iterate the FOR statement from within the FOR/NEXT block, use the ITERATE FOR statement. To repeat the same iteration, use REPEAT FOR.

for i = 1 to 10 if i = 5 then iterate for print i next i

This example sets up a FOR loop from 1 to 10. If i equals 5, the iterate for statement skips the current iteration and continues with the next value of i. The loop will print all values from 1 to 10 except 5.

for i = 1 to 10 print i if i = 5 then repeat for next i

This example sets up a FOR loop from 1 to 10. If i equals 5, the repeat for statement restarts the current iteration, printing 5 repeatedly, causing an infinite loop at i = 5.

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.
Wide screen