Look at this Perl statement:
$counter = $counter + 1;
"Take the value in $counter, add 1 to it, and put it back in $counter". It's a common programing requirement - indeed, so common that you can write it in a shorter form in many languages:
$counter += 1;
"Add one to the value led in $counter and store the result there". And when you think about it, there's even a word in English for adding 1 - "increment"; many languages (Perl, PHP, C, C++, Ruby, Java ...) have adopted a special syntax for it too:
$counter++
There's an example of this shortening - and other shortenings - on our
Perl Programming Course which I've been running in a tailored, single company form today. The original example is
[here] and the example showing the shortenings is
[here].
Some people (class discussion point -
"is this good programming practice?") go a stage further and write statements such as:
$oc = $counter++;
or
$nc = ++$counter;
Which is going to save the value of counter into a new variable, and also to increment (step it up by one). But beware - there is a difference in what happens here.
• If you write
$nc = ++$counter;
then $counter is pre-incremented - in other words, it's increased before a copy is saved into $nc. So - for example - incoming value of $counter is 15, then the outgoing value is also 15 and
$nc also gets the value 15
• But if you write
$oc = $counter++;
then $counter is post-incremented - in other words, it's only increased after a copy hash been saved into $oc. So - for example - incoming value of $counter is 15, then the outgoing value is also 15 and
$oc gets the old value of 14
Personal advise - if in doubt, write it as 2 lines:
$cv = $counter;
$counter++;
or
$counter++;
$cv = $counter;
then you'll be sure whether the copy was taken before or after the increment.
Notes:
1. There's also a -- operator (decrement) which uses the same principles in each of the languages I have mentioned
2. One of the most powerful uses of ++ is to increment an array of counters - as you can see
[here] in a PHP course example which counts the number of times each picture has been viewed on our web site
3. In C, the ++ operator increments variables that contain integers by 1 as you would expect (example
[here]), but if you use it to increment a
pointer then the pointer will be stepped up by the size of the variable that's pointed to. So on must C compilers,
float *quack;
quack++;
will add 4 to the value held in quack (32 bit float = 4 bytes).
If you're new to C, but have programmed before in another language we offer a C programming course
[here]. We also offer a course in C programming for newcomers to programming (
[here]) and C++ courses (
[here])
(written 2010-10-18, updated 2010-10-20)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P204 - Perl - Conditionals and Loops [353] Wimbledon Neck - (2005-06-20)
[930] -> , >= and => in Perl - (2006-11-18)
[1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
[1468] Lexical v Arithemetic testing, Bash and Perl - (2007-12-11)
[1477] Decisions - small ones, or big ones? - (2007-12-18)
[1607] Learning to program in Perl - (2008-04-11)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1727] Equality and looks like tests - Perl - (2008-07-29)
[2351] Ternary operators alternatives - Perl and Lua lazy operators - (2009-08-12)
[2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3895] Flowchart to program - learning to program with Well House - (2012-10-14)
[4031] Showing what programming errors look like - web site pitfall - (2013-03-06)
[4322] Learning to Program - the conditional statement (if) - (2014-11-21)
[4323] Learning to program - Loop statements such as while - (2014-11-22)
H106 - PHP - Arrays [409] Functions and commands with dangerous names - (2005-08-11)
[603] PHP - setting sort order with an associative array - (2006-02-13)
[773] Breaking bread - (2006-06-22)
[832] Displaying data at 5 items per line on a web page - (2006-08-14)
[1116] PHP adding arrays / summing arrays - (2007-03-23)
[1199] Testing for one of a list of values. - (2007-05-22)
[1451] More PHP sample and demonstration programs - (2007-12-01)
[1614] When an array is not an array - (2008-04-17)
[2215] If nothing, make it nothing. - (2009-06-02)
[2274] PHP preg functions - examples and comparision - (2009-07-08)
[2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[3379] Sorting data the way YOU want it sorted - (2011-08-05)
[3534] Learning to program in PHP - Regular Expression and Associative Array examples - (2011-12-01)
[4068] Arrays in PHP - contain different and even mixed data types - (2013-04-24)
[4072] Splitting the difference with PHP - (2013-04-27)
[4244] Disambiguation - PHP List - (2014-03-07)
C207 - C and C based languages - Pointers and references [1155] Pointers in C - (2007-04-19)
[1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
[1497] Training Season Starts again! - (2008-01-07)
[2005] Variables and pointers and references - C and C++ - (2009-01-23)
[2572] The what and why of C pointers - (2010-01-13)
[2670] Pointers to Pointers to Pointers - what is the point? - (2010-03-10)
[3121] New year, new C Course - (2011-01-05)
[3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
[3242] How to return 2 values from a function (C++ and C) - more uses of pointers - (2011-04-10)
[3386] Adding the pieces together to make a complete language - C - (2011-08-11)
[3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[4128] Allocating memory dynamically in a static language like C - (2013-06-30)
[4560] Variables, Pointers and References - C and C++ - (2015-10-29)
C203 - C and C based languages - Conditionals and Loops [962] Breaking a loop - Ruby and other languages - (2006-12-03)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
[2570] Function Prototypes in C - (2010-01-11)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[3243] Breaking the running sequence - an introduction to conditional statements and loops - (2011-04-11)
[3384] Loops - a comparison of goto, while and for - (2011-08-10)
[4337] Learning to program sample program - past its prime, but still useful - (2014-12-02)
Some other Articles
Dulwich College Preparatory, and Sevenoaks, SchoolsSetting up a matrix of data (2D array) for processing in your programSanta announcement, 5th December 2010, MelkshamLots of ways of doing it in Perl - printing out answersIncrement operators for counting - Perl, PHP, C and othersWhat will we be teaching in six years?A list of special method and attribute names in PythonHow will we present courses over the coming years?Looking forward - the next 30002999 - looking back