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 [353] Wimbledon Neck - (2005-06-20)
[962] Breaking a loop - Ruby and other languages - (2006-12-03)
[1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
[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)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
[2570] Function Prototypes in C - (2010-01-11)
[3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
[3121] New year, new C Course - (2011-01-05)
[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)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[4322] Learning to Program - the conditional statement (if) - (2014-11-21)
[4323] Learning to program - Loop statements such as while - (2014-11-22)
[4337] Learning to program sample program - past its prime, but still useful - (2014-12-02)
Some other Articles
Templates in C++ - defining a family pattern of methods / functionsEating out in Melksham - where we like for lunch.Adding the pieces together to make a complete language - CDo university courses teach the right things for life at work later on?Loops - a comparison of goto, while and forAre people who walk into Melksham being asked to subsidise parking?What is the picture?Report - day trip from Swindon / Chippenham / Melksham to WeymouthMelksham Jelly - An Occasional Office for Home WorkersSorting data the way YOU want it sorted