Page 110 - Open Soource Technologies 304.indd
P. 110
Event Driven Programming
5.2 Operators-Math Operator
3.5.1 Using Addition and Subtraction Operators
The two simplest math operations are addition and subtraction. If you have ever used a calculator,
you already have a good idea how these operations are performed in a line of computer code.
A computer program, however, gives you greater flexibility than a calculator in the operations
you can perform. Your programs aren’t limited to working with literal numbers (for example.
1, 15, 37, 63, and -105.2). Your program can add or subtract two or more literal numbers, numeric
variables, or any functions that return a numeric value. Also, like a calculator, you can perform
per form the operations in you program.
The operator for addition in visual Basic is the plus sign (+). The general use of this operator
is as follows:
Result=iNumber One + iNuber Two + iNuberThree
Result is a variable (or control property) that contains the sum of the three numbers to the right
of the equal sign (=). The equal sign indicates the assignment of a value to the variable.
iNumberone, iNumberTwo, and iNumberThree are numeric variables. As just described, you can
also add literal numbers or return values rather than numeric variables from a function. You can
add as many numbers together as you like, but each number pair must be separated by a plus
sign.
The operator for subtraction is the minus sing (-). The syntax is basically the same as for addition:
Result=NumberOne-NumberTwo-NumberThree
Although the order doesn’t matter in addition, in subtraction the number to the right of the
minus sign is subtracted from the number to the left of the sign. If you have multiple numbers,
the second number is subtracted from the first, the third number is subtracted from that result,
and so on, moving from left to right. For example, consider the following equation:
Result=15-603
The computer first subtracts 6 from 15 to yield 9. It then subtracts 3 from 9 to yield 6, which is
the final answer stored in the variable Result. This left-to-right processing is used in the evaluation
of expressions whenever similar operations are being performed.
If you really want to have 3 subtracted from 6 before it’s subtracted from 15 for a Result of 12,
you have to change the operator or force the program to evaluate the expression in the order
you intended. (This order of execution is covered later in the section “Setting the Order of
Precedence in Statements.”) For now, consider the following statements and their resulting
values:
1. Result = 15"-”3"-”6 ‘results in 6
2. Result = 15"-”6+3 ‘results in 12
3. Result = 3-6+15 ‘results in 12
After having studied addition and subtraction operators, we will take a look at the multiplication,
division and exponent operators in this article.
104 LOVELY PROFESSIONAL UNIVERSITY