Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Increment operators for counting - Perl, PHP, C and others

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
C203 - C and C based languages - Conditionals and Loops
  [4337] Learning to program sample program - past its prime, but still useful - (2014-12-02)
  [4323] Learning to program - Loop statements such as while - (2014-11-22)
  [4322] Learning to Program - the conditional statement (if) - (2014-11-21)
  [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)
  [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)
  [353] Wimbledon Neck - (2005-06-20)

C207 - C and C based languages - Pointers and references
  [4128] Allocating memory dynamically in a static language like C - (2013-06-30)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [3386] Adding the pieces together to make a complete language - C - (2011-08-11)
  [3242] How to return 2 values from a function (C++ and C) - more uses of pointers - (2011-04-10)
  [3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
  [2670] Pointers to Pointers to Pointers - what is the point? - (2010-03-10)
  [2572] The what and why of C pointers - (2010-01-13)
  [2005] Variables and pointers and references - C and C++ - (2009-01-23)
  [1497] Training Season Starts again! - (2008-01-07)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
  [1155] Pointers in C - (2007-04-19)

H106 - PHP - Arrays
  [4244] Disambiguation - PHP List - (2014-03-07)
  [4072] Splitting the difference with PHP - (2013-04-27)
  [4068] Arrays in PHP - contain different and even mixed data types - (2013-04-24)
  [3534] Learning to program in PHP - Regular Expression and Associative Array examples - (2011-12-01)
  [3379] Sorting data the way YOU want it sorted - (2011-08-05)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
  [2274] PHP preg functions - examples and comparision - (2009-07-08)
  [2215] If nothing, make it nothing. - (2009-06-02)
  [1614] When an array is not an array - (2008-04-17)
  [1451] More PHP sample and demonstration programs - (2007-12-01)
  [1199] Testing for one of a list of values. - (2007-05-22)
  [1116] PHP adding arrays / summing arrays - (2007-03-23)
  [832] Displaying data at 5 items per line on a web page - (2006-08-14)
  [773] Breaking bread - (2006-06-22)
  [603] PHP - setting sort order with an associative array - (2006-02-13)
  [409] Functions and commands with dangerous names - (2005-08-11)

P204 - Perl - Conditionals and Loops
  [4031] Showing what programming errors look like - web site pitfall - (2013-03-06)
  [3895] Flowchart to program - learning to program with Well House - (2012-10-14)
  [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)
  [1477] Decisions - small ones, or big ones? - (2007-12-18)
  [1468] Lexical v Arithemetic testing, Bash and Perl - (2007-12-11)
  [930] -> , >= and => in Perl - (2006-11-18)


Back to
What will we be teaching in six years?
Previous and next
or
Horse's mouth home
Forward to
Lots of ways of doing it in Perl - printing out answers
Some other Articles
Dulwich College Preparatory, and Sevenoaks, Schools
Setting up a matrix of data (2D array) for processing in your program
Santa announcement, 5th December 2010, Melksham
Lots of ways of doing it in Perl - printing out answers
Increment operators for counting - Perl, PHP, C and others
What will we be teaching in six years?
A list of special method and attribute names in Python
How will we present courses over the coming years?
Looking forward - the next 3000
2999 - looking back
4350 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3004_Inc ... thers.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat