What use is the "+=" operator? Why did the language designers bother to provide it? Yet they did ...
In Perl and PHP
$number_on_bus = $number_on_bus + $goton_here;
becomes
$number_on_bus += $goton_here;
In Python
number_on_bus = number_on_bus + goton_here
becomes
number_on_bus += goton_here
In C and Java
number_on_bus = number_on_bus + goton_here;
becomes
number_on_bus += goton_here;
The += operator adds the result of evaluating the expression to the right into the variable named on the left.
The code is shorter, but there's more too it than that. It makes the code easier to maintain - it saves what we call "Wimbledon Neck" which is very appropriate for me to be writing about this week as the tennis championships are just about to start. If I write
$longvariablename = $longthingummyname + $some;
then anyone coming along to maintain the code has to take a careful look on both sides of the assignment to see whether or not the information is being saved in the same slot from which it was loaded or not; can you visualise the maintainer turning his neck to the left, to the right, to the left, to the right rather like watching a tennis volley from the side line? If the element being used is a member of an array (= list in Perl) or hash (= dictionary, = associative array) then that's all the more checking to do.
So - it may not come natural to you to code += (or -= *= /= etc) but
please do so ... you're making life easier for those who follow behind!
(written 2005-06-20, updated 2006-06-05)
Associated topics are indexed under
C203 - C and C based languages - Conditionals and Loops [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3384] Loops - a comparison of goto, while and for - (2011-08-10)
[3243] Breaking the running sequence - an introduction to conditional statements and loops - (2011-04-11)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[3121] New year, new C Course - (2011-01-05)
[3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
[2570] Function Prototypes in C - (2010-01-11)
[2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
[962] Breaking a loop - Ruby and other languages - (2006-12-03)
H104 - PHP - Control Statements [2912] Predictions for the seagull population - (2010-08-09)
[2304] Extracting real data from an exported file in PHP or Perl - (2009-07-25)
[1825] Question Mark - Colon operator (Perl and PHP) - (2008-10-08)
[1477] Decisions - small ones, or big ones? - (2007-12-18)
[1199] Testing for one of a list of values. - (2007-05-22)
[863] Double and Triple equals operator in PHP - (2006-09-12)
[657] The ternary operator in Python - (2006-03-25)
[421] Don't repeat code - use loops or functions - (2005-08-21)
[406] Assignment, equality and identity in PHP - (2005-08-08)
[340] Code and code maintainance efficiency - (2005-06-08)
J704 - Java - Loops and Conditional Statements [3038] Setting up individual variables, and arrays, in Java - some commented examples - (2010-11-09)
[2734] for and foreach in Java - (2010-04-22)
P204 - Perl - Conditionals and Loops [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
[2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[2351] Ternary operators alternatives - Perl and Lua lazy operators - (2009-08-12)
[1727] Equality and looks like tests - Perl - (2008-07-29)
[1607] Learning to program in Perl - (2008-04-11)
[1468] Lexical v Arithemetic testing, Bash and Perl - (2007-12-11)
[930] -> , >= and => in Perl - (2006-11-18)
Y103 - Python - Conditionals and Loops [3558] Python or Lua - which should I use / learn? - (2011-12-21)
[3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
[3083] Python - fresh examples from recent courses - (2010-12-11)
[2899] Groupsave tickets - 3 or 4 train tickets for the price of 2 - (2010-08-02)
[2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
[1661] Equality, sameness and identity - Python - (2008-05-31)
[1201] No switch in Python - (2007-05-23)
[909] Python is like a narrowboat - (2006-10-30)
[835] Python - when to use the in operator - (2006-08-16)
[788] New - Conditional expressions in Python 2.5 - (2006-07-01)
[668] Python - block insets help with documentation - (2006-04-04)
[299] What - no switch or case statement? - (2005-05-03)
Some other Articles
Where do Perl modules load fromSudoku helper or sudoku cheatContext in PerlBallooning from BathWimbledon NeckImproved mining techniques!Is photoajustment an addiction?Want to be one betterComments in TclGraveyard pages