From EcmaScript standard:
11.4.4 Prefix Increment Operator
The production UnaryExpression : ++ UnaryExpression is evaluated as follows:
- Let expr be the result of evaluating UnaryExpression.
- Throw a SyntaxError exception if the following conditions are all true: �
- Type(expr) is Reference is true
- IsStrictReference(expr) is true
- Type(GetBase(expr)) is Environment Record
- GetReferencedName(expr) is either "eval" or "arguments"
- Let oldValue be ToNumber(GetValue(expr)).
- Let newValue be the result of adding the value 1 to oldValue, using the same rules as for the + operator (see 11.6.3).
- Call PutValue(expr, newValue).
- Return newValue.
and
11.13.2 Compound Assignment ( op= )
The production AssignmentExpression : LeftHandSideExpression AssignmentOperator AssignmentExpression, where AssignmentOperator is @= and @ represents one of the operators indicated above, is evaluated as follows:
- Let lref be the result of evaluating LeftHandSideExpression.
- Let lval be GetValue(lref).
- Let rref be the result of evaluating AssignmentExpression.
- Let rval be GetValue(rref).
- Let r be the result of applying operator @ to lval and rval.
- Throw a SyntaxError exception if the following conditions are all true:
- Type(lref) is Reference is true
- IsStrictReference(lref) is true
- Type(GetBase(lref)) is Environment Record
- GetReferencedName(lref) is either "eval" or "arguments"
- Call PutValue(lref, r)
Thus, var i = 0; i += ++i is:
i = 0; lvalue = value(i), which is 0; rvalue = value(++i), which is: increment i, then value of i (1); thus, rvalue = 1; i = lvalue (0) + rvalue (1), which is 1.Completely according to spec.
However, in C++, this is specifically defined to be undefined behaviour, thus on a different compiler you might also get 1. Or 99. Or it could set your computer on fire. All of those would be standard-compliant compilers. Thus, most people will recommend you only use pre/post-incremented variable once in a statement.
Dalam materi-materi perluangan simbol ++ sering kita temukan.
Simbol tersebut termasuk dalam operator aritmatika dan penugasan untuk meningkatkan atau menambah satu (+1) sebuah variabel.
Kadang ditulis seperti ini i++ dan juga seperti ini ++i.
Apa sih perbedaanya?
Post-increment dan Pre-increment
Penulisan simbol ++ di dibelakang vairbel disebut dengan post-incement, sedangkan bila ditulis di depan, disebut dengan pre-increment.
Contoh:
Post-increment:
for(int i = 1; i <= 10; i++){ System.out.print(i); }
Pre-increment:
for(int i = 1; i <= 10; ++i){ System.out.print(i); }
Kedua perulangan tersebut akan menghasilkan bilangan dari 1 sampai 10.
Keduanya menghasilkan output yang sama, lalu letak perbedaanya di mana?
Perbedaan i++ dengan ++i
Simbol ++ sebenarnya bentuk singkatan dari sebuah ekspersi:
- i++ merupakan singkatan dari i += 1 atau i = i + 1.
- ++i merupakan singkatan dari i += 1 atau i = 1 + i.
Biar lebih jelas, mari kita ganti i menjadi gelas.
Perhatikan kode berikut
int gelas = 0; gelas = 5; gelas++; Sistem.out.println(gelas);
Berapa nilai gelas?
Jawabannya 6, karena nilai gelas sebelumnya akan ditambah satu.
gelas = 5; gelas = gelas + 1;
Paham?
Lalu apa bedanya ++gelas dengan gelas++?
Perbedaan i++ dan ++i dapat kita lihat saat digunakan pada sebuah ekspresi.
Misalkan, selain variabel i, kita punya variabel a. Kemudian variabel a kita isi dengan i++ atau ++i.
Perhatikan contoh berikut:
Post-Increment
int a; int i = 1; a = i++; System.out.println("a = " + a); System.out.println("i = " + i);
Hasil output:
Pre-Increment
int a; int i = 1; a = ++i; System.out.println("a = " + a); System.out.println("i = " + i);
Hasil output:
Kenapa bisa begitu?
Mari kita jabarkan:
- post-increment: a = i++ itu sama seperti a = i; i = i + 1;.
- pre-increment: a = ++i itu sama seperti i = 1 + i; a = i;.
Pada post-increment, variabel a akan mengambil nilai i lalu variabel i ditambah 1. Sedangkan pada pre-increment variabel i akan ditambah satu terlebihdahulu, kemudian diisi ke variabel a.
Kesimpulan
Jadi posisi simbol ++ menentukan kapan nilai variabel akan ditambah.
Bila ditulis di depan, maka akan ditambah dulu. Sedangkan bila ditulis di belakang akan ditambah belakangan.
Selain simbol ++ ada juga simbol -- untuk mengurangi.
Apakah kamu sudah memahami perbedaan i++ dengan ++i sekarang?
Bacaan lebih lanjut:
- What is the difference between ++i and i++?
- Difference between i++ and ++i in a loop?