Alright. We'll discuss variables then. Now, there are two types:
VARIABLES AND CONSTANTS
Variables are shown like this:
VAR NAMEOFVARIABLE : TYPE
The var part you must have and then you put the name of the variable (can be anything, just don't start with numbers or weird characters). Then it is followed by a colon and the variable type:
INT: Integers. Any whole number -1, 34, 3343, -1234, 0
REAL: Any number: -1, 4.563, 932, 0.9465, -43.53
STRING: Characters like "A, BEGD, HELLO, HE#@8745"
BOOLEAN: True or False.
(Note: this is also explained in the boolean section I talked about before)
If you want the variable to have a value assigned to it, you can do that from the same line by saying:
VAR X : INT := 9
I chose X as my variable, it is an Integer and it has the value of 9. Therefore, X = 9.
If you want a variable to have a value, just write := (colon equals) and type in the value after it.
Now CONSTANTS are like variables. However, constants CANNOT change. The value given to a constant will always be the same. Constants are usually used in formulas (like the value of pi
CONST PI := 3.141592654
So instead of writing 3.14159... everytime, you can just say PI
So, you can use variables and contants just like you do numbers. You can add then together, subtract them, divide them, multiply them (if they are integers or real numbers).
So, let's say X = 9 and Y = 1
You can say this
PUT "VALUE OF X + Y is: ", x + y
So in that case, it will say "VALUE OF X + Y is 10" (Because of the values)
And in doing that, you can do any type of mathematics just by using the right operand (math symbol):
+ ADD
- SUBTRACT
* MULTIPLY (asterisk or SHIFT + 8)
/ DIVIDE (SHIFT + ?)
** SQUARE (Will square the value of your variable or constant)
A few examples (let x = 9 and y = 3)
CODE
X + Y WOULD EQUAL 12
X - Y WOULD EQUAL 6
X * Y WOULD EQUAL 27
X / Y WOULD EQUAL 3
X ** Y WOULD EQUAL 729
Now, as with numbers, variable values follow the BEDMAS rules. Every math part, goes in order.
B=Brackets/parenthesis
E=Exponents
D=Division
M=Multiplication
A=Addition
S=Subtraction
So, if you say this:
(X + Y) / 4
The computer will add X and Y first because of the brackets. Then, it will take that value and divide it by 4. So X + Y (If we keep the values the same, x = 9 and y = 3) it would look like this:
(9 + 3) / 4
So that's 12/4 which is 3, so that would be the answer
Now, with variables, it is important to know how to store values into them. You can take any variable (I'll make one called Z just as an example. We'll say Z = 0 for now). NOTE: It is always a good programming practice to set a variable with a default value (a value that it starts with). That way, if it is used in a formula or someplace else, it will not stop the execution of your program because it doesn't have a value.
.
So, if you want to store a value into a variable, you just need to do this
VAR X: INT:=9
VAR Y: INT:=3
VAR Z: INT:= 0
Z:= X + Y
Now, you see what I did? Z will be the value of X and Y added together (12). Whatever Z's value was before, forget about it, as soon as you make a statement like that, Z's value is gone, and it gains a new value (in this case, 12). Say you wanted something to add to the value of Z, you would need to do something like this
Z:= Z + 9
So, if we look at it like this, the value of Z will get 9 more added to it (20 because of 12+9).
Well, that's probably the basics of variables. If you have anymore questions, let us know
.
Archived topic from Iceteks, old topic ID:2825, old post ID:23398