Operators & Expressions ![]()
CAPITAL Business Script is made up of three core elements:
Operators
These range from simple assignments (:=) to complex mathematical operators such as modulus (%). They can also be used for the combination of character strings, creating relations between data files, and comparison.
A
common mistake new users make is to mix the "=" (is equal to) operator with the ":="
(assign value) operator. In some languages the syntax of these operators are interchangeable. They are
not interchangeable in CAPITAL Business Script. You must always use the := operator to guarantee
correct assignment of values to variables.
Procedure
A procedure is CBS jargon for a unit or block of program code or "script". A block can consist of as little as one or two CBS statements, and as much as 64K worth of lines. Other languages commonly refer to procedures as "sub routines" or "methods".
Return
A procedure finishes when the Return command is reached or when the last command in the procedure is executed. When the Return command is encountered program control passes back to Visual Builder, or if a procedure was called from another procedure, to the upper level procedure that initiated the call. There may be any number of return commands in a procedure. The format of the command is:
RETURN <exp>
where the <exp> is the data item to be passed back. It is usually very important to include a RETURN statement in a procedure so that CAPITAL will know the result of the operation and take appropriate action. If a procedure does not include a RETURN statement then execution continues until the last executable statement is reached. An empty string ("") is usually returned as the default result if no result is specified. However, the return result may vary if not specified explicitly in the script. Therefore its always recommended to include a RETURN statement if the script extends over more than one line.
Arithmetic Operations
The symbols * , / , % , + , - , and ** are the arithmetic operators available in CBS. These symbols along with the brackets ( ) and the = and := assignment operators are used to perform arithmetic functions. Consider this algebraic expression:
Average : = (John + David + Pete) / 3
This adds the variables John, David and Pete together, divides the total by 3 and places the result in the variable Average. As CBS adheres to the order of precedence defined in mathematics, if the brackets were omitted CBS would divide Pete by 3 and then add it to John and David. This is not the desired result, hence the inclusion of the brackets. It is always recommended that brackets be included in calculations to eliminate ambiguity.
|
Operator |
Meaning |
|
* |
Multiply |
|
/ |
Divide |
|
% |
Modulus |
|
+ |
Add |
|
- |
Subtract |
|
** |
Exponential |
|
$ |
Within operator (string comparisons only) |
|
<> |
Not equal |
|
# |
Not equal |
Character Operators
Of the arithmetic operators mentioned above, the assignment and addition operators have uses in character expressions. To concatenate (join) two strings simply add them together as you would two numbers in a mathematical expression:
FullName : = 'John ' + 'Nugent'
This will result in the string "John Nugent" being placed in the variable FullName.
Date Operators
To create a date variable either declare the variable as a date type or use the CTOD() (character to date) function and pass it the date as a string. The TODAY() function also returns the system date as a date variable.
Once you have a date variable you can use + and - to add or subtract a number of days. You can also use - to subtract one date from another in which case the result is the number of days between the two dates.
This expression defines a date as the 1st of March 1999:
MyDate := CTOD('01/03/1999')
This statement converts the string "01/03/1999" into a date variable. Note the format which is:
DD-MM-YY. (2 digit day, 2 digit month, 2 digit year.)
Array Operators
As well as being variables, arrays are complex data structures. Each element of an array can be of any data type so the operations that can be performed on an array element are the operations which are valid for any variable of that type. There are several operations that can be performed on an array that are concerned with the creation and sizing of the array.
An array can be created by either declaring a variable as an array or assigning {} brackets to a variable that has already been declared. For example:
Array1 : = {}
You can also assign data elements to the array like so:
Array2 : = {3, 2, 1}
Array2 contains three numeric elements into which the values 1, 2, and 3 are placed. To access the third element of array Array2 use:
MyVar := Array2[3]
In the above example MyVar will be assigned the number 1, which is the third element of the array. In CBS, arrays start from element 1, not zero as in some other programming languages.
Comparison Operators
These are used to compare the contents of variables and the results of functions and expressions.
|
Operator |
Description |
|
# |
Not equal (mathematical evaluation) |
|
<> |
Not equal (mathematical evaluation) |
|
! |
Not equal (logical evaluation) |
|
$ |
Within (character strings only) |
|
< |
Less than |
|
> |
Greater than |
|
<= |
Less than or equal |
|
>= |
Greater than or equal |
|
= |
Equal (numeric) or match (string) |
|
== |
Equal to or exact string match |
Example statements include:
If MyValue <> 50
* Does not equal 50
Endif
If ! lLogical
* If lLogical = FALSE
Endif
If "PETER" $ "PETER BOWER"
* The string "PETER" is within the string "PETER BOWER"
Endif
If "PETER BOWER" = "PETER"
* True because all the characters of the
* first string match the characters of
* the second string
Endif
If "PETER BOWER" == "PETER"
* FALSE because the character strings
* are not an exact match
Endif
Logical Operators
Comparison operators can be combined together using the logical operators .AND. .OR. and .NOT. to check multiple conditions and ranges at once. For example, this statement checks if the variable MyValue is greater than 100 but less than 200:
If MyValue > 100 .AND. MyValue < 200
* Do something if between 101 and 199
Endif
This statement checks if MyValue is either 1 or 2
If MyValue = 1 .OR. MyValue = 2
* Equals 1 or 2
Endif
Special Punctuation
|
Symbol |
Meaning |
|
* |
Comment line |
|
; |
Wrap line |
Place a * (asterisk) in front of a line to have CBS ignore processing of that line.
Place a ; (semi colon) at the end of a line to let CBS treat the next line as a continuation of the existing line.
____________________________
Related Topics: