Slick-CŪ statements are constructed in the same manner as the statements in the C language.
Topics in this section:
The simple assignment statement has the syntax variable=expression. For example:
i=1; i=i+1;
Assignment statements can be cascaded (x=y=z). Assignment statements within if and while conditions are not allowed. The compiler flags assignments within if and while statements as an error. See Declaring Local Variables With := for more information.
The syntax for an if statement is the following:
if (expression) statement [else statement]
statement can be a C-style statement block which contains multiple statements. For more information, see Block Statement.
CAUTION The value 0 for all types is false. All other values are true. Like C++, Slick-CŪ uses the value 0 for null pointers. For the string type, only a one-byte length string where the first character is an ASCII 0 is false. A 0 length string ("") is true when used in a boolean expression. Slick-C also considers an empty (=null) pointer variable or class instance as false.
Example:
if (x<y) a==1;
if (x=="a") {
y=1;
} else if (x=="b") {
y=2;
} else if (x=="c") {
y=3;
} else if (x=="d") {
y=4;
}A statement block is typically used to allow multiple statements within an if or loop construct. However, it can also be used to declare a new local scope. A statement block has the following syntax, where statement may declare local variables:
{
statement1;
statement2;
...
}
Example:
int i=0;
if (i<1) {
int x=1;
{
int x;
// Can do the assignment here.
x=3;
}
// The variable x will be 1 here and not 3.
}Slick-CŪ supports C-style do, for, and while loops. In addition, Slick-C also supports Java/C#-style foreach loops and the Ada-style loop statement. You can use break and continue with all styles of loops.
The do loop executes statement first and then evaluates condition_exp. If expression is true (not the value 0), the statement continues to be executed until expression becomes false (0) or a break statement is reached.
Example:
[label:] do statement while ( condition_exp );
The C-style for loop is free-form. The expressions before the first semicolon of the for loop are executed before entering the loop. The condition_exp expression is checked before entering the for loop also. If condition_exp is true (not the value 0), the statement is executed. The statement continues to be executed until condition_exp becomes false (0) or a break statement is reached. When the bottom of the for loop is reached, but before condition_exp is checked again, the expressions after the second semicolon are executed.
The syntax of for is:
[label:] for (b4e1 ,b4e2 ... .b4e3]; [condition_exp] ; {cont_e1,cont_e2 ... ,cont_e3]) statement
Examples:
// The following loops are equivalent.
loop1:
for ( i=1;i<10;++i ) {
messageNwait("i="i);
}
loop2:
i=1;
for ( ;i<10; ) {
messageNwait("i="i);
++i;
}
loop3:
for ( i=1;i<10;++i ) messageNwait("i="i);
loop4:
i=1;
while ( i <10 ) {
messageNwait("i="i++);
}
loop5:
i=1;
do {
messageNwait("i="i);
} while ( i<10 );The foreach statement works with arrays, hash tables, strings (same as Bourne shell), structs (iterates over the fields of the struct), and classes (if instance of sc.lang.llterable, otherwise like structs). The syntax of foreach is:
foreach ( [ k => ] v in a ) {
statements;
}
Example:
void printStats(int (&statistics):[])
{
foreach (auto name => auto count in statistics) {
say("testForeach: "name"="count);
}
int i,j=0;
foreach (i in range(10, 20, 2)) {
say("printStats: sequence["j++"]="i);
}
}There is an optional key which is useful for hash tables. The value can be omitted (key=> . in ht):
foreach( key => value in ht ) {
statements;
}Both value and index can be auto-declared using the auto keyword. If value is auto-declared, its type will be inferred from the type of the collection. The implementation uses _nextel().
The generic loop statement is similar to that found in the Ada and D languages. The following statements are equivalent:
for(;;) { ... }
loop { ... }Example:
status := search(":","@");
loop {
if ( status ) break;
get_line(line);
messageNwait("found match line="line);
status = repeat_search();
}Another example:
defmain()
{
i:=0;
j:=0;
loop {
say("test, i="i);
i++;
if (i>1000) {
break;
}
inner: loop {
say("defmain: j="j);
if (j++ > 750) break inner;
}
say("defmain: H1");
if (i < 500) {
continue;
}
say("defmain: H2");
}
}The while loop evaluates condition_exp first and then executes statement if condition_exp is true (not the value 0). The statement will continue to be executed until condition_exp becomes false (0) or a break statement is reached.
Example:
[label:] while (condition_exp) statement
Loops are exited with the break primitive. The break primitive supports an optional label argument (like Java™). If specified, the label must match the label of one of the loops that you are currently using.
The continue primitive can be used to skip to the top of a loop. Using continue on a for loop causes the expressions after the second semicolon to be executed before condition_exp is checked. When continue is used on a do statement, the condition_exp is not checked and execution resumes at the top of the loop.
Loops can also be exited with the continue primitive. The continue primitive supports an optional label argument (like Java). If specified, the label must match the label of one of the loops that you are currently using.
Example:
outerloop:
for ( i=1;i<3;++i ) {
for ( j=1;;++j ) {
if ( j==2 ) continue outerloop; // Exit inner loop.
if ( j==3 ) break outerloop; // Exit both loops.
messageNwait("i="i);
}
} The syntax for parse is parse string with template. This statement parses string as specified by template.
The table below shows what template may contain.
|
Item |
Description |
|---|---|
|
variable_name |
Output variable. |
|
. |
Null output variable. |
|
nnn |
Number specifying new parse column. |
|
+ nnn |
Amount to increment parse column relative to start of last string found or last column setting. |
|
- nnn |
Amount to decrement parse column relative to start of last string found or last column setting. |
|
'text'[,search_options] |
String constant to search for. If found, parse column becomes first character after text. Otherwise parse column becomes first character after length of string being parsed. search_options is an optional expression that may evaluate to a string of one or more of the option letters U, R, B, I, and Y:
See the topic "regular expressions" in the SlickEditŪ Help system for more information. |
|
(expression)[,search_options] |
String expression to search for. If found, parse column becomes first character after text. Otherwise parse column becomes first character after length of string being parsed. See above for a description of the search options. |
The rules for parse column are:
The parse column is initialized to column 1.
If a column or column increment specifies a column greater than the length of the string being parsed, the parse column is set to the length of the string being parsed plus one.
If a column decrement specifies a column less than the length of the string being parsed, the parse column is set to column 1.
The rules for setting output variables are:
Output variables are set in groups. An output variable group is defined to be consecutive variables with no search or column specifiers between them.
Before variables of an output variable group can be set, the end parse column within the source string must be found. In the case the end parse column is set by a search, the end parse column for this output variable group becomes the first character to the left of the text found. In the case the end parse column is set by a column or column increment the end parse column becomes the first character to the left of the column. The start parse column is the current parse column as specified by the template.
A word parse of the text between the start and end columns is performed to set the variables in an output variable group if the group contains more than one variable. Otherwise the one output variable is set to the text between the start and end columns of the source string. Each variable set by a word parse will have no leading or trailing tabs/spaces except for the last output variable which is set to the rest of the sub-string.
If the start column is greater than the end column the variables in the output group are set to null.
Wildcard regular expressions are supported for parse. You can also use the auto keyword to auto-declare the output string variables in a parse statement. For example, the following statement declares "firstword" and "secondword" as strings:
parse s with auto firstword auto secondword;
Examples of parse:
// Results are a=='1', b=='2', c=='3'. parse '1 2 3' with a b c; // Results are a=='1', b=='2', c=='3'. Note that tab and space characters are // stripped. parse '1 '\t' 2 '\t' 3' with a b c; // Results are a=='1', b=='3'. parse '1 2 3' with a . b; // Results are a=='1', b=='2', c=='3', d=='4', e=='5'. parse 'xxx1 2 3yyy 4 5' with 'xxx' a b c 'yyy' d e; // Results are a=='1 2 3', b==' 4 5'. parse 'xxx1 2 3yyy 4 5' with 'xxx' a 'yyy' b; // Results are a=='xxx1 2 3', b=='yyy 4 5'. parse 'xxx1 2 3yyy 4 5' with 'xxx' +0 a 'yyy' +0 b; // Results are delim=='/', s1=='x', s2=='y', options==''. parse 'c/x/y' with 2 delim +1 s1 (delim) s2 (delim) options;
Slick-CŪ supports the C switch statement. The Slick-C switch supports integers and string types. The switch statement uses the following syntax:
switch (expression) {
[ case expression:
statements
]
[ case expression:
statements
]
...
[ default:
statements
]
}
The switch expression is evaluated and compared against all the case expressions. After a match is found, ALL statements below the case are executed, including those statements found in the next case and the default, until a break statement is reached.
Example:
outerloop:
for ( i=1;;++i ) {
switch ( i ) {
case 1:
case 2:
messageNwait("i=1 or i=2");
break;
// Done with these cases.
case 3:
break outerloop;
}
}