T E L I X ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ SALT Programming Manual Copyright (C) 1986,1987,1988,1989,1990,1991 by Exis Inc. ALL RIGHTS RESERVED. Exis Inc. P.O. Box 130, West Hill, ON CANADA M1E 4R4 (416)-289-4641 / (416)-289-4645 fax (416)-439-9399 BBS Telix v3.15 - SALT Programming COPYRIGHT ii COPYRIGHT NOTICE Telix is Copyright (c) 1986,1987,1988,1988,1989,1990,1991 by Exis Inc. This document is Copyright (c) 1988,1989,1990,1991 by Exis Inc. No parts of Telix or this document may be copied in part or in whole, except as provided in the License included with Telix. DISCLAIMER Exis Inc. makes no warranty of any kind, either express or implied, including but not limited to implied warranties of merchantability and fitness for a particular purpose, with respect to this software and accompanying documentation. IN NO EVENT SHALL EXIS INC. BE LIABLE FOR ANY DAMAGES (INCLUDING DAM- AGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS PROGRAM, EVEN IF EXIS INC. HAS BEEN AD- VISED OF THE POSSIBILITY OF SUCH DAMAGES. TRADEMARKS Telix and SALT are trademarks of Exis Inc. Telix v3.15 - SALT Programming Contents iii C O N T E N T S 1. The Telix SALT Language.....................................1 1.1 What Can be Accomplished With SALT?........................1 1.2 About This Manual..........................................1 1.3 Notation...................................................1 1.4 Creating SALT Programs.....................................1 2. Syntax......................................................3 2.1 Comments...................................................4 3. Program Structure...........................................5 3.1 Variables..................................................5 3.2 Expressions and Operators..................................7 3.3 Functions..................................................9 3.4 Statements................................................10 3.4.1 The Expression statement..............................11 3.4.2 The If statement......................................11 3.4.3 The While statement...................................13 3.4.4 The Do...While statement..............................13 3.4.5 The For statement.....................................14 3.4.6 The Return statement..................................15 3.4.7 The Break statement...................................15 3.4.8 The Continue statement................................16 3.4.9 The Goto statement....................................17 4. Built-in Functions.........................................19 4.1 Quick Listing of Functions by Type........................20 4.2 Complete Function Reference...............................22 5. System Variables...........................................97 6. Appendix A - ASCII Character Set..........................114 7. Appendix B - Extended Key Scan Codes......................115 8. Appendix C - Color Values.................................116 9. Index.....................................................117 Telix v3.15 - SALT Programming Introduction 1 1. THE TELIX SALT LANGUAGE Telix has a built-in programming language called SALT (Script Applica- tion Language for Telix). SALT will allow you to perform almost any communications related applications with Telix. SALT looks similar to the C language, however if you have used almost any programming lan- guage (such as Pascal, BASIC, etc.), you should feel quite at home with SALT. While SALT was designed to be easy to learn, it is like most programming languages quite complete, so it is recommended that you read this chapter thoroughly and study the examples provided, as well as the sample SALT scripts included with Telix. 1.1 What Can be Accomplished With SALT? ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ Like a program in any programming language, a SALT program (also called a 'script') is typically used to perform a needed task or func- tion. The task can range from the very simple to the very complicated. For example, a SALT script can be linked to a dialing directory entry, so that when you have established a connection to that service, it au- tomatically sends your i.d. and password to the remote service. A much more complicated SALT script is used as the basis for the Host Mode included with Telix. 1.2 About This Manual ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ This manual is basically a reference to the SALT programming language. It is by no means a tutorial on programming in general. It is assumed that the reader of this manual is at least familiar with general pro- gramming concepts. 1.3 Notation ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ Throughout this manual, certain words in examples and in the text will be surrounded by angle brackets and italicized, for example, . These words are not to be taken as literal text. they stand for something else, such as a word, a group of words, or even several lines of text. What these italicized words stand for will be explained as they come up. 1.4 Creating SALT Programs ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ A SALT script is basically a sequence of instructions for Telix to follow, using a specific syntax. You may use any text editor to pro- duce this script file, as long as its output is normal ASCII text (this means that if you use your word processor, you must usually ex- plicitly tell it to write out the file using ASCII format and to not embed any special codes in the file). You may give any name you wish Telix v3.15 - SALT Programming Introduction 2 to a SALT source file, although we recommend that you always use the extension .SLT for clarity. Once you have written your script file and saved it to disk, it must be compiled. The program CS.EXE included with Telix reads your 'source' script file, and compiles it to a form which Telix can under- stand. The compiled script can then be loaded more quickly by Telix, and is also usually smaller. To compile a script source file, type cs while at the DOS prompt and then press Enter (or Carriage Return). The CS.EXE program must be in the current directory or on the DOS PATH. is the name of the file to compile, and may include the drive and directory as well as the filename. The output file is writ- ten to the same name except that the extension .SLC is used. When the script compiler finds an error in your source file, it will abort the compile process and give you the line number on which the error occurred, as well as the type of error. The error should then be fixed and the source re-compiled. This is repeated until the compiler detects no more errors in your source file. The compiled script can then be run in Telix using several methods. It may be run using the 'Run script' command, as a command-line option when running Telix from DOS, as a linked script to a dialing directory entry, or from another script. The first three methods are described in the Telix manual, while the last is described later in this manual. Telix v3.15 - SALT Programming Syntax 3 2. SYNTAX Case is not important in command, function, and variable names. The only time case matters is inside a string constant (e.g., "Hello" is not the same string as "hello"). Whitespace (such as the space, the tab, the Carriage Return, or the Line Feed character) is not impor- tant. The script compiler does not care where you place items, so that you may arrange the program as you see fit. For example, if (value == 1) prints("value is equal to 1!"); else prints("values is not equal to 1."); is equivalent to if (value == 1) prints("value is equal to 1!"); else prints("value is not equal to 1"); or even to if(value==1)prints("value is equal to 1!");else prints("value is not equal to 1."); The only time whitespace matters is when it would split up key-words or function name, or in a string. For example, the key-word 'while' must not be split up if it is to be recognized. The same applies to other key-words or function names. As well, there must be space be- tween the letters of a command and other letters. For example, 'while' is not the same as 'whileabc'. In the interest of clarity, it is rec- ommended that you try to make your code easy to understand, by indent- ing where appropriate, and by using space effectively. There is no reason, for example, to put more than one statement on a line, even if it is perfectly legal. A good example of program style can be found by looking at the sample scripts included with Telix. A string constant is a sequence of ASCII characters enclosed in quotes, for example, "Hello", "Good-bye", or "Telix". It is often nec- essary for a string constant to include special characters that can not easily be typed from the keyboard, or can not be easily displayed. This is done with something called the escape character, which is the caret ('^') symbol. When the SALT compiler is reading a string con- stant and comes to the '^' symbol, it replaces it with a certain ASCII code based on the character following the ^. Translations are as fol- lows: ^c 'c' is a letter. The Control representation of whatever letter 'c' is, is inserted into the text. Therefore ^M represents Ctrl-M, ^j represents Ctrl-J, etc. Whether the letter 'c' is upper or lower case is not signifi- cant. Note that what is really happening here is that 64 is being subtracted from the value of 'c', so for example, the Escape character can be represented as ^[. ^^ An actual caret ('^') symbol is placed into the text. Telix v3.15 - SALT Programming Syntax 4 ^" An actual double quote symbol ('"') is placed into the text. If a string must contain a double quote symbol, this is how it has to be done. If the plain '"' symbol were to be used, the compiler would think that the string was terminated at that point. For example, the string "He said, ^"Hello^"." is translated to 'He said, "Hello".'. ^' An actual single quote symbol (''') is placed into the text. ^nnn 'nnn' is up to 3 digits representing the ASCII value of the character which should be placed into the text. A maximum of three digits is read, or up to the first non-digit character. For example, the compiler would read in the string "S^65LT" and output the string "SALT", since 65 is the ASCII value of 'A'. Note that if nnn is less than 3 digits you may have to pad it with one or two leading zeros if there are digits imme- diately following it in the string, so that the wrong value is not read in. For example the string "^79 Park Avenue" would translate to "O Park Avenue" since 79 is the ASCII value of 'O'. If you actually wanted Ctrl-G (ASCII code 7) followed by "9 Park Avenue", you would use the string "^0079 Park Avenue". An integer constant is a sequence of digits representing an integer value in the range -2147483648 to 2147483647. An integer constant must start with a digit from 0 to 9 or the negative sign (-) followed by a digit. The following are all valid integer constants: 10 -400067 999 An integer constant may also be entered in hexadecimal form (base 16, where each digit may be from '0' to '9' or 'a' to 'f', to represent 16 values). Hex values must be preceded by '0x' for the compiler to in- terpret them as such, and case is not important. The following are all valid integer constants enter in hexadecimal form: 0xff00 0Xa2 0x7D 0x1AbCdEf 2.1 Comments ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ A comment in a source file is text that does not affect what the pro- gram does, and is meant purely for explaining or describing something. In a SALT source file, whenever the symbol // is encountered on a line, all the characters from that point on until the end of the line are considered to be a comment and are ignored. For example: prints("Hello"); // This line will print "Hello" Telix v3.15 - SALT Programming Program Structure 5 3. PROGRAM STRUCTURE A SALT script has the following format: ... ... ... and so on. Basically a script file consists of definition of global variables (variables which are available to any part of the script file after which they are defined, and function definitions (functions are lines of code clustered together in a group, so that they can be called by a name). A script file does not have to have any global variables or functions, but to run it must at least have one function called 'main'. The following, for example, is a complete script file: main() { prints ("hello"); } When compiled and run, this script would print the string "hello" to the screen. 3.1 Variables ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ A variable is a location in memory where something is stored. The con- tents of a variable can be changed by program code (hence the name). In SALT, there are two types of variables, integer variables, and string variables. The former holds an integer value (e.g., 485624, or -627), while the latter holds a text string (e.g. "Telix", or "SCRIPT"). Depending on where it is defined, a variable is either global or local. If a variable is global, it means that it can be used by any part of the script after the point where it is defined. If a variable is local, it means that it can only be used by the part of the script to which it is 'local', for example, the function inside which it is defined. A variable name can be up to 31 digits long, and may include the letters 'A' to 'Z' or 'a' to 'z', the digits '0' to '9', or the underscore character (_). The name may not start with a Telix v3.15 - SALT Programming Program Structure 6 digit. For example, 'his_name2' and '_his_name2' are legal as variable names, while '2his_name' is not. An integer variable is defined in the form int ; where is the name to be given to the variable. An alternate definition is int , , ..., ; which allows you to define more than one integer variable in one statement. An original value can be assigned to the integer variable by using the form int = ; where is an integer constant. Similarly, an original value can be assigned in the multiple definition above by placing the as- signment before the comma. Some examples are: int maximum; int start = 0; int level, i, count = 20, loop; A string variable is defined in the form str []; where is the name to be given to the variable. is the maximum number of characters that the string can hold, and must be in the range of 0 to 32767. An alternate definition is str [], [], ..., []; which allows you to define more than one string variable in a state- ment. An original value can be assigned to the string variable by us- ing the form str [] = ; where is a string constant. Similarly, an original value can be assigned in the multiple definition above by placing the as- signment before the comma. Some examples are: str password[80]; str password[40] = "mypass", name[30]; The string length field may be left empty if an original value is specified, in which case the length of the string variable is assumed to be that of the assigned text, e.g. str name[] = "John"; If a variable is outside of a function, it is global. If it is defined inside a function, it is local to that function and will only be rec- Telix v3.15 - SALT Programming Program Structure 7 ognized there. If a variable defined inside a function uses the same name as a global variable, any reference to that name while in the function will access the local variable. After the function has com- pleted, the local variable is removed and a reference to that name will access the global variable. 3.2 Expressions and Operators ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ An expression is a mixture of symbols which resolves to a value when evaluated. In other words, an expression is basically a formula. An expression can consist of constants, variables, function calls, and operators. An expression can be very simple, or very complicated. For example, some expressions are: 10 + 3 - 5 9 * 7 / 63 - 30 result = 10 * max(a, b) month >= 10 200 command == "bye" prints("Hello") In an expression, the data being acted upon are constants, variables, and functions calls, while the operators (+, *, etc.) are the symbols that do things with the data. There are many different operators, of which there are two basic types. Binary operators (such as +, *, /) perform a calculation on the expression on either side of them. Unary operators appear before a single expression and work on that. The fol- lowing table lists the operators available in SALT: Symbol (Un/Bin)ary What it is/does - unary Arithmetic negation ! unary Logical NOT not unary Logical NOT (alternate) ++ unary Increment -- unary Decrement * binary Multiplication / binary Division % binary Remainder (Mod) + binary Addition - binary Subtraction < binary Less than > binary Grater than <= binary Less than or equal to >= binary Greater than or equal to == binary Equality != binary Inequality & binary Bitwise AND | binary Bitwise OR ^ binary Bitwise Exclusive OR && binary Logical AND and binary Logical AND (alternate) || binary Logical OR or binary Logical OR (alternate) = binary Assignment Telix v3.15 - SALT Programming Program Structure 8 Note that the hyphen symbol can be either an arithmetic negation or a subtraction depending on its use. Note that '!' is equivalent to 'not', '&&' is equivalent to 'and', and '||' is equivalent to 'or'. The first form is preferred as you do not have to leave whitespace around it for the compiler to recognize it, but beginners may have an easier time remembering the second form. Also, do not confuse the '=' (assignment operator) with the '==' (equality operator). The former is used to assign a value to a variable, while the latter is used to com- pare two values. Assuming you have the two expressions, and , = would assign one to the other, while == would test the two to see if they are equal. For example num = 10 would assign the value 10 to the variable called 'num', while num == 10 would resolve to a value of non-zero (TRUE) if num was equal to 10, and 0 (FALSE) if num was not equal to 10. There is also a difference between the Logical operators and the Bitwise operators. The Logical operators (such as and, &&, or, ||, etc), work with TRUE or FALSE val- ues and result in a TRUE or FALSE value, while the Bitwise operators (&, |, ^) work with the actual bits of the data they are handling. The Bitwise operators almost never have to be used in a Telix script, un- less it is needed to get at the actual bits in a data byte. Every operator resolves to a value, which is the result of the opera- tion performed (e.g, 10 * 7 would resolve to 70). The conditional or equality operators such as ==, >, <=, etc., resolve to a 0 (FALSE)) or non-zero (TRUE) value based on the results of the expression. Even the assignment operator = resolves to a value. The result of the ex- pression num = 10 would be 10. All the operators have something called precedence, which is their im- portance, and determines the order in which they are evaluated. For example, 7 + 3 * 9 is equal to 34, because 3 * 9 is evaluated first, and then added to 7 (* has a higher precedence than +). All the opera- tors are listed below in order of decreasing precedence. All the oper- ators on the same line have the same precedence, and are resolved in the order that they are encountered. Telix v3.15 - SALT Programming Program Structure 9 - ! ++ -- * / % + - < > <= >= == != & | and && or || = If a certain evaluation order is required that does not follow these rules of precedence, parentheses may be used. Thus, 99 + 1 * 10 equals 109, while (99 + 1) * 10 equals 1000. If you are writing an expression of any sort, and are not sure of the exact precedence of the operators you are using, use parentheses! 3.3 Functions ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ A function is a way of grouping together some lines of code. A Telix script consists of one or more functions. There are quite a few advan- tages to having functions: One function can be called from another, to do a certain task. The calling function does not have to know anything about the called function other than what it does. This allows a script to be split up into modular units, and makes code writing and debug- ging easier. As mentioned above, what a function does it private. This means that data variables defined in a function are local to that func- tion, and therefore you do not have to worry about another part of the script unintentionally modifying them. A library of functions can thus be built. Later, you do not have to re-write old code. Functions are defined in the following format: (, , ..., ) { ... ... } Telix v3.15 - SALT Programming Program Structure 10 is the name of the function. It follows the same rules of other identifiers in SALT. There can only be one function that uses a given name, however. through are the declarations of the arguments (parameters) that have been passed to the function by its caller (sometimes, to accomplish its task, a function needs to have some val- ues passed to it). Each argument is defined in the form where is 'int' or 'str', and is the name it should be called by. At present, a function is not allowed to have more than 12 values passed to it. is a variable definition, as described in the above section on that topic. Any number of variables may be declared at this part of the function. All such variables will be local variables and available only to this function. is an actual line of code. There may be as many lines of statements in the function as needed. The format of a statement is de- scribed below. First though, here is an example of a complete func- tion: max ( int a, int b ) { int result; if (a > b) result = a; else result = b; return result; } This function returns the larger (maximum) of the two values passed to it. It could have been written much more simply (without the use of the variable), but was written this way so that all the function ele- ments would be there. 3.4 Statements ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ A statement is the basic element of code. A statement ALWAYS ends with a semicolon character (;). In any location where a statement is ac- ceptable, you may use a group of statements, by enclosing them all in curly braces (more on this below). There are many types of statements, including: expression, if, while, do...while, for, return, break, con- tinue, and goto statements. Each type has several different parts. Telix v3.15 - SALT Programming Program Structure 11 3.4.1 The Expression statement The 'expression' statement is the simplest and most common type of statement. Its format is ; where is any expression. Example are: result = 20; password = "Beef"; pause(20); num = 20 * max(a, b); Do not forget the semicolon character at the end of the statement. If you do, the compiler will think that the next statement is part of the current one, and will report some unexpected error. 3.4.2 The If statement An 'if' statement is used when a statement or group of statements should be evaluated only if a condition is true. The format for an 'if' statement is as follows: if () is any statement as described above and below (that is, an expression, if, while, do...while, for, return, break, or continue statement), and will only be executed if evaluates to non-zero. By using curly braces around them, a whole group of state- ments may be conditionally evaluated. Some examples are: if (result == -1) prints("ERROR!"); if (num_tries > maximum) return 0; if (month > 10 && day < 20) { clear(); prints("In range."); return 1; } An alternate form of the if statement is: if () else In this case, if evaluates to non-zero (TRUE), is executed, otherwise is executed. Again, Telix v3.15 - SALT Programming Program Structure 12 multiple statements may be used instead by grouping them in curly braces. Some examples are: if (stat == -1) prints("Error status returned."); else prints("Function finished without problems."); if (level < 10) { alarm(1); prints("Warning!"); } else prints("Everything's ok."); Since the statement to be executed conditionally can be of any type, that means that any number of if statement can be nested if needed. For example: if (num < 10) if (!error) if (read != 0) return 1; This also means that something like the following is legal: if (value == 10) do_this(); else if (value == 100) do_that(); else if (value == 1000) do_something_else(); else do_whatever(); What is really happening here is that each if statement is being nested after the else portion of the previous one. The above example could also be written as: if (value == 10) do_this(); else if (value == 100) do_that(); else if (value == 1000) do_something_else(); else do_whatever(); Any amount of nesting is theoretically legal, but the compiler does have a limit due to memory constraints. While you may write the code in any way which suits you, it is recom- mended that you use indenting, for clarity. Indenting your code at the proper places makes it a lot easier to read. Telix v3.15 - SALT Programming Program Structure 13 A very common error to watch out for is accidentally placing a semi- colon after the parenthesis ending the expression. For example, if the following is run: if (num == 10); prints("Num is equal to 10); the string would always be printed, no matter what num was equal to. This is because the semicolon after the parenthesis ending the expres- sion signifies the end of the statement. In the above case, it would just be a null (empty) statement. 3.4.3 The While statement The while statement is used to loop continuously while a certain con- dition is true. It has the form while () would continue to be repeated over and over while evaluated to non-zero (TRUE). Note that if the expression evaluates to 0 (FALSE) from the beginning, the statement will never be executed. Again, multiple statements may be used by surrounding them in curly braces. A few examples are: while (stat != -1) stat = myfunc(); while (num < 100) { printn(num); prints(""); num = num + 1; } while (1) { if (func1()) return 0; func2(); } Again, be careful to not place a semicolon after the parenthesis end- ing the expression. 3.4.4 The Do...While statement The do...while statement is similar to the while statement and has the form: do while (); Telix v3.15 - SALT Programming Program Structure 14 will be executed at least once and will continue to be ex- ecuted repeatedly until the expression becomes 0 (FALSE). A few exam- ples are: do stat = func1(); while (stat != -1); do { prints("hello"); num = num + 1; } while (num < 100); 3.4.5 The For statement The for statement is used to loop continuously while a certain condi- tion is true. The advantages over the while statement is that a count variable can be initialized and incremented quite easily. The for statement has the form: for (; ; ) The first expression is the one that should initialize the count vari- able. For example, if you wanted to count from 1 to 100, and were keeping the count in a variable called 'num', the first expression would be 'num = 1'. The second expression is the conditional test. As long as it evaluates to non-zero (TRUE), the statement will be exe- cuted. Following the above example, this expression would be 'num < 100'. The third expression is the one that is used to increment the count variable. For the above example, it would therefore be 'num = num + 1'. This for statement differs in format from that in most other languages, but doing it this way is actually gives the pro- grammer a lot of power and flexibility. Note that any of the expres- sions can be left empty, in which case they evaluate to non-zero (TRUE). Some examples are: for (count = 0; count < 100; count = count + 1) { printn(count); prints(""); } for (c = 1000; c > 0; c = c - 1) do_this(c); The following would execute an infinite loop: for (;;) prints("Hello!"); Note that there is really no restriction on what the expressions are. For example, the following is quite legal: Telix v3.15 - SALT Programming Program Structure 15 for (c = num = 0; c < 100 && stat != -1; c = c + 1) { stat = func(num); num = func2(); } The statements would only be executed if c was smaller than 100 and stat didn't equal -1. 3.4.6 The Return statement At some time, every function must be exited. If the end of the func- tion is reached, control will automatically return to the calling function. Very often however, it is necessary to leave a function somewhere while only halfway through it, perhaps based on a condi- tional test. As well, it is often necessary that a function returns a value to the caller. The format of the return statement is: return ; If the return statement is encountered anywhere in the function, con- trol immediately returns to the function that called this function. The expression is the value that should be returned. If no expression is supplied, a dummy value is returned. The expression should match they type of value that the caller of this function is expecting. That is, if an 'int' type is expected, the expression should resolve to an integer value. If a 'str' type is expected, the expression should re- solve to a string value. Due to memory constraints, a local string variable may NOT be returned from a function. Some examples are: return; return 1; return level; return (sum + 25); return "hello"; return (func() + 20); Notice that when a complex expression is returned it is usually sur- rounded by parentheses. This is done only for clarity and is not nec- essary. Also, it should be clear that what is returned is not the ex- pression but what it evaluates to. 3.4.7 The Break statement Often while using a looping statement (while, do...while, for), it is necessary to break out of (exit) the loop. The break statement serves this purpose. When the break statement is encountered, execution of the smallest while, do...while, or for loop is terminated, and execu- tion continues immediately after the terminated loop statement. It is an error for a break statement to appear outside of a loop. The format of the break statement is: break; For example, assuming you had the following code: Telix v3.15 - SALT Programming Program Structure 16 int num = 0; while (1) { num = num + 1; if (num > 100) break; } prints("Done"); Ordinarily, since there will always be a non-zero (TRUE) value in the conditional part of this while statement, it would execute forever. However, when the 'num' variable is > 100, the break statement is exe- cuted to exit from the loop, at which point the next statement would be executed (the function call to prints). 3.4.8 The Continue statement The continue statement is used within a loop (while, do...while, or for statement). The continue statement has the form: continue; It is illegal for a continue statement to appear outside of a loop body. When a continue statement is encountered, program control is im- mediately transferred to the end of the body of the innermost en- closing while, do...while, or for statement. The effect in a while or do...while statement is that the condition part of the while or do...while statement is evaluated, and the next iteration of the loop occurs. For example: num = 0; while (num < 100000) { num = num + 1; if (num > 100) continue; prints("Hello"); } The effect of the continue statement in the above loop would be that 'Hello' would only be printed while 'num' was smaller or equal to 100, as the continue statement is executed when num is bigger than 100, which causes the rest of the loop body to be skipped. An example for statement would be: for (num = 0; num < 100000; num = num + 1) { if (num > 100) continue; prints("Hello"); } The effect in this case would be the same. While 'num' is smaller or equal to 100, the entire loop body executes. If 'num' is greater than 100 however, the continue statement is executed. This causes the rest of the loop body to be skipped, so the 'Hello' is then not printed. Telix v3.15 - SALT Programming Program Structure 17 3.4.9 The Goto statement The goto statement is used to branch (jump) from one place to another, within a function. The use of goto statements is considered bad style. They can make code very hard to understand, and are in fact almost never necessary. For example, Telix is written mainly in the C lan- guage, which has a goto statement, yet except for a few pieces of pre- written code, the goto statement was never used nor needed. On the other hand, used very sparingly and properly, it can sometimes make some code clearer and perhaps faster. The goto statement consists of two parts, the 'label' or marker, which is where execution will jump to, and the actual goto itself. A label is defined in the form : where follows the same rules as for variable names. Note that a colon follows the name, not a semicolon. The colon character must immediately follow the label name, with no intervening spaces. A label does not have to be on a line by itself, and is not considered a statement by itself. The goto takes the form goto