Assignment operations alter computer memory even when the variable on the left of is also = involved in the expression to the right of For example, the variable is incremented by =. int j 1 with this assignment operation: j = j + 1; This type of update—incrementing a variable—is performed so frequently that Java offers operators with the express purpose of incrementing variables. The and operators increment ++ -- and decrement a variable by 1, respectively. For example, the expression adds to the value j++ 1 of and the expression reduces by 1. The and unary operators alter the numeric j, x-- x ++ -- variable that they follow (see the table below). Statement Value of j int j = 0; 0 j++; 1 j++; 2 j--; 1 So, within the context of the determinate loop, the update step can be written as rather than j++ This loop j = j + 1. for for (int j = 1; j <= n; j = j + 1) { // ... } may also be written with the operator for equivalent behavior: ++ for(int j = 1; j <= n; j++) { // ... } These new assignment operators are shown because they provide a convenient way to increment and decrement a counter in loops. Also, most Java programmers use the operator in for ++ for loops. You will see them often. Java has several assignment operators in addition to Two of them, and add and =. += -=, subtract value from the variable to the left, respectively. Operator Equivalent Meaning Increment variable on left by value on right. += Decrement variable on left by value on right. -= These two new operators alter the numeric variable that they follow. Statement Value of j int j = 0; 0 j += 3; 3 j += 4; 7 j -= 2; 5 Whereas the operators and increment and decrement the variable by one, the ++ -- operators and increment and decrement the variable by any amount. The operator += -= += is most often used to accumulate values inside a loop.
Assignment operations alter computer memory even when the variable on the left of = is also involved in the expression to the right of =. For example, the variable int j is incremented by 1 with this assignment operation: j = j + 1; This type of update—incrementing a variable—is performed so frequently that Java offers operators with the express purpose of incrementing variables. The ++ and -- operators increment and decrement a variable by 1, respectively. For example, the expression j++ adds 1 to the value of j, and the expression x-- reduces x by 1. The ++ and -- unary operators alter the numeric variable that they follow (see the table below). Statement Value of j int j = 0; 0 j++; 1 j++; 2 j--; 1 So, within the context of the determinate loop, the update step can be written as j++ rather than j = j + 1. This for loop for (int j = 1; j <= n; j = j + 1) { // ... } may also be written with the ++ operator for equivalent behavior: for(int j = 1; j <= n; j++) { // ... } These new assignment operators are shown because they provide a convenient way to increment and decrement a counter in for loops. Also, most Java programmers use the ++ operator in for loops. You will see them often. Java has several assignment operators in addition to =. Two of them, += and -=, add and subtract value from the variable to the left, respectively. Operator Equivalent Meaning += Increment variable on left by value on right. -= Decrement variable on left by value on right. These two new operators alter the numeric variable that they follow. Statement Value of j int j = 0; 0 j += 3; 3 j += 4; 7 j -= 2; 5 Whereas the operators ++ and -- increment and decrement the variable by one, the operators += and -= increment and decrement the variable by any amount. The += operator is most often used to accumulate values inside a loop.