411 REFERENCE · Wikibooks
PBASIC Programming/Basic Arithmetic
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Arithmetic Operators
We can use some of the basic arithmetic operators for common operations. Operators can be used with variables, constants, or numbers.
Common Operators
AdditionTo add two numbers together, we use the "+" operator:
Var1 + Var2
SubtractionTo subtract one number from the other, we use the "-" operator:
Var1 - Var2
MultiplicationTo multiply, we use the "*" operator:
Var * Var2
DivisionTo divide two numbers, we use the "/" operator:
Var1 / Var2
Other Math Functions
Multiply High "**" returns upper 16 bits of 32 bit result:
Var1 ** Var2
Assignment
We can use the "=" operator to assign a value to a variable. For instance, we can write:
MyVar VAR Byte
MyVar = 10
MyVar VAR Byte
MyVar = 10 + 5
MyVar VAR Byte
MyVar2 VAR Byte
MyVar = 10
MyVar2 = MyVar + 10
We can also write a self-assignment, or an assignment where a variable appears on the left and the right side of the statement:
MyVar VAR Byte
MyVar = 10
MyVar = MyVar + 1
On the third line of this program, MyVar equals 10, and then we add one and store the new result into MyVar again. After this program runs, MyVar contains the value of 11.
Overflow and Underflow
Overflow occurs when we attempt to store a number in a variable that is too large for that variable. For instance, if we try to store a number that is larger then 255 in a Byte, or a number that is larger then 65535 in a word. Underflow is the exact opposite problem, where we attempt to create a negative number that is so low that the variable cannot hold it.