Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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
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)


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
4759 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, 88, 89, 90, 91, 92, 93, 94, 95, 96 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., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.info/mouth/3004_Inc ... thers.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb