Increment and decrement
If a=10 so if we write ++a it gives 11 but by mathematical way it gives 20
9/11/2020 4:49:35 PM
Harsh Singh
3 Answers
New AnswerThe increment and decrement operators (++, --) increment and decrement numeric types by 1. The operator can either precede or follow the variable, depending on whether you want the variable to be updated 'before' or 'after' the expression is evaluated. • The prefix: increments the value of x, and then assigns it to y. int x = 5; int y = ++x; // x is 6, y is 6 • The postfix: assigns the value of x to y, and then increments x. int x = 5; int y = x++; // x is 6, y is 5 • https://www.sololearn.com/post/150404/?ref=app • https://code.sololearn.com/chPAcDs7YZgV/?ref=app • https://code.sololearn.com/cm4wSfJZ8h28/?ref=app
Hi! a++ stands for a = a + 1 a = 10 a++ --> 10 + 1 --> 11 To have the result of 20 you should have: a = 10 a += a --> a = a + a --> 20 https://www.programiz.com/article/increment-decrement-operator-difference-prefix-postfix https://en.m.wikipedia.org/wiki/Increment_and_decrement_operators