PBASIC Programming/Basic Arithmetic
We can use some of the basic arithmetic operators for common operations. Operators can be used with variables, constants, or numbers.
- To add two numbers together, we use the "+" operator:
Var1 + Var2
- To subtract one number from the other, we use the "-" operator:
Var1 - Var2
- To multiply, we use the "*" operator:
Var * Var2
- To divide two numbers, we use the "/" operator:
Var1 / Var2
Other Math Functions
- We can use exponents with the "**" operator:
Var1 ** Var2
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.