|
Lesson 8
This lesson and the following one will examine how to use the program structure - as opposed to data structure - reserved words. Lets start with the looping structures: do repeated_statement while ( logical_expression ); repeated_statement, which may be a block of code, will be executed repetitively until the logical_expression, becomes false. If you have been exposed to ( corrupted by? ) another language remember that there is no `until' test at the end of a loop. Note that the repeated_statement is always executed once irrespective of the state of the logical_expression. while ( logical_expression ) repeated_statement; repeated_statement is executed repetitively while the logical_expression is true. Once again statement may be a block of code. Note that if the logical_expression evaluates to FALSE then the repeated_statement is NEVER executed. Associated with the looping structures are the control words: break; continue; break; allows you to leave a loop in the middle of a block, and continue; allows you to re-start it from the top. Finally we must not forget the most common and useful looping construct: for ( initialising statement; logical_expression; incremental_statement ) repeated_statement; Some further explanation is needed. The initialising statement is executed once, but to allow for the need to initialise several separate variables the assignment statements may be separated by commas. The logical_expression must be true for the loop to run, and the incremental_statement is executed once each time the loop is run. The for statement is completely general and may, for example, be used to manipulate a set of pointers to operate on a linked list. Some examples. A do loop program. #ident "@(#) do_demo.c - An example of the do loop" #include Fairly obviously it prints: abcdefghijklmnopqrstuvwxyz A while loop example. #ident "@(#) while_demo.c - An example of the while loop" #include Its output is exactly the same as the previous example: abcdefghijklmnopqrstuvwxyz In this totally trivial case it is irrelevant which program structure you use, however you should note that in the `do' program structure the repeated statement is always executed at least once. A for loop example. The `for' looping structure. #ident "@(#) for_demo.c - An example of the for loop" #include Surprise, Surprise! abcdefghijklmnopqrstuvwxyz You should be aware that in all the looping program structures, the repeated statement can be a null statement ( either just a `;' or the reserved word `continue;' ). This means that it is possible to - for example - position a pointer, or count up some items of something or other. It isn't particularly easy to think up a trivial little program which demonstrates this concept, however the two `for' loops give some indication of the idea. #ident "@(#) pointer_demo.c - Pointer operations with the for loop" #include Another Surprise! abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ So much for the looping structures provided by the `C' language. The other main structures required to program a computer are the ones which alter the program flow. These are the switch, and the if and its extension the if ... else combination. More demo programs are much the best way of getting the message across to you, so here they are, first the if construct. #ident "if_demo.c" #include And the if ... else demo. #ident "if_else_demo.c" /* ** The Language #define could go in the compiler invocation line if desired. */ #define ENGLISH #include Well as you can no doubt gather it simply compares two numbers on the command line and ejects a little message depending on the relative magnitude of the numbers. In the UNIX tradition the help message is perhaps somewhat terse, but it serves the purpose of getting you - the student - to think about the importance of creating programs which always cope with nonsensical input in a civilised way. Here are the lines of output. Usage: if_else_demo Now that the international community is shrinking with vastly improved
telecommunications, it is perhaps a good idea to think carefully about
creating programs which can talk in many languages to the users. The method
of choice is - I believe - that presented above. The #if defined( LANGUAGE )
gives us an easy method of changing the source code to suit the new sales
area. Another possibility is to put all the text output needed from a program
into a file. The file would have to have a defined layout and some consistent
way of `getting at' the message strings.
From a commercial point of view this may or may not be a good business plan.
Quite definitely it is an absolute no no to scatter a mass of string literals
containing the messages and message fragments all over your program script.
There are two more methods of altering the program flow.
1 ) The goto a label.
2 ) The setjump / longjmp library routines.
The concept of the go to a label construction has had reams of literary
verbiage written about it and this author does not intend to add to the pile.
Suffice it to say that a goto is a necessary language construct. There are a
few situations which require the language to have ( in practice ) some form of
unconditional jump. Treat this statement with great caution if you wish your
code to be readable by others. An example of legitimate use.
This is one of the very few "legitimate" uses of goto, as there is no
"break_to_outer_loop" in `C'. Note that some compilers complain if the label
is not immediately followed by a statement. If your compiler is one of these
naughty ones, you can put either a `;' or a pair of braces `{}' after the
`:' as a null statement.
An example of a program package which makes extensive use of the goto is the
rz and sz modem communications protocol implementation by Chuck Forsberg of
Omen Technology. You should download it and study the code, but do remember
that the proof of the pudding argument must apply as the rz & sz system has
become extremely popular in its application because it works so well.
The other method of changing program flow is the setjump and longjmp pair of
library functions. The idea is to provide a method of recovery from errors
which might be detected anywhere within a large program - perhaps a compiler,
interpreter or large data acquisition system. Here is the trivial example:
Although in this silly little demo the call to longjmp is in the same file
as the call to setjmp, this does not have to be the case, and in the practical
situation the call to longjmp will be a long way from the call to setjmp. The
mechanism is that setjmp saves the entire state of the computer's CPU in a
buffer declared in the jmp_buf save; statement and longjmp restores it exactly
with the exception of the register which carries the return value from longjmp.
This value is the same as the second argument in the longjmp call - i in our
little demo. This means, of course, that the stack and frame pointer registers
are reset to the old values and all the local variables being used at the time
of the longjmp call are going to be lost forever. One consequence of this is
that any pointer to memory allocated from the heap will also be lost, and
you will be unable to access the data stored in the buffer. This is what the
jargonauts call "memory leakage", and is really very difficult bug to find.
Your program runs out of dynamic memory long before it should. Take care.
So you have to keep a record of the buffers' addresses and free them
before the call to longjmp.
More details later on when we learn about the heap memory allocation routines.
Copyright notice:- (c) 1993 Christopher Sawtell. I assert the right to be known as the author, and owner of the
intellectual property rights of all the files in this material,
except for the quoted examples which have their individual
copyright notices. Permission is granted for onward copying,
but not modification, of this course and its use for personal
study only, provided all the copyright notices are left in the
text and are printed in full on any subsequent paper reproduction. |