This will all add up in the end

advertisement
This will all add up in the end
 Assignment operator
 = Simple Assignment operator
 Arithmetic Operators
 + Additive operator
 – Subtraction operator
 * Multiplication operator
 / Division operator
 % Remainder operator
int num;
num = 30 + 5;
//num now stores
num = 30 - 5;
//num now stores
num = 30 * 5;
//num now stores
num = 30 / 5;
//num now stores
the value of 35
the value of 25
the value of 150
the value of 6
 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
int num;
num = 8 % 5;
//num now stores
num = 21 % 5;
//num now stores
num = 49 % 5;
//num now stores
num = 70 % 5;
//num now stores
the value of 3
the value of 1
the value of 4
the value of 0
int num;
num = 10 + 50 – 2 * 6 / 3; //valid
10 + 50 – 2 * 6 / 3 = num; //invalid!
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;
 The Order of Operations is as follows:
Parenthesis
2. *, /, and % are evaluated from left to right
3. + and – are evaluated from left to right
1.
 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.
int num;
num = 10 + 4 * 3;
//num now stores the
num = (10 + 4) * 3;
//num now stores the
num = 10 * 5 % 3;
//num now stores the
num = 10 * (5 % 3);
//num now stores the
value of 22
value of 42
value of 2
value of 20
 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;
 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--;
 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.
 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