Topic : C Lessons
Author : Christopher Sawtell
Page : << Previous 15  Next >>
Go to page :


use more and you very quickly
  run out of "line", especially on an eighty column screen ). If a statement
  is too long to fit on a line I break the line up with the arguments set out
  one to a line and I then the indentation rule to the parentheses "()"
  as well. Sample immediately above. Probably as a hang-over from a particular
  pretty printing program which reset the indentation position after the
  printing of the closing brace "}", I am in the habit of doing it as well.
  Long "if" and "for" statements get broken up in the same way. This is
  an example of it all. The fragment of code is taken from a curses oriented
  data input function.

  /*
  ** Put all the cursor positions to zero.
  */

  for ( i = 0;
        s[i].element_name != ( char *) NULL &&
        s[i].element_value != ( char *) NULL;
        i = ( s[i].dependent_function == NULL )
            ? s[i].next : s[i].dependent_next
        )
  {                              /* Note that it is the brace and NOT the    */
                                 /* "for" which moves the indentation level. */
    s[i].cursor_position = 0;
    }

  /*
  ** Go to start of list and hop over any constants.
  */

    for ( i = edit_mode = current_element = 0;
          s[i].element_value == ( char *) NULL ;
          current_element = i = s[i].next
          ) continue;                               /* Note EMPTY statement. */

  /*
  ** Loop through the elements, stopping at end of table marker,
  ** which is an element with neither a pointer to an element_name nor
  ** one to a element_value.
  */

  while ( s[i].element_name != ( char *) NULL &&
          s[i].element_value != ( char *) NULL
          )
  {
    int c;           /* Varable which holds the character from the keyboard. */

    /*
    **  Et Cetera for many lines.
    */

    }


  Note the commenting style. The lefthand comments provide a general
overview of what is happening and the righthand ones a more detailed view.
The double stars make a good marker so it is easy to separate the code and
the comments at a glance.

  The null statement.

  You should be aware that the ";" on its own is translated by the compiler
as a no-operation statement. The usefullness of this is that you can do
little things, such as counting up a list of objects, or positioning a pointer
entirely within a "for" or "while" statement. ( See example above ).
There is, as always, a flip side. It is HORRIBLY EASY to put a ";" at the
end of the line after the closing right parenthesis - after all you do just
that for function calls! The suggestion is to both mark deliberate null
statements with a comment and to use the statement "continue;". Using
the assert macro will pick up these errors at run time.

  The assert macro.

  Refer to the Programmers Reference Manual section 3X and find the
documentation on this most useful tool.

  As usual an example is by far the best wasy to explain it.

/* ----------------------------------------- */

#ident "@(#) assert-demo.c"

#include <stdio.h>
#include <assert.h>

#define TOP_ROW 10
#define TOP_COL 10

main()
{
  int row, col;

  for ( row = 1; row <= TOP_ROW; row++);
  {
    assert ( row <= TOP_ROW );
    for ( col = 1; col <= TOP_COL; col++ )
    {
      assert ( col <= TOP_COL );
      printf ( "%4d", row * col );
      }
    printf ( "\n" );
    }
  }

/* ----------------------------------------- */


  Which produces the output:-

Assertion failed:  row <= TOP_ROW , file assert-demo.c, line 15
ABORT instruction (core dumped)

  It does this because the varable "row" is incremented
to one greater than The value of TOP_ROW.

  Note two things:

  1) The sense of the logical condition. The assert is asserted
     as soon as the result of the logical condition is FALSE.
     Have a look at the file /usr/include/assert.
     Where is the ";" being used as an empty program statement?

  2) The unix operating system has dumped out an image of the executing
     program for examination using a symbolic debugger. Have a play with
     "sdb" in preparation for the lesson which deals with it in more
     detail.

  Lets remove the errant semi-colon, re-compile and re-run the program.

   1   2   3   4   5   6   7   8   9  10
   2   4   6   8  10  12  14  16  18  20
   3   6   9  12  15  18  21  24  27  30
   4   8  12  16  20  24  28  32  36  40
   5  10  15  20  25  30  35  40  45  50
   6  12  18  24  30  36  42  48  54  60
   7  14  21  28  35  42  49  56  63  70
   8  16  24  32  40  48  56  64  72  80
   9  18  27  36  45  54  63  72  81  90
  10  20  30  40  50  60  70  80  90 100

  Here's the ten times multiplication table, for you to give to to
the nearest primary-school child!

  I would agree that it is not possible to compare the value of a program
layout with a real work of fine art such as a John Constable painting or
a Michaelangelo statue, I do think a well laid out and literate example of
programming is not only much easier to read and understand, but also it
does have a certain aesthetic appeal.


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

Page : << Previous 15  Next >>