Lesson+04+-+Using+Numeric+Variables

Lesson 4 by Felipe and Alvaro

=**ASSIGNMENT OPERATOR**= The assignment operator is the = sign. Know this! This is the right way to initialize data int i = 3; not int 3 = i; nor 3 = int i; =**MULTIPLE DECLARATIONS**= double a, bad, cat int ant= 3, eat= 4, fan= 7, pu; //* Be careful, the variable "pu" is only declared and is not initialized. Java will run this, but pu has an unknown value; (it actually has what was in that memory location previously!! That could be anything, which is why we always must remember to initialize)// =FUNDAMENTAL ARITHMETIC PROBLEMS = / * % + -

//Modulus is a tricky arithmetic operation, its basically calculates the remainder of a division problem. Remember those old days in your 3rd grade class, you do a division and if a math problem leaves out some numbers that cannot be divided by the divisor, then the number is what Java will print out. Like 5/2=2 with a remainder of 1. That remainder of 1 will print out if you want the Modulus to do its job.parenthesis// =ORDER OF OPERATIONS=

WORK ON TERMS (those elements separated by addition or subtraction) LEFT TO RIGHT.
Then apply PEMDAS //Parentheses// //exponents// //multiplication or division (which ever comes first left to right)// //addition or subtraction (which ever comes first left to right)// =INCREMENT AND DECREMENT= x++ (postfix increment operator) is the same as x = x + 1; Increments x AFTER it is used in the statement; ++x (prefix increment operator) is the same as x = x + 1, but it increments x BEFORE it is used in the statement; int x = 5; System.out.println(x++); ///* the x++ term evaluates to 5 and behind the scenes x increments to 6. Any future use of x will have the value of 6.*/// //System.out.prinln(x);// prints 6

int xb= 5; System.out.println(++xb); /* ++xb increments first, thus the term ++xb evaluates 6. Any future use of x will have the value of 6 */ //System.out.prinln(xb);// prints 6

x-- is the same as x = x - 1; Decrements x AFTER it is used in the statement; --x is the same as x = x - 1, but it decrements x AFTER it is used in the statement;

The --x or ++x should be done after parentheses but before multiplication, division, addition, subtraction. See Appendix H in book.

int j = 2; System.out.println(7%3 + j++ + (j – 2) ); //4// =IMPORTANT!= = Even though it says to do parens first in appendix H, evaluate the expressions left to right, solving each term by applying PEMDAS order of operations. = There are 3 terms in the problem -- System.out.println(7%3 + j++ + (j – 2) ); //1) Do first term with the modulus. First term evaluates to 1;// //2) Do second term with the postfix increment operator j++. Because it is postfix, the 2nd term is 2, but then changes to 3 behind the scenes for any future uses of j.// //3) Do third term, the parens. j is now 3 (from evaluation of 2nd term), thus (3-2) = 1, thus 3rd term is now 1// 1+ 3+1 = 4;

//Now if we change the order we get a different answer. In regular math, we would NOT get a different answer because addition is commutative (5 + 3 should equal 3 + 5, right?). However, because java works left to right, we have to see what happens to j along the way between each term.// int j = 2; System.out.println(7%3 + (j-2) + j++); //the increment operator is now at the end// //1) First term still evaluates to 1// //2) Second term evaluates to 0 because j is still 2 (we haven't hit the increment operator yet)// //3) Third term evaluates to 2, but then behind the scenes for future uses of j, it becomes 3.// 1 + 0 + 2 = 3 =**COMPOUND OPERATORS**= x = x + 3 is the same as x+=3; x = x - 5 is the same as x-=5;

Be careful with minus and division x = x - (4-y) is the same as x -= 4-y; x = x - 4 - y is the same as x-= 4+y; //see the difference from the previous one? The minus will be applied to everything on the right side so we need to change the sign in front of the y to get back to the original.// //r = r%2 is the same as r = r % 2;// modulus 2 will tell us if the number is even or odd. =INTEGER DIVISION= int + int => int; Makes sense //int - int => int;// Makes sense int * int => int; //Makes sense// //int / int => int;// Huh? What about possible fractional parts?

With integer division, the fractional part is dropped (Yes, Java doesn't like to lose data, but only for doubles. It figures you don't want the fractional part if none of the numbers in the division are doubles.) int m = 36; int j = 5; m = m / j; //new m is old m divided by j// //System.out.println(m);// //What’s printed?// 7

=NOT THE SAME AS ALGEBRA= x = x + 1; not allowed in algebra, but in computers means take the old value of x, add 1 to it, then store it back in x Order of operations works left to right -- we don't evaluate the 3rd term first just because it has parentheses. Work left to right on each term. Division of integers yields an integer