PBASIC Programming/Basic Arithmetic

From Wikibooks, open books for an open world
< PBASIC Programming
Jump to: navigation, search

Arithmetic Operators[edit]

We can use some of the basic arithmetic operators for common operations. Operators can be used with variables, constants, or numbers.

Common Operators[edit]

Addition
To add two numbers together, we use the "+" operator:
Var1 + Var2
Subtraction
To subtract one number from the other, we use the "-" operator:
Var1 - Var2
Multiplication
To multiply, we use the "*" operator:
Var * Var2
Division
To divide two numbers, we use the "/" operator:
Var1 / Var2

Other Math Functions[edit]

Power
We can use exponents with the "**" operator:
Var1 ** Var2

Assignment[edit]

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[edit]

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.