({{, x+=dx
adds dx to x and returns the new value of x. }})

OpenersNotesSection(Spacer1)Details

(RefLink[AddTo, paclet:ref/AddTo]) has the attribute (RefLink[HoldFirst, paclet:ref/HoldFirst]).

x+=dx is equivalent to x=x+dx.

OpenersPrimaryExamplesSection(Spacer1)Examples  (10)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

k=1;k+=5

k

OpenersExampleSection1(Spacer1)Scope  (3)

Add to a numerical value:

x=1.5; x+=3.75;x

Add to a symbolic value:

v=a;v+=b;v

Add to all values in a list:

x={1,2,3}

x+=17;x

x+={20,21,22};x

OpenersExampleSection2(Spacer1)Applications  (1)

Find the first prime above one million:

i=10^6-1;
While[!PrimeQ[i],i+=2];
i

OpenersExampleSection3(Spacer1)Properties & Relations  (4)

(RefLink[AddTo, paclet:ref/AddTo]) is a short form of this assignment:

k=1;
k=k+5

(RefLink[PreIncrement, paclet:ref/PreIncrement]) is a special case of (RefLink[AddTo, paclet:ref/AddTo]):

k=1;
k+=1

++k

(RefLink[SubtractFrom, paclet:ref/SubtractFrom]) is closely related:

k=10;
k+=-5

k-=5

A related operation for lists is (RefLink[AppendTo, paclet:ref/AppendTo]):

list={1,2,3};

AppendTo[list,4];
list

OpenersExampleSection4(Spacer1)Possible Issues  (1)

A variable to be incremented must have an initial value:

x+=2

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Increment, paclet:ref/Increment])  ▪  (RefLink[PreIncrement, paclet:ref/PreIncrement])  ▪  (RefLink[Set, paclet:ref/Set])  ▪  (RefLink[PrependTo, paclet:ref/PrependTo])  ▪  (RefLink[Plus, paclet:ref/Plus])  ▪  (RefLink[SubtractFrom, paclet:ref/SubtractFrom])  ▪  (RefLink[TimesBy, paclet:ref/TimesBy])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Special Forms of Assignment, paclet:tutorial/SpecialFormsOfAssignment])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Procedural Programming, paclet:guide/ProceduralProgramming])}, {▪(RefLinkPlain[Assignments, paclet:guide/Assignments])}, {▪(RefLinkPlain[Structured Data in the Cloud, paclet:guide/StructuredDataInTheCloud])}})}})