Learning the Language - V Fundamentals We now have locations to store things so we need a way to get things into those storage locations To do that, we use assignment statements Deja Moo: The feeling that you've heard this bull before. 2 An assignment statement reads Place what the right side of the expression evaluates to into the location on the left side of the expression The left side and right side are separated by an equals sign (=) Note that the right side is evaluated and then placed into the left side The left side is just a location For example the assignment statement X = 1. Would evaluate the right side A constant evaluates to itself Then since there is nothing else to evaluate it would store the result in the memory location labeled X 4 1
Fundamentals Since we have an evaluation, then a replacement, a statement like X = X + 1. Is completely proper and meaningful The computer looks at the right side first It gets the value stored in X Adds 1 to it Store the result in X If X had 21 originally stored in it and the expression X = X + 1 Was executed, after the execution of the expression, X would contain 22 Since X is a variable, it can change values during the execution of the program 5 6 If we had mistakenly tried 21 = X + 1 That would have generated an error because 21 is a constant and doesn t refer to a memory location Yes, mathematically it seems to be completely correct but the = sign is a replacement operator in this case not a logical condition 7 8 2
VBA doesn t look at the algebraic quality of the statement, just the syntax of the statement Remember that when encountered like this, the equal sign means store the evaluation of the right side in the location pointed to by the right side 9 10 When we evaluate an expression in VBA we need to know what order things are done in For example if we have the expression X = 2 + 4 / What would we expect to be stored in X after execution? Two possibilities X = 2 X =. X = 2 + 4 / Two possibilities X = 2 X =. The first evaluation comes from 2 + 4 = 6 and then 6 / = 2 The second evaluation comes from 4 / = 1. and then 1. + 2 =. 11 12
s Your calculator would probably give you the value of 2 because you would punch it in left to right as you read it VBA has a bit more sophistication For ARITHMETIC evaluations, VBA has a number of operators 1 14 All of the operators except two are just what you would expect them to be The two special operators require some explanation 15 16 4
When we use regular division on two integers and then store the result in an integer, what we get is a rounding off of the quotient into an integer Not all languages are like that but VB has chosen that as their standard For example if we have a function We have two integer arguments and the function returns an argument If we call the function with a=1 and b=2 the function returns 0 because the result of the division rounds off to 0 1 0.5 0 2 = 17 18 If we call the function with a= and b=4, the function evaluates to 1 because 0.75 rounds to 1 0.75 1 4 = The integer division operator will make the same division but rather than rounding, it will remove any decimal part of the result 0.75 0 4 = 19 20 5
If we write a function using the integer division operator we get a different result from the evaluation 0.75 0 4 = The Mod operator is used to divide two numbers and return only the remainder. The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result. For example, in the following expression, A (result) equals 5. A = 19 Mod 6.7 21 22 Again, illustrating with a function we have All of the operators beside the first take two arguments The uniary minus only take one argument 2 24 6
For example, the uniary minus would be used as X = -1.2 The order in which arithmetic expression are evaluated are 25 26 For operators of equal precedence, they are evaluated in left to right order For example, if we have the expression X = 1. + 2.. * 4. / 5. ^ 1. / 2. X = 1. + 2. -. * 4. / 5. ^ 1. / 2. 1 X = 1. + 2. -. * 4. / 5. / 2. 2 X = 1. + 2. - 12./ 5. / 2. X = 1. + 2. - 2.4 / 2. 4 X = 1. + 2. - 1.2 5 X =. - 1.2 6 X = 1.8 27 28 7
Remember, the order of operations is uniary negation, exponentiation, multiplication and division, integer division, modulo arithmetic, and then addition and subtraction For operators of equal precedence, the order is left to right Some examples for you to try Find the value for X after each of these expressions has executed X = -1./.^5/10 Mod 20. X = -1./.*5./10*20. X = -1./.*5.*10\20 X = 1./-.*-5.*10./-20. 29 0 We have a way to circumvent the normal arithmetic order of operations is we choose to do so Anything that we enclose in parentheses will be evaluated first This order goes from inner to outer X = 1. + ((2. +.) * 4. / 5.) ^ (1. / 2.) For Example 1 X = 1. + 2. -. * 4. / 5. ^ 1. / 2. 1 X = 1. + 2. -. * 4. / 5. / 2. 2 X = 1. + 2. - 12./ 5. / 2. X = 1. + 2. - 2.4 / 2. 4 X = 1. + 2. - 1.2 5 X =. - 1.2 6 X = 1.8 X = 1. + (5. * 4. / 5.) ^ (1. / 2.) 2 X = 1. + (20. / 5.) ^ (1. / 2.) X = 1. + 4. ^ (1. / 2.) 4 X = 1. + 4. ^ 0.5 5 X = 1. + 2. 6 X =. 1 2 8
Parentheses allow us to control the order of evaluation of an arithmetic expression Parentheses are evaluated from the inside out when they are nested For every open parenthesis ( there must be a closed parenthesis ) In VBA this looks like 4 The tall line after the equal sign is just where the cursor was The Range( A1 ).Value = X puts a copy of the value that is currently stored in the variable X into the Value property of the Range object So when we run the program we get 5 6 9
Now if we modify our program to check the two arithmetic expressions use used before we would have The # signs is VBA s way of showing that these are single constants and not integer constants After you type in 1. when you hit the end of the expression and hit return, VBA will replace 1. with 1# 7 8 We are going to put the evaluation of the first expression in cell A1 and the evaluation of the second expression in cell A2 On running the program 9 40 10