What is an expression?
Anything that computes to a different value:
This = That
Expression 1 Expression 2
An expression can contain different types of mathematical operators - in Java the different operators you can use include:
+ addition
- subtraction
* multiplication
/ division
% modulus or remainder
() parentheses
These operators will be dependent on what types of numbers are used, for example if you use only integer-type number variables, then the answer to 7/2 would be 3 (the full answer will not round but be cutoff to the whole number value) if you used floating type number variables the answer would be the full 3.5
The % can be used to obtain the remainder in this case 7/2 = 3 remainder 1 so a 7%2 would return the result of 1
The % can be useful for determining if a value is even or odd or to test if one integer is evenly divisible by another or not.
If a floating point type number (3.5) is combined in an expression with an integer type number the result will be a floating type number.
( ) parentheses are used as in standard math formulas to determine the order of operations in an expression
Expression Shortcuts
You can combine a few terms together when you want to update a variable to a new value that includes an operator on itself, for example:
cost = cost +1000; OR cost += 1000;
You can quickly increment a variable by or decrease a variable by one:
minutes = minutes + 1 OR ++minutes OR minutes++
minutes = minutes - 1 OR --minutes OR minutes--
No comments:
Post a Comment