QMBasic - Expressions and Operators |
![]() ![]() ![]() |
A QMBasic expression consists of one or more data items (constants or variables) linked by operators.
In all cases above, var may be a matrix reference, for example
var(r,c)[s,n]
where r and c are expressions which evaluate to the desired matrix index values.
There is also a special conditional item of the form
IF conditional.expr THEN expr.1 ELSE expr.2
where conditional.expression is evaluated to determine whether the overall value is that of expr.1 or expr.2.
The boolean (true/false) values used by QMBasic are that any value other than zero or a null string is treated as true, zero or a null string being treated as false. An expression returning a boolean value returns the integer value 1 for true, zero for false. The boolean values are available as @TRUE and @FALSE for use in programs.
The substring extraction operation x[s,n] extracts n characters starting at character s of the string x. Character positions are numbered from one. Thus A = "abcdefghijkl" Z = A[5,3] sets Z to the string "efg".
If the bounds of the substring extend beyond the end of the string from which it is to be extracted, the result is truncated. Trailing spaces are not added to make up the shortfall. A start position of less than one is treated as one.
The trailing substring extraction operation x[n] extracts the last n characters of the string x. Thus A = "abcdefghijkl" Z = A[3] sets Z to the string "jkl".
If the length of the substring to be extracted is greater than the length of the source string, the entire source string is returned.
The field extraction operator x<f,v,s> extracts field f, value v, subvalue s from the source string x. If s is omitted or zero, field f, value v is extracted. If v is omitted or zero, field f is extracted. Thus
The operators of QMBasic are set out in the table below. The numbers in the right hand column are the operator precedence, the lower valued operators taking precedence in execution. Operations of equal precedence are processed left to right with the exception of the exponentiation operator which is processed right to left. Round brackets may be used to alter the order of execution or to improve readability of complex expressions.
The following alternative logical and relational operator formats may be used
The relational operators are defined such that, if the two items to be compared can both be treated as numbers, a simple numeric comparison is performed. If one or both items cannot be treated as numbers, they are compared as left aligned character strings. The COMPARE() function can be used to force a string comparison.
Note: The language syntax includes an ambiguity with the use of the < and > characters as both relational operators and in dynamic array references. For example, the statement A = B<C> + 0 could be extracting field C from dynamic array B and adding zero to it (to force it to be stored as a numeric value) or it could be testing whether B is less than C and the result of this comparison is greater than zero. In cases such as this, the compiler looks at the overall structure of the statement and takes the most appropriate view. Use of brackets when mixing relational operators with field references will always avoid possible misinterpretation.
The MATCHES operator matches a string against a pattern consisting of one or more concatenated items from the following list.
The values n and m are integers with any number of digits. m must be greater than or equal to n.
The 0A, nA, 0N, nN and "string" patterns may be preceded by a tilde (~) to invert the match condition. For example, ~4N matches four non-numeric characters such as ABCD (not a string which is not four numeric characters such as 12C4).
A null string matches patterns ..., 0A, 0X, 0N, their inverses (~0A, etc) and "".
The 0X and n-mX patterns match against as few characters as necessary before control passes to the next pattern. For example, the string ABC123DEF matched against the pattern 0X2N0X matches the pattern components as ABC, 12 and 3DEF.
The 0N, n-mN, 0A, and n-mA patterns match against as many characters as possible. For example, the string ABC123DEF matched against the pattern 0X2-3N0X matches the pattern components as ABC, 123 and DEF.
The pattern string may contain alternative templates separated by value marks. The MATCHES operator tries each template in turn until one is a successful match against the string.
As an example of the MATCHES operator, the statement IF S MATCHES "1N0N'-'1N0N" THEN PRINT "OK" would print OK when S contains one or more digits followed by a hyphen and one or more digits. Note the use of 1N0N to ensure that at least one digit is present.
See also: |