Difference between revisions of "SCI Programming Language/Control Flow"
(26 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
<div align="center"> | <div align="center"> | ||
Chapter: | Chapter: | ||
− | [[ | + | [[SCI Programming Language/Introduction|1]] | |
− | [[ | + | [[SCI Programming Language/Primitive Procedures|2]] | |
− | [[ | + | [[SCI Programming Language/Primitive_Procedures#Arithmetic_Primitives|3]] | |
− | [[ | + | [[SCI Programming Language/Primitive_Procedures#Boolean_Primitives|4]] | |
− | [[ | + | [[SCI Programming Language/Primitive_Procedures#Assignment_Primitives |5]] | |
− | [[ | + | [[SCI Programming Language/Data Types and Variables|6]] | |
− | [[ | + | [[SCI Programming Language/Definitions|7]] | |
− | [[ | + | [[SCI Programming Language/Control Flow|8]] | |
− | [[ | + | [[SCI Programming Language/Control Flow#Conditionals|9]] | |
− | [[ | + | [[SCI Programming Language/Control Flow#Iteration|10]] | |
− | [[ | + | [[SCI Programming Language/Procedures|11]] | |
− | [[ | + | [[SCI Programming Language/Files|12]] | |
− | [[ | + | [[SCI Programming Language/Compiling SCI|13]] | |
− | [[ | + | [[SCI Programming Language/Index|Index]] |
</div><br /> | </div><br /> | ||
− | <div align="center"><span style="font-size: 22pt"> | + | <div align="center"><span style="font-size: 22pt">Control Flow</span><br /> |
''Author: [[Jeff Stephenson]]''</div> | ''Author: [[Jeff Stephenson]]''</div> | ||
| | ||
+ | |||
+ | ==<br /> 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: | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (= x 3) | ||
+ | (= Y 2) | ||
+ | (= Y | ||
+ | (if (> x y) | ||
+ | (-x y) | ||
+ | else | ||
+ | (+ x y) | ||
+ | ) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | |||
+ | 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. | ||
+ | |||
+ | ==<br /> Conditionals == | ||
+ | <blockquote> | ||
+ | ===<br /> if === | ||
+ | <blockquote> | ||
+ | <code>(if expression code1 [else code2])</code> | ||
+ | |||
+ | If expression is not FALSE, execute code1, else execute code2. | ||
+ | |||
+ | ''example: Set the value 0'x to the larger of a or b.'' | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (if (> a b) ;expression | ||
+ | ( = x a) ;code1 | ||
+ | else | ||
+ | (= x b) ;code2 | ||
+ | ) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
+ | |||
+ | ===<br /> cond === | ||
+ | <blockquote> | ||
+ | <code>(cond (expl codel) (exp2 code2) ... [(else codeN)])</code> | ||
+ | |||
+ | 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.'' | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (cond | ||
+ | ((== a b) ;expression1 | ||
+ | (= x 0) ;code1 | ||
+ | ) | ||
+ | ((> a b) ;expression2 | ||
+ | (= x a) ;code2 | ||
+ | ) | ||
+ | (else | ||
+ | (= x b) ;codeN | ||
+ | ) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
+ | |||
+ | ===<br /> switch === | ||
+ | <blockquote> | ||
+ | <code>(switch expression (exp1 code1) (exp2 code2) ... [(else codeN) 1 )</code> | ||
+ | |||
+ | 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.'' | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (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 | ||
+ | ) | ||
+ | ) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
+ | |||
+ | ===<br /> switchto === | ||
+ | <blockquote> | ||
+ | <code>(switchto e xpression (code1) (code2) ... [(else codeN)])</code> | ||
+ | |||
+ | 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.'' | ||
+ | |||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (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 | ||
+ | ) | ||
+ | ) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
+ | |||
+ | ==<br /> Iteration == | ||
+ | <blockquote> | ||
+ | ===<br /> for === | ||
+ | <blockquote> | ||
+ | (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.'' | ||
+ | |||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (for | ||
+ | ((= a 0)(= b 1)(= i 3)) ;initialization | ||
+ | (< i n) ;condition | ||
+ | ((++ i)) ;re-initialization | ||
+ | (= x (+ a b)) ;code | ||
+ | ) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Note that the initialization statements may include more than one expression and therefore require a set of surrounding parentheses. | ||
+ | </blockquote> | ||
+ | |||
+ | ===<br /> while === | ||
+ | <blockquote> | ||
+ | <code>(while condition code)</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: | ||
+ | |||
+ | <blockquote><code>(for (0) condition (0) code)</code></blockquote> | ||
+ | |||
+ | ''example: Set x = x + the incremented value of i while i < n.'' | ||
+ | |||
+ | <blockquote> | ||
+ | (while | ||
+ | (< i n) ;condition | ||
+ | (+= x (++ i)) ;code | ||
+ | ) | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
+ | |||
+ | ===<br /> repeat === | ||
+ | <blockquote> | ||
+ | <code>(repeat code)</code> | ||
+ | |||
+ | Continually execute the code until some condition in the code (a break) causes the loop to be exited. This is equivalent to: | ||
+ | |||
+ | <blockquote><code>(while TRUE code) or (for (0) TRUE (0) code)</code></blockquote> | ||
+ | |||
+ | ''example: Set x =x + 2. Note that this will loop forever.'' | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (repeat | ||
+ | (+= x 2) ;code | ||
+ | ) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
+ | |||
+ | ==<br /> Supporting constructs tor iteration == | ||
+ | <blockquote> | ||
+ | ===<br /> break === | ||
+ | <blockquote> | ||
+ | <code>(break [n])</code> | ||
+ | |||
+ | 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.'' | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (repeat | ||
+ | (++ i) | ||
+ | (if (> i n) (break)) | ||
+ | ) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
+ | |||
+ | ===<br /> breakif === | ||
+ | <blockquote> | ||
+ | <code>(breakif expression [n])</code> | ||
+ | |||
+ | 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.'' | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (repeat | ||
+ | (++ i) | ||
+ | (breakif (> i n)) | ||
+ | ) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
+ | |||
+ | ===<br /> continue === | ||
+ | <blockquote> | ||
+ | <code>(continue [n])</code> | ||
+ | |||
+ | 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.'' | ||
+ | |||
+ | <blockquote> | ||
+ | (for ((i = -5)) << i 5) ((++ i)) | ||
+ | (if (== i 0) (continue)) | ||
+ | (=x (/ y i)) | ||
+ | ) | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
+ | |||
+ | ===<br /> contif === | ||
+ | <blockquote> | ||
+ | <code>(contif expression [n])</code> | ||
+ | |||
+ | 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.'' | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (for ((= i -5)) << i 5) ((++ i)) | ||
+ | (contif (not i)) | ||
+ | (=x(/yi)) | ||
+ | ) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
+ | |||
+ | ==<br /> return == | ||
+ | <blockquote> | ||
+ | <code>(return [expression))</code> | ||
+ | |||
+ | 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:'' | ||
+ | |||
+ | <blockquote> | ||
+ | <div class="CodeBlockHeader">Code:</div> | ||
+ | <syntaxhighlight lang="sci"> | ||
+ | (return | ||
+ | (+ x k ) ;optional expression | ||
+ | ) | ||
+ | </syntaxhighlight> | ||
+ | </blockquote> | ||
+ | </blockquote> | ||
+ | |||
+ | ;Notes | ||
+ | <references /> | ||
+ | |||
+ | [[SCI Programming Language | Table of Contents]] | ||
+ | |||
+ | <span style="float: left">[[SCI Programming Language/Definitions|< Previous: Definitions]]</span> | ||
+ | <span style="float: right">[[SCI Programming Language/Procedures|Next: Procedures >]]</span> | ||
+ | |||
+ | | ||
+ | |||
+ | [[Category:SCI Documentation]] | ||
+ | [[Category:SCI32]] | ||
+ | [[Category:Scripting]] |
Latest revision as of 19:10, 26 May 2016
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
< Previous: Definitions Next: Procedures >