Multiple Statements
Conditional Statements
Recursion
While Loops

Multiple Statements
Evaluating a variable can result in more than one statement being executed. Typing the \ key inserts a vertical
blue bar which separates multiple statements.
Example
The key strokes
a <ctrl>x 3 <up arrow>\ b <ctrl>x 7 <up arrow>\ c <ctrl>x a + b <ctrl>z
yield the following result.
The <up arrow> keys are needed because the vertical bar has the same precedence as the define.
Notice that the first two statements that set the values of a and b are evaluated, the value returned by the
multiple statement is the value of the last component, the result of setting c equal to a+b, which is 10.

Conditional Statements
Evaluation of a statement of the form
proceeds as follows:
exp1 is evaluated, and if the value is TRUE, then the value of exp2 is returned.
The right arrow is made by typing <ctrl>a.
The conditional statement provides a way to stop the execution of a multiple statement. If a conditional statement
is one component of a multiple statement and the condition is true, then the remaining components of the conditional
statement are not evaluated.
Example
Both of the multiple statements above contain a conditional statement. In the first case, the condition 2 <
3 is true, so the value of a is returned, and the last component, the constant 35 is not reached.
In the second case, the condition 7 < 3 is not true, so a is not returned. Instead, the last component is
evaluated and its value, 35, is returned.
Example

Recursion
Functions may be defined recursively with conditional statements and multiple statements..
Example
The key strokes
f(n <up arrow><up arrow><ctrl>x n < 2 <ctrl>a 1 \ n*f(n-1 <ctrl>z
produce the recursively defined function below.
The value of f(n) is n factorial (n !).
Warning: You must be careful with recursive statements like the definition of f above. If we had not
provided a conditional statement to stop the execution of the multiple statement, then f would continue calling
itself until reaching stack overflow. That would probably lockup the browser.

While Loops
A while loop has the form
while( boolean statement | exp2 | exp3 | ...)
The boolean statement must return either true or false. The subsequent components of the multiple statement
can be statements of any nature.
Example
Notice that the last component of the multiple statement inside the while loop is the variable total. When the
condition on n is no longer satisfied, the last value of the last statement is returned.
See the Simulation of a Game on the Samples page for more programming examples.
|