cubloc:operators:index

When evaluating expressions in a programming language, it is important to understand the order of operations, also know as operator precedence. The following table describes Cubloc BASIC's operator precedence.

Operator | Explanation | Type | Precendence |
---|---|---|---|

^ | To the power of | Math | First |

*,/,MOD | Multiply, Divide, MOD | Math | |

+,- | Add, Subtract | Math | |

«, » | Left Shift, Right Shift | Logic | |

<, >, ⇐, >= | Less than, Larger than, Less or Equal to , Larger or Equal to. | Comparison | |

=, <> | Same, Different | Comparison | |

AND, XOR, OR,NOT | Boolean AND, XOR, OR, & NOT | Logic | Last |

Within each row in the table above, the operators are evaluated from left to right. Operators can be used in conditional statement as show below.

IF A+1 = 10 THEN GOTO ABC

Use parenthesis to explicitly control the order of operations in an expression.

When multiple operators are used, the expression will be evaluated in the following order:

1) Operator(s) inside parenthesis 2) Negative Sign (–) 3) Exponent (^) 4) Multiplication, Division, Remainder (*, /, MOD) 5) Addition/Subtraction (+,-) 6) Bit-wise Left Shift, Bit-wise Right Shift («, »)

Operators used in Cubloc BASIC may differ slightly from common mathematical operators. Please refer to the table below:

Operator | Math | Basic | Example |
---|---|---|---|

Add | + | + | 3+4+5, 6+A |

Subtract | - | - | 10-3, 63-B |

Multiply | X | * | 2 * 4, A * 5 |

Division | / | 1234/3, 3843/A | |

To the power of | 5^{3} | ^ | 5^3, A^2 |

MOD | Remainder of | mod | 102 mod 3 |

When numbers of different types are mixed in an expression, the final result is cast to the type of the assigned variable.

Dim F1 As Single Dim A As Long F1 = 1.1234 A = F1 * 3.14 ' A gets 3 even though result is 3.525456.

Please be sure to include a decimal point(.) when using floating point numbers even if your computer's language setting uses a different character for its decimal separator.

F1 = 3.0/4.0 ' Write 3/4 as 3.0/4.0 for floating values F1 = 200.0 + Floor(A) * 12.0 + SQR(B) '200 as 200.0, 12 as 12.0…

The And, Xor, and Or operators are used for both logical and bit-wise operations.

If A=1 And B=1 Then C=1 ' if A=1 and B=1 …(Logical Operation) If A=1 Or B=1 Then C=1 ' if A=1 or B=1…(Logical Operation) A = B And &HF 'Set the upper 4 bits to zero. (Bit-wise Operation) A = B Xor &HF 'Invert the lower 4 bits. (Bit-wise Operation) A = B Or &HF 'Set the lower 4 bits to 1. (Bit-wise Operation).

**String** can be compared with the = operator.

Dim ST1 AS String * 12 Dim ST2 AS String * 12 ST1 = "COMFILE" ST2 = "CUBLOC" If ST1=ST2 Then ST2 = "OK" ' Check if ST1 is same as ST2.

When comparing Strings, Cubloc BASIC compares the ASCII value of each character in the String. Therefore, String comparisons are casesensitive; “COMFILE” does not equal “comfile”.

cubloc/operators/index.txt · Last modified: 2016/04/14 11:07 (external edit)