Difference between revisions of "SCI Programming Language/Control Flow"

From SCI Wiki
Jump to navigationJump to search
 
(13 intermediate revisions by the same user not shown)
Line 24: Line 24:
 
 
 
 
  
== Control Flow ==
+
==<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:
 
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:
Line 148: Line 148:
  
 
==<br /> Iteration ==
 
==<br /> Iteration ==
 
+
<blockquote>
 
===<br /> for ===
 
===<br /> for ===
 
<blockquote>
 
<blockquote>
Line 171: Line 171:
  
 
===<br /> while ===
 
===<br /> while ===
 +
<blockquote>
 +
<code>(while condition code)</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 resultant value of a while condition is always FALSE.) This is equivalent to:
  
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>
  
(for (0) condition (0) code).
+
''example: &nbsp;Set x = x + the incremented value of i while i < n.''
|-
 
|''example:'' ||Set x = x + the incremented value of i while i < n.
 
  
 +
<blockquote>
 
(while
 
(while
 
   (< i n) ;condition
 
   (< i n) ;condition
 
       (+= x (++ i)) ;code
 
       (+= x (++ i)) ;code
 
)
 
)
 
+
</blockquote>
 
+
</blockquote>
Page 18
 
 
 
 
 
 
 
  
 
===<br /> repeat ===
 
===<br /> repeat ===
 +
<blockquote>
 +
<code>(repeat 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:
  
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>
  
(while TRUE code) or (for (0) TRUE (0) code)
+
''example: &nbsp;Set x =x + 2. Note that this will loop forever.''
|-
 
|''example:'' ||Set x =x + 2. Note that this will loop forever.
 
  
 +
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
<syntaxhighlight lang="sci">
Line 207: Line 205:
 
)
 
)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</blockquote>
 +
</blockquote>
 +
</blockquote>
  
 
==<br /> Supporting constructs tor iteration ==
 
==<br /> Supporting constructs tor iteration ==
 
+
<blockquote>
 
===<br /> break ===
 
===<br /> break ===
 +
<blockquote>
 +
<code>(break [n])</code>
  
(break [n])
+
Break out of n levels of loops. If n is not specified break out of the innermost loop.
  
Break out of n levels of loops. If n is not specified break out of the innermost loop.
+
''example: &nbsp;Repeat incrementing i until i > n.''
|-
 
|''example:'' ||Repeat incrementing i until i > n.
 
  
 +
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
<syntaxhighlight lang="sci">
Line 225: Line 227:
 
)
 
)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</blockquote>
 +
</blockquote>
  
 
===<br /> breakif ===
 
===<br /> breakif ===
 +
<blockquote>
 +
<code>(breakif expression [n])</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.
  
If expression is not FALSE, break out of n levels of loops. If n is not specified, break out of the innermost loop.
+
''example: &nbsp;Repeat incrementing i until i > n.''
|-
 
|''example:'' ||Repeat incrementing i until i > n.
 
  
 +
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
<syntaxhighlight lang="sci">
Line 241: Line 246:
 
)
 
)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</blockquote>
 +
</blockquote>
  
 
===<br /> continue ===
 
===<br /> continue ===
 
+
<blockquote>
(continue [n])
+
<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.
 
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.''
  
Page 19
+
<blockquote>
 
 
 
 
 
 
 
 
|-
 
|''example:'' ||Set x =y / i unless i =O.
 
 
 
 
(for ((i = -5)) << i 5) ((++ i))
 
(for ((i = -5)) << i 5) ((++ i))
 
       (if (== i 0) (continue))
 
       (if (== i 0) (continue))
 
       (=x (/ y i))
 
       (=x (/ y i))
 
)
 
)
 +
</blockquote>
 +
</blockquote>
  
 
===<br /> contif ===
 
===<br /> contif ===
 +
<blockquote>
 +
<code>(contif expression [n])</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.
  
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: &nbsp;Same as "continue" except using the logical NOT to test i.''
|-
 
|''example:'' ||Same as "continue" except using the logical NOT to test i.
 
  
 +
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
<syntaxhighlight lang="sci">
Line 277: Line 281:
 
)
 
)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</blockquote>
 +
</blockquote>
 +
</blockquote>
  
 
==<br /> return ==
 
==<br /> return ==
 
+
<blockquote>
(return [expression))
+
<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.
 
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:
+
''example:''
  
 +
<blockquote>
 
<div class="CodeBlockHeader">Code:</div>
 
<div class="CodeBlockHeader">Code:</div>
 
<syntaxhighlight lang="sci">
 
<syntaxhighlight lang="sci">
Line 292: Line 300:
 
)
 
)
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</blockquote>
 +
</blockquote>
  
 
;Notes
 
;Notes

Latest revision as of 19:10, 26 May 2016

Official SCI Documentation

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


Table of Contents

< Previous: Definitions Next: Procedures >