Software: Apache/2.0.54 (Unix) mod_perl/1.99_09 Perl/v5.8.0 mod_ssl/2.0.54 OpenSSL/0.9.7l DAV/2 FrontPage/5.0.2.2635 PHP/4.4.0 mod_gzip/2.0.26.1a uname -a: Linux snow.he.net 4.4.276-v2-mono-1 #1 SMP Wed Jul 21 11:21:17 PDT 2021 i686 uid=99(nobody) gid=98(nobody) groups=98(nobody) Safe-mode: OFF (not secure) /usr/doc/slang-1.4.5/html/ drwxr-xr-x | |
| Viewing file: Select action/file-type: 8. Statements
Loosely speaking, a statement is composed of expressions that are grouped according to the syntax or grammar of the language to express a complete computation. Statements are analogous to sentences in a human language and expressions are like phrases. All statements in the S-Lang language must end in a semi-colon. A statement that occurs within a function is executed only during execution of the function. However, statements that occur outside the context of a function are evaluated immediately. The language supports several different types of statements such as assignment statements, conditional statements, and so forth. These are described in detail in the following sections.
8.1 Variable Declaration StatementsVariable declarations were already discussed in chapter ???. For the sake of completeness, a variable declaration is a statement of the form
where the variable-declaration-list is a comma separated list
of one or more variable names with optional initializations, e.g.,
8.2 Assignment Statements
Perhaps the most well known form of statement is the assignment statement. Statements of this type consist of a left-hand side, an assignment operator, and a right-hand side. The left-hand side must be something to which an assignment can be performed. Such an object is called an lvalue. The most common assignment operator is the simple assignment
operator
In addition to the simple assignment operator, S-Lang
also supports the assignment operators += and -=.
Internally, S-Lang transforms
to
Similarly, a -= b is transformed to a = a - b. It is
extremely important to realize that, in general, a+b is not
equal to b+a. This means that a+=b is not the same
as a=b+a. As an example consider
After execution of these two statements, a will have the
value "helloworld" and not "worldhello".
Since adding or subtracting
are all equivalent. Similarly,
are also equivalent.
Strictly speaking,
The closest valid S-Lang form involves a comma-expression:
S-Lang also supports a multiple-assignment statement. It is discussed in detail in section ???.
8.3 Conditional and Looping Statements
S-Lang supports a wide variety of conditional and looping statements. These constructs operate on statements grouped together in blocks. A block is a sequence of S-Lang statements enclosed in braces and may contain other blocks. However, a block cannot include function declarations. In the following, statement-or-block refers to either a single S-Lang statement or to a block of statements, and integer-expression is an integer-valued expression. next-statement represents the statement following the form under discussion.
Conditional Forms
ifThe simplest condition statement is the
If integer-expression evaluates to a non-zero result, then the
statement or group of statements implied statement-or-block
will get executed. Otherwise, control will proceed to
next-statement.
An example of the use of this type of conditional statement is
This example illustrates two if statements where the second
if statement is part of the block of statements that belong to
the first.
if-elseAnother form of
Here, if expression returns non-zero,
statement-or-block-1 will get executed and control will pass
on to next-statement. However, if expression returns zero,
statement-or-block-2 will get executed before continuing with
next-statement. A simple example of this form is
Consider the more complex example:
This example illustrates a problem that beginners have with
if-else statements. The grammar presented above shows that
the this example is equivalent to
It is important to understand the grammar and not be seduced by the
indentation!
!if
One often encounters
or equivalently,
The !if statement was added to the language to simplify the
handling of such statements. It obeys the syntax
and is functionally equivalent to
orelse, andelse
These constructs were discussed earlier. The syntax for the
This causes each of the blocks to be executed in turn until one of
them returns a non-zero integer value. The result of this statement
is the integer value returned by the last block executed. For
example,
returns 6 since the second block is the first to return a
non-zero result. The last two block will not get executed.
The syntax for the
Each of the blocks will be executed in turn until one of
them returns a zero value. The result of this statement is the
integer value returned by the last block executed. For example,
returns 0 since the third block will be the last to execute.
switchThe switch statement deviates the most from its C counterpart. The syntax is:
The `:' operator is a special symbol which means to test
the top item on the stack, and if it is non-zero, the rest of the block
will get executed and control will pass out of the switch statement.
Otherwise, the execution of the block will be terminated and the process
will be repeated for the next block. If a block contains no
: operator, the entire block is executed and control will
pass onto the next statement following the switch statement.
Such a block is known as the default case.
As a simple example, consider the following:
Suppose x has an integer value of 3. The first two
blocks will terminate at the `:' character because each of the
comparisons with x will produce zero. However, the third
block will execute to completion. Similarly, if x is
7, only the last block will execute in full.
A more familiar way to write the previous example used the
The case keyword is a more useful comparison operator because
it can perform a comparison between different data types while
using == may result in a type-mismatch error. For example,
will fail because the == operation is not defined between
strings and integers. The correct way to write this to use the
case keyword:
Looping Forms
whileThe
It simply causes statement-or-block to get executed as long as
integer-expression evaluates to a non-zero result. For
example,
will cause the newline function to get called 10 times.
However,
would loop forever (or until i wraps from the most negative
integer value to the most positive and then decrements to zero).
If you are a C programmer, do not let the syntax of the language seduce you into writing this example as you would in C:
The fact is that expressions such as i-- do not return a
value in S-Lang as they do in C. If you must write this way, use
the comma operator as in
do...whileThe
The main difference between this statement and the while
statement is that the do...while form performs the test
involving integer-expression after each execution
of statement-or-block rather than before. This guarantees that
statement-or-block will get executed at least once.
A simple example from the jed editor follows:
This will cause all lines in the buffer to get indented via the
jed intrinsic function indent_line.
forPerhaps the most complex looping statement is the
In addition to statement-or-block, its specification requires
three other expressions. When executed, the for statement
evaluates init-expression, then it tests
integer-expression. If integer-expression returns zero,
control passes to next-statement. Otherwise, it executes
statement-or-block as long as integer-expression
evaluates to a non-zero result. After every execution of
statement-or-block, end-expression will get evaluated.
This statement is almost equivalent to
The reason that they are not fully equivalent involves what happens
when statement-or-block contains a continue statement.
Despite the apparent complexity of the
which computes the sum of the first 10 integers.
loopThe
If the integer-expression evaluates to a positive integer,
statement-or-block will get executed that many times.
Otherwise, control will pass to next-statement.
For example,
will cause the function newline to get called 10 times.
foreverThe
A trivial example of this statement is
foreachThe The simple type of
Here container-object can be an expression that returns a
container object. A simple example is
This example shows that if the container object is an array, then
successive elements of the array are pushed onto the stack prior to
each execution cycle. If the container object is a string, then
successive characters of the string are pushed onto the stack.
What actually gets pushed onto the stack may be controlled via the
The allowed values of control-list will depend upon the type
of container object. For associative arrays (Assoc_Type),
control-list specified whether keys, values, or both
are pushed onto the stack. For example,
results in the keys of the associative array a being pushed
on the list. However,
will cause the values to be used, and
will use both the keys and values of the array.
Similarly, for linked-lists of structures, one may walk the list via code like
This foreach statement is equivalent
Consult the type-specific documentation for a discussion of the
using control words, if any, appropriate for a given type.
8.4 break, return, continue
S-Lang also includes the non-local transfer functions
Here, a function fun has been defined that contains a forever
loop consisting of statements s1, s2,...,s3, and
three if statements. As long as the expressions condition_1,
condition_2, and condition_3 evaluate to zero, the statements
s1, s2,...,s3 will be repeatedly executed. However,
if condition_1 returns a non-zero value, the break statement
will get executed, and control will pass out of the forever loop to
the statement immediately following the loop which in this case is
s4. Similarly, if condition_2 returns a non-zero number,
the return statement will cause control to pass back to the
caller of fun. Finally, the continue statement will
cause control to pass back to the start of the loop, skipping the
statement s3 altogether.
Next Previous Contents |
:: Command execute :: | |
--[ c99shell v. 1.0 pre-release build #13 powered by Captain Crunch Security Team | http://ccteam.ru | Generation time: 0.004 ]-- |