Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

difference between ++ and +=

OpenStudy (woodrow73):

In java, there is something called the increment/decrement operators. ++ is an increment operator and -- is a decrement operator. What they do is simple- they add one or subtract one to the variable you attach it to. for instance: ``` int x = 8; x++; System.out.println(x); ``` the above will print out 9, and if you used the -- instead of increment, it would print out 7. It is important to note that there is a subtle but important difference between using the increment/decrement operator before / after the variable. In general it's good practice to put the operator before the variable.. this is because of the order of operations.. if you use the increment/decrement in a statement, using the operator before the variable will give you the desired result (++x) for instance: ``` int x = 1; System.out.println(++x); System.out.print(x); ``` will print out 2 on the first line, and 2 on the second line like you'd expect, however if you use them after the variable, it will actually change x after the statement prints, for example: ``` int x = 1; System.out.println(x++); System.out.print(x); ``` The above will print out 1 on the first line, and 2 on the second line. So it is good practice to use the operators before the variable.

OpenStudy (anonymous):

thank you

OpenStudy (anonymous):

can u tell difference b/w ++ and+= in c also

OpenStudy (woodrow73):

+= is a combined assignment operator... so is *= and /= and %= and -= A combined assignment operator doesn't perform a special function, though it is a command to condense code. The combined assignment operator will take the variable left of the equals sign, and place it on the right side, followed by the operator you include in the assignment operator after, and then parenthesis around anything that is right of the equals sign.. for example: ``` int x = 1; x += 5; System.out.println(x + " this will print out 6 for x."); System.out.println("x += 5 is the same as saying x = x + 5"); ``` to show a more complex example: ``` int x = 10; x = 11; x += 5; x /= 4; x *= 16; x *= 25-24; x -= 5; x %= 2*2+6; if(x == 9) System.out.println(x + " is equal to 9."); else System.out.println("this will never print out, because x results in 9."); ``` pemdas will operate in java as you expect it to - like 2*2+6 is the same as (2*2)+6 with the combined assignment operator, x %= 2*2+6 can be written out as x = x%(2*2+6);

OpenStudy (woodrow73):

note that my elaboration of the syntax and pemdas is for java, may not be the same for c language

OpenStudy (woodrow73):

though I bet the general concept is the same

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!