Home Accessibility Courses Diary The Mouth Facebook Resources Site Map About Us Contact
Loops - a comparison of goto, while and for

Almost every program you write, in any language, will have repeating code in it in the form of a loop. Programs without loops are limited to really simple "read a piece of data, calculate on it and print a result" tasks, and ever with them there will be loops within the function calls that you make if not within your own code.

In my early days of programming, I maintained a number of programs that made heavy use of the goto loop, where an instruction to jump backwards in the program to a labelled (named) line caused the piece of code between the label and the goto statement to be repeated multiple times; the goto was accompanied by a conditional which (once it became false) allowed the code to carry on beyond the loop.

goto has gone out of fashion / been replaced by other mechanisms which are less error prone and easier to follow. During yesterday's C course, we ended up talking about gotos, and I wrote an example in class to illustrate the structure. [full source].

Here are the key elements of the goto loop:

  int siblings = 17;
  [more code]
  looper:
  [code to be repeated]
  if (siblings > 1) {
    siblings = siblings - 1;
    goto looper;
  }


The while loop is a massive improvement on goto. There's no danger of badly nested loops, and no need for the maintainance programmer to be desparately seaching up and down the source for a label. I wrote a comparatibe example - [full source]; the key elements are:

  int siblings = 17;
  [more code]
  while (siblings > 0) {
  [code to be repeated]
  siblings = siblings - 1;
  }


That's "better" but not "best". There are still three distinct places you need to look in the code to find out the start point, step and end point of the loop and they can be well separated from each other by intermediate code. But at least you know it's a loop, which you don't initially with the goto.

So what's best? A for loop, where the initial value, end point and step are all a single statement. It turns out to be very easy to conver a while into a for - simply change the word while to for and move the initial setting and increment into the statement, like this:

  int siblings;
  [more code]
  for (siblings = 17; siblings > 0; siblings = siblings - 1) {
  [code to be repeated]
  }


You can see the full final code (and I show some shortenings such as the use of ++ and -- too) [here].
(written 2011-08-10)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
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)
  [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)
  [353] Wimbledon Neck - (2005-06-20)


Back to
Are people who walk into Melksham being asked to subsidise parking?
Previous and next
or
Horse's mouth home
Forward to
Do university courses teach the right things for life at work later on?
Some other Articles
Templates in C++ - defining a family pattern of methods / functions
Eating out in Melksham - where we like for lunch.
Adding the pieces together to make a complete language - C
Do university courses teach the right things for life at work later on?
Loops - a comparison of goto, while and for
Are people who walk into Melksham being asked to subsidise parking?
What is the picture?
Report - day trip from Swindon / Chippenham / Melksham to Weymouth
Melksham Jelly - An Occasional Office for Home Workers
Sorting data the way YOU want it sorted
4255 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 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/3384_Loo ... d-for.html • PAGE BUILT: Sun Mar 30 15:20:58 2014 • BUILD SYSTEM: WomanWithCat