# SCI Programming Language/Control Flow

Chapter: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Index

Control Flow
Author: Jeff Stephenson

## Control Flow

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.

In the following, code1, code2, ... codeN are sequences of expressions. Brackets [...] indicate optional entries. The term "not FALSE" is used to indicate a non-zero result.

## Conditionals

### if

`(if expression code1 [else code2])`

If expression is not FALSE, execute code1, else execute code2.

example:  Set the value 0'x to the larger of a or b.

Code:
```(if (> a b)	;expression
( = x a)	;code1
else
(= x b)	;code2
)```

### cond

`(cond (expl codel) (exp2 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.

example:  Set x to the larger of a or b and to 0 if a = b.

Code:
```(cond
((== a b)	;expression1
(= x 0)	;code1
)
((> a b)	;expression2
(= x a)	;code2
)
(else
(= x b)	;codeN
)```

### switch

`(switch expression (exp1 code1) (exp2 code2) ... [(else codeN) 1 )`

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.

example:  Evaluates a -b.

Code:
```(switch (-a b)						;expression
(0							;expression1
(Prints "They are equal")				;code1
)
(-1							;expression2
(Prints "B is one unit larger than A")		;code2
)
( 1							;expression3
(Prints "A is one unit larger than B")		;code3
)
(else
(Prints "A and B differ by more than one")	;codeN
)
)```

### switchto

`(switchto e xpression (code1) (code2) ... [(else codeN)])`

Evaluate expression. If it is equal to 0, execute code1. If it is equal to 1, execute code2, and so on. switchto is a shorthand form of the switch statement where the test values are consecutive integers beginning with 0. It is commonly used in the changeState method of an SCI script object (where the expression is the state of the script).

example:  Evaluates the variable stateNum.

Code:
```(switchto stateNum					;expression
(
(Prints "This is state O")			;code1
)
(
(Prints "This is state 1")			;code2
)
(else
(Prints "The variable stateNum is not 0 or 1")	;codeN
)
)```

## Iteration

### for

(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.

example:  Set x = a + b while i < n. Note that this value does not change.

Code:
```(for
((= a 0)(= b 1)(= i 3))	;initialization
(< i n)			;condition
((++ i))			;re-initialization
(= x (+ a b))		;code
)```

Note that the initialization statements may include more than one expression and therefore require a set of surrounding parentheses.

### while

`(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 resultant value of a while condition is always FALSE.) This is equivalent to:

`(for (0) condition (0) code)`

example:  Set x = x + the incremented value of i while i < n.

(while (< i n) ;condition (+= x (++ i)) ;code )

### repeat

`(repeat code)`

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

`(while TRUE code) or (for (0) TRUE (0) code)`

example:  Set x =x + 2. Note that this will loop forever.

Code:
```(repeat
(+= x 2)	;code
)```

## Supporting constructs tor iteration

### break

`(break [n])`

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

example:  Repeat incrementing i until i > n.

Code:
```(repeat
(++ i)
(if (> i n) (break))
)```

### breakif

`(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.

example:  Repeat incrementing i until i > n.

Code:
```(repeat
(++ i)
(breakif (> i n))
)```

### continue

`(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.

example: ||Set x =y / i unless i =O.

(for ((i = -5)) << i 5) ((++ i)) (if (== i 0) (continue)) (=x (/ y i)) )

### contif

`(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.

example:  Same as "continue" except using the logical NOT to test i.

Code:
```(for ((= i -5)) << i 5) ((++ i))
(contif (not i))
(=x(/yi))
)```

## return

`(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.

example:

Code:
```(return
(+ x k )		;optional expression
)```
Notes