Evaluating Arithmetic Expressions
Given the following values indicated how each expression would be evaluated?
Treat each expression separately, using the original values stated at the top and side.
intA = 2 intB = 4 intC = 3 intD = 11
|
Expression |
Result |
|
||
|
intA + intB ^ 2 |
Answer = 18 4 squared = 16 16 + 2 = 18 |
|
||
|
8 / intB / intA |
Answer = 1 8 / 4 = 2 2 / 1 = 1 |
|
||
|
intA * (intA + 1) |
Answer = 6 2 + 1 = 3 operation inside parenthesis occur first 2 * 3 = 6 |
|
||
|
intA * intA + 1 |
Answer = 5 2 * 2 = 4 4 + 1 = 5 |
|
||
|
intB - intA + intC * 2 |
Answer = 8 3 * 2 = 6 multiplication happens before subtraction or addition 4 – 2 = 2 2 + 6 = 8 |
|||
|
intD mod intB |
answer = 3 Modulus returns the remainder after division of the two operands. 4 goes into 11 2 times with a remainder of 3. |
intA 2 intB 4 intC 3 intD 11 |
||
|
intB - (intA + intC) * 2 |
Answer = -6 2 + 3 = 5 operation inside parenthesis occur first 5 * 2 = 10 multiplication happens before subtraction or addition 4 - 10 = -6 |
|
||
|
intD \ intA + intA |
Answer = 7 integer division (\) returns an integer and discards the remainder or decimal so 11 \ 2 = 5 5 + 2 = 7 |
|
||
|
(intB - intA) + intC * 2 |
Answer = 8 4 – 2 = 2 operation inside parenthesis occur first 3 * 2 = 6 multiplication happens before subtraction or addition 2 + 6 = 8 |
|
||
|
intB * intB \ intC |
answer = 5 4 * 4 = 16 16 \ 3 = 5 integer division (\) returns an integer and discards the remainder or decimal |
|
||
|
intC * 5 mod intA |
Answer = 1 multiplication has precedence over modulus (remainder) 15 mod 2 = 1 |
|
||
|
((intB - intA) + intC) * "2" |
Visual Basic will evaluate this to 10 However the statement is an INVALID statement, you must not do arithmetic with nonnumeric constants (anything in quotes) |
|
||
|
intD \ intC |
Answer = 3 integer division (\) returns an integer and discards the remainder or decimal. 3 goes into 11 three times. |
|
||
|
intD mod intA + intA |
Answer = 3 Modulus (which returns the remainder from division) is evaluated first. 11 mod 2 equal 1 1 + 2 = 3 |
|