Operations
Now that we can declare variables and assign them values, we want to be able to perform calculations with them.
Mathematical operations
These operations deal with numerical data (ints and doubles). Here are the mathematical operators that you can use:
+: addition
-: subtraction
*: multiplication
/: division
%: modulus (returns the remainder of dividing one whole number by another)Here are some examples:
//gives num the value 6
int num = 2*3;
//gives x the value 3.3
double x = 6.6/2.0
//this is integer division, so we drop the decimal portion
//gives result the value 2
int result = 7/3;
//7/3 is 2 remainder 1
//gives mod the value 1
int mod = 7%3;We can also use multiple mathematical operators at once, and we can involve variables in the expressions.
Here are some more examples:
int x = 3;
double y = 4.4;
int z = 10;
//gives result1 the value 29
int result1 = x*z – z%x;
//gives result1 the value 26
result1 = (int)(z/y + 73/x);Notice that these examples are NOT the same as equations in algebra. In an equation, the expression on the left-hand side equals the expression on the right-hand side. In these examples, we are assigning a variable (the left-hand side) to have the VALUE from the expression on the right-hand side.
We can also do something like this:
int num = 1;
num = num + 1;This updates the value of num to be one bigger than the old value of num. Now num has the
value 2.
We CANNNOT do things like this:
//Illegal!
num + 1 = num;
//Illegal!
5 = num;The left-hand side must be a single variable, and we are updating that variable’s value to be the result of the right-hand side.
Expressions
An expression is a computation of variables and constant values (literals) using mathematical or other operators. When we evaluate an expression, we get a value as a result. For example, variable assignments might look like:
variable = expression;For example:
int x = 7*(3-2)+1;Here, 7*(3-2)+1 is an expression. We evaluate it first, and then the result is stored in the x
variable.
Shortcuts
There are several simple operations that you will find yourself doing over and over, such as adding one to a variable or multiplying a variable by a single value.
For example:
int num = 6;
//gives num the value 8
num += 2;This statement adds 2 to num, so that num now has the value 8. It is equivalent to the statement:
num = num + 2;We can also do something similar with -=, *=, /=, and %=. Here are some examples:
int x = 1;
//gives num the value 7
num -= x;
//gives num the value 35
num *= 5;
//gives num the value 11
num /= 3;
//gives num the value 1
num %= 2;There are other shortcuts we can use if we want to add one to a variable or subtract one from a variable:
int val = 4;
//adds one to val, now val is 5
val++;
//subtracts one from val, now val is 4
val--;The statement val++ is equivalent to:
val += 1;It is also equivalent to:
val = val + 1;A similar comparison is true for the statement val--.
Conditional operations
There is another group of operators we can apply to boolean variables (variables that can be either true or false). Here is a list of these conditional operators:
| Operator | What it does |
|---|---|
| == | checks whether two values are equal |
| != | checks whether two values are not equal |
| < | checks if the first value is less than the second value |
| > | checks if the first value is greater than the second value |
| <= | checks if the first value is less than or equal to the second value |
| >= | checks if the first value is greater than or equal to the second value |
| && | checks if both variables or expressions are true |
| ! | switches a variable or expression from false to true, or from true to false |
Here are some examples:
boolean a = true;
boolean b = false;
int x = 4;
int y = 3;
int z = 3;
//result is set to false (x does not equal y)
boolean result = (x == y);
//result is set to false (y does equal z)
result = (y != z);
//result is set to false (x is not less than z)
result = (x < z);
//result is set to false (z is less than x, but a does not equal b)
result = (z < x) && (a == b);
//result is set to true (z is less than x – it doesn't matter whether a equals b)
result = (z < x) || (a == b);
//result is set to true (!b is true, and a is true)
result = !b && a;