|
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. |
The DO/LOOP statement in Sheerpower creates a loop that repeatedly executes a block of code until a specified condition is met.
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.
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.
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.
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.
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.
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.
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 LoopFOR/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. |
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 SheerpowerThis 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.
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.
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.
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.
The looping constructs in SheerPower provide developers with powerful tools to control the flow of their programs efficiently and effectively. By understanding and leveraging these constructs, you can implement everything from simple iterations to complex conditional loops with ease.
do/loop
supports infinite or condition-based loops.do/end do
is ideal for executing a block of code exactly once with
potential early exits and repeats.exit do
allows breaking out of loops prematurely.repeat do
restarts the loop from the beginning when specific conditions are met.while
and until
statements provide flexibility to evaluate
conditions dynamically within loops.for/next
loops iterate over a range of values with
optional step increments or decrements.iterate for
and repeat for
enable skipping or repeating iterations as needed.exit for
offers an immediate exit from the loop.while
and until
strategically within loops
for maximum clarity.exit
and repeat
statements to handle edge cases efficiently.By mastering these constructs, you’ll unlock SheerPower's ability to process data efficiently and execute logic precisely, whether handling simple tasks or managing complex, iterative workflows.
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. |