Presentation is loading. Please wait.

Presentation is loading. Please wait.

This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.

Similar presentations


Presentation on theme: "This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication."— Presentation transcript:

1 This will all add up in the end

2 Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication operator / Division operator % Remainder operator

3 int num; num = 30 + 5; //num now stores the value of 35 num = 30 - 5; //num now stores the value of 25 num = 30 * 5; //num now stores the value of 150 num = 30 / 5; //num now stores the value of 6

4 The other operators (+-*/) you have probably seen before. But you might not have seen the mod operator which is represented by the percent sign: % The mod operator is also refered to as the remainder operator since that is what it returns. In the following expression it returns the remainder when a is divided by b: a % b

5 int num; num = 8 % 5; //num now stores the value of 3 num = 21 % 5; //num now stores the value of 1 num = 49 % 5; //num now stores the value of 4 num = 70 % 5; //num now stores the value of 0

6 int num; num = 10 + 50 – 2 * 6 / 3; //valid 10 + 50 – 2 * 6 / 3 = num; //invalid!

7 The mistake on the previous slide will cause a compilation error. But you have to be careful when you are assigning variables to one another as that will not. For example, given the following variables: int num1 = 44; int num2 = 13; The following line of code will result in both variables storing the value of 13 num1 = num2; The following line of code will result in both variables storing the value of 44 num2 = num1;

8 The Order of Operations is as follows: 1. Parenthesis 2. *, /, and % are evaluated from left to right 3. + and – are evaluated from left to right You will want to always use parenthesis!!! Even if you are certain that the expression will be evaluated in the right order, they make your code easier to read.

9 int num; num = 10 + 4 * 3; //num now stores the value of 22 num = (10 + 4) * 3; //num now stores the value of 42 num = 10 * 5 % 3; //num now stores the value of 2 num = 10 * (5 % 3); //num now stores the value of 20

10 The following expression adds 3 to what is stored in num and stores the new value back in num: num = num + 3; But you can use a short cut by using += like so: num += 3; The same can be done for other expressions: num = num - 3; same as num -= 3; num = num * 3; same as num *= 3; num = num / 3; same as num /= 3;

11 Adding or subtracting one is very common so there are unary operators that will do that for us like ++ and --. The following 3 expressions all do the same thing: num = num + 1; num += 1; num++; The following three expression do the same thing as well: num = num - 1; num -= 1; num--;

12 Something to be weary of is that the division of integers does not work the way you would expect. Given the following code: int a = 9, b = 2; double c = a / b; System.out.println(c); Would result in the following being printed out: 4.0 This is because the result of 4.5 is truncated and the decimal value is cut off.

13 To get around this we can cast one of the integer numbers into a double in the following way: int a = 9, b = 2; double c = (double)a / b; System.out.println(c);


Download ppt "This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication."

Similar presentations


Ads by Google