The Script Programming Language/Control Flow

From SCI Wiki
Jump to: navigation, search

The Original SCI Documentation

Chapter: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Index


The Script Programming Language

Control Flow

Author: Jeff Stephenson

 


Control Flow

In the following, code1, ..., codeN are arbitrary sequences of expressions. There are no BEGIN ... END blocks as in Pascal or progn forms as in Lisp.

The value of a control flow expression is the value of the last expression in the control body which was evaluated. Thus, if we execute the following code:

Code:
(= x 3)
     (= y 2)
     (= y (if (> x y)
          (- x y)
     else
          (+ x y)
     )
)

y will have the value 1.


Return:

Code:
(return [expression])

The return statement returns control to the procedure which called the currently executing procedure. If the optional expression is present, that value is returned as the value of the current procedure. There is an implicit return at the end of all procedures, and the value returned in that case is the value of the last expression evaluated. A return from the main procedure of script 0 returns to the operating system.


Conditionals:

Code:
(if expression code1 [else code2])

If expression is not FALSE, execute code1, else execute code2. (The else clause is optional).

Code:
(cond (e1 code1) (e2 code2) ... [(else codeN)])

Evaluate e1. If it is not FALSE, execute code1 and exit the cond clause. If it is FALSE, evaluate e2 and continue. If all of the expressions are FALSE and the optional else clause is present, execute codeN.

Code:
(switch expression (exp1 code1) (exp2 code2) ... [(else codeN)])

Evaluate expression. If it is equal to exp1, execute code1 and exit the switch. If it is equal to exp2, execute code2 and exit. If it doesn't equal any of the expressions and the optional else clause is present, execute codeN.


Iteration:

Code:
(for (initialization) condition (re-initialization) code)

Evaluate the expressions comprising initialization. Then evaluate condition. If the result is FALSE, exit the loop. Otherwise, execute code, then the expressions comprising re-initialization, and loop back to condition.

Code:
(while condition code)

Evaluate condition. If not FALSE, execute code and loop back to evaluate condition again. Exit the loop when condition is FALSE. (Note that this means that the value of a while condition is always FALSE.) This is equivalent to

Code:
(for () condition () code)
Code:
(repeat code)

Continually execute the code until some condition in the code (a break) causes the loop to be exited. This is equivalent to

Code:
(while TRUE code)

or

Code:
(for () TRUE () code)


Supporting constructs for iteration:

Code:
(break [n])

Break out of n levels of loops. If n is not specified break out of the innermost loop.

Code:
(breakif expression [n])

If expression is not FALSE, break out of n levels of loops. If n is not specified, break out of the innermost loop.

Code:
(continue [n])

Loop back to the beginning of the nth level loop. If n is not specified, loop to the beginning of the innermost loop.

Code:
(contif expression [n])

If expression is not FALSE, loop back to the beginning of the nth level loop. If n is not specified, loop to the beginning of the innermost loop.

 

Notes


 

Table of Contents

< Previous: Primitive Procedures Next: Procedures >