************* BACK AND FORTH PAGING SUBROUTINE ************

PAGEFLOP

If they want to quit, do so
 If They want to quit subsession, Do SO!
  Decrement LOCAL and GLOBAL Page Counters

FORWARD

  Increment LOCAL and GLOBAL Page Counters
   *************** END PAGEFLOP *********************

P24

Control Constructs|The for Statement|3-8.1|17,54
     The for statement is similar to the while
statement.  All three parts of the for loop are 
specified at the top of the loop.

SYNTAX:
        for ([initialized variables)]; [evaluate
            expression]; [update variables]) {
            statement(s)
        }

         ----------- IS EQUIVALENT TO ------------

        initialized variable(s);
        while (expression) {
            statement(s);
            update variables;
        }

P25

Control Constructs|The for Statement|3-8.2|13,54
The following program calculates the sum of numbers 
in the range of 1 through 10.  The variable sum, 
initialized to zero, holds the current sum and the 
variable i is the iteration counter.

          main()
          {
              int sum, i;
          
              sum = 0;    /* initialize the sum */
              for (i = 1; i <= 10; i++)
                  sum = sum + i;
          }

P26

Control Constructs|The for Statement|3-8.3|7,54
The next example also illustrates a for statement.  
Note that there is an infinite loop, (one with no 
terminating condition) inside the for statement.  
Only when the value of x becomes zero will break 
take the program control outside of the for loop.  
At the end, the terminating value of the variable y 
will be printed.

P27

Control Constructs|The for Statement|3-8.4|12,30
main()
{
   int x = 17;

   for (;;) {
      if (--x)
          y = y + x*x;
      else
          break;
    }
    printf("result = %d\n", y);
}

P28

Control Constructs|The do-while Statement|3-9.1|9,54
     The do - while statement is similar to the 
while statement, however, expression validity is 
tested at the bottom of the loop instead of at 
the top.

SYNTAX:
                      do {
                         statement(s);
                      } while (expression);

P29

Control Constructs|The do-while Statement|3-9.2|17,58
--------->|
|         v
|   -------------
|  | Statement_1 |
|   ------:------
|         v
|   -------------
|  | Statement_n |
|   ------:------
|         v
|         *
|TRUE  *     *
|---*     e?    *
       *     *
          *
          | FALSE
          v
 The do - while statement
 executes a group of statements at
 least ONE time.
 The do - while continues
 its execution as long as
 the test criterion is TRUE 
 at the top of the loop, i.e.,
 NON-ZERO.
  <<<< END OF DIAGRAM >>>>

P30

Control Constructs|The do-while Statement|3-9.3|12,53
The following example demonstrates the do - while 
statement.  The statements inside the loop will be 
executed as long as the while expression evaluates 
to TRUE or non-zero.  This example uses a classical 
method for searching known as the binary search. It 
requires a maximum of log2n (to the base 2) comparisons 
to determine whether a value exists in a sorted array 
of n elements.  Note that the bounds are set up at 
0 and n-1, the index range for C arrays.  The search 
process terminates when either a match is found, or when
lower becomes greater than upper - which logically 
would be absurd.

P31

Control Constructs|The do-while Statement|3-9.4|18,58
b_search(x,ar,n)          /* look for x in ar[0..n-1] */
    int x, ar[];           /* x - item, ar - array     */
    unsigned n;         /* n - number of items in ar[] */
{
    int lower = 0, upper, mid;
    upper = n-1;                /* set up upper limit  */
    do {
        mid = (lower + upper) / 2;
        if (x < ar[mid])
            upper = mid - 1;           /* upper < mid  */
        else if (x > ar[mid])
            lower = mid + 1;             /* lower > mid    */
        else {                          /* found match */
           printf("match found!\n");
           return(mid);
        }
   } while (lower <= upper);
   printf("No match exists\n"); return(-1);    }

P32

Control Constructs|The do-while Statement|3-9.5|11,52
The following is a prototype of the UNIX command wc,
the word count utility.  Our program will count the 
number of characters and the number of lines in the 
input text.  Note that nc holds the number of 
characters, while nl holds the number of lines.  A
line is considered a piece of text delimited by a '\n' 
character or a set of 80 characters.  The process 
itself is straightforward with a do-while loop 
controlling the execution.  An EOF character is the 
terminating condition.  At the end, both the number 
of characters and the number of lines are printed.

P33

Control Constructs|The do-while Statement|3-9.6|17,54
#include <stdio.h>
#define   LIMIT         80               /* line length */
main()
{
   int c;                         /* character buffer   */
   int nc = 0, nl = 0;                    /* initialize */
      do {
         while ((c = getchar()) != '\n' &&
            c != EOF && i < LIMIT )
            nc += 1;     /* increment character counter */
         if (c != EOF)
            nc +=1;     /* new line is also a character */
         nl++;             /*increment the line counter */
      } while (c != EOF);         /* as long as not EOF */
      printf ("number of lines = %d", nl);
      printf ("number of characters = %d\n", nc);
}

P34

Control Constructs|The break Statement|3-10.1|4,46
     The break statement causes a program 
to exit the innermost enclosing loop (or 
switch) immediately, i.e., before the 
terminal value is reached.

P35

Control Constructs|The break Statement|3-10.2|11,54
The following example illustrates the break statement.  
The program reads the input text by calling the function
read_line(), which is already written and linked with 
the function main().  read_line() will return the 
filled line and the number of characters in the line.  
The data in the buffer line will be parsed, and if a 
tab is found within the line, all the characters 
following the tab will be discarded and the leading 
characters before the tab along with '\n' will be
written to standard output.  The break statement will 
terminate the internal while loop.

P36

Control Constructs|The break Statement|3-10.3|11,59
main()
{
   int len, i, char, line[81];
    while (i < len) {   /* substitute newline for tab */
           break;
       }
       i++;
       }
    printf("%s\n",line);
    }
}

P37

Control Constructs|The continue Statement|3-10.4|3,50
     The continue statement causes the next 
iteration of the enclosing loop (for, while, 
do-while) to begin.

P38

Control Constructs|The continue Statement|3-10.5|12,52
The following example demonstrates the use of the 
continue statement within a switch statement.  
Since continue does not apply to the switch 
once continue is executed, control is 
transferred to the top of the while statement.  
On the other hand, the break statement does
apply to the switch statement and transfers
the program control outside the switch statement.  
NOTE:  param is the string buffer.  If this 
buffer contains character flags 'a' and 'x', then 
a TRUE/NON-ZERO value will be returned by the 
function prog_param.

P39

Control Constructs|The continue Statement|3-10.6|18,56
#define  TRUE   1
#define  FALSE  0
proc_param()
{
    int x_flag=FALSE, alpha_flag=FALSE, i=0;
    char params[81];             /* character buffer */
    getparams(params);         /* get the parameters */
    while (params[i++]) {
        switch (params[i]) {
           case 'a' :                /*  the -a FLAG */
               alpha_flag = TRUE; continue;
           case 'x' :                /*  the -x FLAG */
               x_flag = TRUE; continue;
           default:                /* others unknown */
               printf("Unknown Parameter\n"); break;
        }
    }
    return alpha_flag && x_flag; }