Arithmetic Expressions 3+4x10(y-5)a+b+c) 49+x +9(-+ 5 X is translated to (3+4*x)5-10*(y-5)(a+b+c)x+9*(4/x+(9+x)y) Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 26 Arithmetic Expressions ) 4 9 9( 10( 5)( ) 5 3 4 y x x x x y a b c + + + − + + − + is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
Shortcut Assignment Operators Operator Example equivalent i+=8 i=1+8 f-=8.0f=f-8.0 大二 i大=8 大 i/=8 i=i/8 8 i=i88 Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 27 Shortcut Assignment Operators Operator Example Equivalent += i+=8 i = i+8 -= f-=8.0 f = f-8.0 *= i*=8 i = i*8 /= i/=8 i = i/8 %= i%=8 i = i%8
Increment and Decrement Operators Operator Name Description ttvar preincrement The expression(++varincrements yar by 1 and evaluates to the new value in var after the increment var++ postincrement The expression(var++)evaluates to the originalvalue in yar and increments yar by 1 -var predecrement The expression (var)decrements yar by 1 and evaluates to the new value in yar after the decrement. var- postdecrement The expression(var--)evaluates to the original value in yar and decrements yar by 1 Liang, Introduction to Java Programming, revised by dai-kaiyu
Liang, Introduction to Java Programming,revised by Dai-kaiyu 28 Increment and Decrement Operators Operator Name Description ++var preincrement The expression (++var) increments var by 1 and evaluates to the new value in var afterthe increment. var++ postincrement The expression (var++) evaluates to the original value in var and increments var by 1. --var predecrement The expression (--var) decrements var by 1 and evaluates to the new value in var afterthe decrement. var-- postdecrement The expression (var--) evaluates to the original value in var and decrements var by 1
Increment and Decrement Operators, cont int i =10; Same effect as int newNum =10 i++ d int newNum=10 i i=i+1 int i =10: Same effect as int newNum =10 *(++i)i int newNum =10 i Liang, Introduction to Java Programming, revised by dai-kaiyu
Liang, Introduction to Java Programming,revised by Dai-kaiyu 29 Increment and Decrement Operators, cont. int i = 10; int newNum = 10 * i++; int newNum = 10 * i; i = i + 1; Same effect as int i = 10; int newNum = 10 * (++i); i = i + 1; int newNum = 10 * i; Same effect as
补充了解 Assignment Expressions and assignment Statements Prior to Java 2, all the expressions can be used as statements. Since Java 2, only the following types of expressions can be statements variable op= expression; //Where op is + ,* or ++variable variable++ variable variable Liang, Introduction to Java Programming, revised by dai-kaiyt
Liang, Introduction to Java Programming,revised by Dai-kaiyu 30 Assignment Expressions and Assignment Statements Prior to Java 2, all the expressions can be used as statements. Since Java 2, only the following types of expressions can be statements: variable op= expression; // Where op is +, -, *, /, or % ++variable; variable++; --variable; variable--; 补充了解