Operators and mathematic expressions |
Top Previous Next |
An operator is a symbol that performs a specific function on a variable, constant or identifier. Operators consist of mathematic operators, conditional operators, Boolean operators and control operators. Some symbols are reused for other purposes depending on the context of the operator. For example the & symbol is used as the bit wise AND operator and also as the Address Of operator Mathematic operators The EBASIC compiler understands the following table of mathematic operators.
Every mathematic operator requires a left hand and right hand operand with the exception of the unary minus operator which expects only a right hand operand. Operations may be ordered with parenthesis. Compound operators and postfix operators Compound operators, also called assignment operators, use one mathematic operator in conjunction with an assignment to create a shortcut to common expressions. For example it is very common in programming to add a number to a variable and assign that number to itself as in A=A+5. The compound operators shorten this by only requiring the variable name once, so to add 5 to a variable you can specify A += 5. The following table lists the compound operators:
Another common expression in programming is to increment or decrement a variable by 1 as in A=A+1. Emergence BASIC supports two postfix operators namely ++ and -- that perform this task easily. Examples: A++ : REMAdd one to A
Conditional operators Conditional operators are used to compare variables or constants and return either TRUE (1) or FALSE (0) as a result. Conditional operators are normally used by conditional statements but also can be used in conjunction with mathematic operators. The following table lists the conditional operators understood by the compiler
Example usages: IF A < 5 THEN PRINT "A < 5" Boolean operators EBASIC supports two Boolean operators. AND and OR. The operators return either TRUE or FALSE and are generally used by conditional statements to group two or more conditional tests together. AND and OR are blocking operators which means if the first conditional test is FALSE for AND, TRUE for OR, then the second conditional test will not be executed. Examples of blocking: A=0 : Z=1 Blocking is useful when performing the second test would be undesirable if the first test fails. A common usage is to test a pointer for NULL before de-referencing it. Control operators Control operators either cause code to be executed or return information about a function or identifier. The following control operators are supported by the EBASIC compiler:
Control operators will be described in detail elsewhere in the help document. Operator usage with STRING variables EBASIC allows conditional comparison of strings. When comparing strings the allowed operators are: = Test for equality <> Test for inequality < Test if one string is less than another alphabetically > Test if one string is greater than another alphabetically <= Test for less than or equal to >= Test for greater than or equal to The less than and greater than variants check each character position until a determination can be made. The first character always has greatest significance in the test. String concatenation operator The + operator can be used with strings to combine, or append them, together. The result of the operation is the combination of all the strings in the expression. A$ = "This " + "is " + "a " + "String!" See Also: APPEND$
Operator Precedence Operator precedence defines the order of execution of operators in an expression. The precedence can be overridden by using parenthesis to order the execution. Precedence at the same level is from left to right. From highest precedence to lowest: ( ) - Unary Minus ^, #, ## /, *, %,|| +, - << >> <, >, <=, >=, <>, = &, |, AND, OR Examples:
A = 1 + 2 * 4 Performs the multiplication first, followed by the addition with a result of 9 A = (1+2) * 4 Performs the addition first followed by the multiplication with a result of 12 A = 1 + 2 * 4 - 5 Multiplication is first followed by the addition and then the subtraction with a result of 4 Type Promotion For all mathematical operations the resultant type will be the same as the operand with the highest precision. Operands can be given a different type for use in the calculation with the INT or FLT functions. For example a common user programming error is dividing two integers and assuming the result will be a floating point value: DEF b,c as INT Will result in a value of 0 because both operands are integer. However if one of the operands is a floating point type the result will be 0.5. This can be accomplished by using either a type modifier or the FLT function. A = b / FLT(c) In this case c is temporarily promoted to a floating point value and the result will then be a floating point value. |