83Plus:Basic:Repeat

From WikiTI
Revision as of 11:25, 23 February 2007 by Catsleepsunderoakleaves (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


The Repeat loop is a program-flow statement that repeats the execution a section of code at least once, based on the value of a condition. The Repeat loop is comprised of a Repeat( token, a conditional statement, and a block of code terminated by an End token. It is similar to, but fundamentally different than, the While loop.

Synopsis

Token Size: 1 byte

Syntax:

:Repeat condition
:  code to be executed
:End

Synatax

The Repeat loop consists of at least three lines of code; the initialization and condition of the loop:

:Repeat( condition )

the body of the loop, possibly consisting of several lines, and a terminating End statement.

This loop pushes its return condition onto the stack, so it should ideally exclude Goto statements, or a memory error is possible.

Execution

When the Repeat( command is encountered, execution of the code block begins. At the end of the loop, the condition is evaluated. If it is false, the loop repeats; if it is true, the loop exits. Becuase continuation of the loop is dependant on the opposite boolean value of that which controls the While loop, the repeat loop is often used incorrectly.

Use

The Repeat loop can be generally used as an alternative to thw While loop. In general, a repeat should be used for a loop that should always run at least once. The exact choice is dependant on the needs of the programmer.

The following are essentially identical:

:Repeat(Z=105)
:  ...
:getKey\->\Z
:End
...comapre to...
:0\->\Z
:While(Z≠105)
:  ...
:getKey\->\Z
:End

The Repeat loop is smaller than the While by three bytes. A similar program using the Ans variable makes the While loop larger by one byte. Either way, the Repeat loop is generally smaller.