Sheerpower Logo
B.4  Looping and FOR/NEXT
Loop-Related Features
Loop Statement Description
do/loop Defines the beginning and end of an infinite loop block, which will continue executing until explicitly exited.
do/end do Marks the start and end of a loop that runs exactly once, often used for clarity or to group a block of code.
exit do Exits the loop prematurely, immediately stopping further iterations and continuing with the code after the loop.
repeat do Repeats the loop block, starting the loop again from the beginning.
while xxx Executes the loop as long as the condition 'xxx' evaluates to TRUE. The condition is checked before each iteration.
until xxx Executes the loop until the condition 'xxx' evaluates to TRUE. The condition is checked after each iteration.

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/NEXT Statement Description
for varname = start_value to end_value / next varname Executes the loop block with varname starting at start_value and incrementing to end_value.
for varname = start_value to end_value step step_value / next varname Executes the loop block with varname starting at start_value, incrementing by step_value each time until end_value is reached.
repeat for Repeats the current iteration of the loop without adjusting varname.
iterate for Skips to the next iteration of the loop, incrementing varname as usual.
exit for Exits the for/next loop block immediately.
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 delay repeat for end if 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