NEW C PRIMER PLUS SECOND EDITION
CHAPTER 7 NOTES
Copyright (C) 1993 by The Waite Group
Copyright (C) 1997 by Linda Buck


 1. If Statement
    ------------
    Form:
        if (expression)
          statement;  or  { statement(s) }

    Meaning:
                |
                V
               / \  zero
              /exp\---------
              \   / (false)|
               \ /         |
                | nonzero  |
                V (true)   |
         ----------------  |
         | statement(s) |  |
         ----------------  |
                |          |
                |<----------
                V

    Note:
    (1) (a) If there is no opening brace ({) immediately after
            if (expression) then the body of the if runs up to the
            first semicolon (;).
        (b) If there is an opening brace ({) immediately after
            if (expression) then the body of the if runs up to the
            matching closing brace (}).

 2. Example 7.1
    -----------
    Program #1:
    /* if1.c -- if statement */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /* braces are optional when the if's statement is one statement */
      printf("Please enter an integer.  Type q to quit.\n");
      while (scanf("%d", &num) == 1)
      {
        if (num > 0)
          printf("num > 0\n");
        printf("**********\n");
        printf("Please enter an integer.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #1:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num > 0
    Output: **********
    Output: Please enter an integer.  Type q to quit.
    Input:  -5
    Output: **********
    Output: Please enter an integer.  Type q to quit.
    Input:  q

    Program #2:
    /* if2.c -- if statement */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /* braces are required when the if's statement is more than one
         statement */
      printf("Please enter an integer.  Type q to quit.\n");
      while (scanf("%d", &num) == 1)
      {
        if (num > 0)
        {
          printf("num > 0\n");
          printf("**********\n");
        }
        printf("Please enter an integer.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #2:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num > 0
    Output: **********
    Output: Please enter an integer.  Type q to quit.
    Input:  -5
    Output: Please enter an integer.  Type q to quit.
    Input:  q

 3. Example 7.2
    -----------
    Program #1:
    /* if3.c -- if statement */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /*** error ***/
      /* semicolon after if (...) -- if's statement is empty */
      printf("Please enter an integer.  Type q to quit.\n");
      while (scanf("%d", &num) == 1)
      {
        if (num > 0);
          printf("num > 0\n");
        printf("Please enter an integer.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #1:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num > 0
    Output: Please enter an integer.  Type q to quit.
    Input:  -5
    Output: num > 0
    Output: Please enter an integer.  Type q to quit.
    Input:  q

    Program #2:
    /* if4.c -- if statement */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /*** error ***/
      /* uses = instead of ==:
         (1) (num==5) -- tests to see if num is 5
         (2) (num=5)
             (a) num = 5 -- assigns 5 to num
             (b) (5) -- 5 is always true */
      printf("Please enter an integer.  Type q to quit.\n");
      while (scanf("%d", &num) == 1)
      {
        if (num = 5)
          printf("num = %d\n", num);
        printf("Please enter an integer.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #2:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num = 5
    Output: Please enter an integer.  Type q to quit.
    Input:  -5
    Output: num = 5
    Output: Please enter an integer.  Type q to quit.
    Input:  q

 4. If-Else Statement
    -----------------
    Form:
        if (expression)
          statement1;  or  { statement(s)1 }
        else
          statement2;  or  { statement(s)2 }

    Meaning:
                        |
                        V
              nonzero  / \  zero
            ----------/exp\----------
            | (true)  \   / (false) |
            V          \ /          V
    -----------------       -----------------
    | statement(s)1 |       | statement(s)2 |
    -----------------       -----------------
            |                       |
            -------------------------
                        |
                        V

    Note:
    (1) If you attempt to put more than one statement between the
        if (expression) and the else without the enclosing braces, the
        compiler will generate an error message at the start of the
        second statement.
    (2) (a) If there is no opening brace ({) immediately after else
            then the body of the else runs up to the first semicolon
            (;).
        (b) If there is an opening brace ({) immediately after else
            then the body of the else runs up to the matching closing
            brace (}).

 5. Example 7.3
    -----------
    Program #1:
    /* if_else1.c -- if-else statement */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /* (1) braces are optional when the if's statement is one
             statement
         (2) braces are optional when the else's statement is one
             statement */
      printf("Please enter an integer.  Type q to quit.\n");
      while (scanf("%d", &num) == 1)
      {
        if (num > 0)
          printf("num > 0\n");
        else
          printf("num <= 0\n");
        printf("**********\n");
        printf("Please enter an integer.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #1:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num > 0
    Output: **********
    Output: Please enter an integer.  Type q to quit.
    Input:  -5
    Outupt: num <= 0
    Output: **********
    Output: Please enter an integer.  Type q to quit.
    Input:  q

    Program #2:
    /* if_else2.c -- if-else statement */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /* (1) braces are required when the if's statement is more than
             one statement
         (2) braces are required when the else's statement is more than
             one statement */
      printf("Please enter an integer.  Type q to quit.\n");
      while (scanf("%d", &num) == 1)
      {
        if (num > 0)
        {
          printf("num > 0\n");
          printf("++++++++++\n");
        }
        else
        {
          printf("num <= 0\n");
          printf("----------\n");
        }
        printf("Please enter an integer.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #2:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num > 0
    Output: ++++++++++
    Output: Please enter an integer.  Type q to quit.
    Input:  -5
    Output: num <= 0
    Output: ----------
    Output: Please enter an integer.  Type q to quit.
    Input:  q

 6. Example 7.4
    -----------
    Program #1:
    /* if_else3.c -- if-else statement */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /* preferred version because it does the minimum number of tests
         on a given num to get the desired result */
      /* tests a given num 1 time */
      printf("Please enter an integer.  Type q to quit.\n");
      while (scanf("%d", &num) == 1)
      {
        if (num > 0)
          printf("num > 0\n");
        else
          printf("num <= 0\n");
        printf("Please enter an integer.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #1:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num > 0
    Output: Please enter an integer.  Type q to quit.
    Input:  -5
    Output: num <= 0
    Output: Please enter an integer.  Type q to quit.
    Input:  q

    Program #2:
    /* if_else4.c -- if-else statement */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /* tests a given num 2 times, gives the same results */
      printf("Please enter an integer.  Type q to quit.\n");
      while (scanf("%d", &num) == 1)
      {
        if (num > 0)
          printf("num > 0\n");
        if (num <= 0)
          printf("num <= 0\n");
        printf("Please enter an integer.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #2:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num > 0
    Output: Please enter an integer.  Type q to quit.
    Input:  -5
    Output: num <= 0
    Output: Please enter an integer.  Type q to quit.
    Input:  q

 7. Pairing elses With ifs
    ----------------------
    Rule:
    An else goes with the most recent if unless braces indicate
    otherwise.

    Example:
    (1)    if (expression1)
          -->if (expression2)
          |    statement1;  or  { statement(s)1 }
          ---else
               statement2;  or  { statement(s)2 }

    (2) -->if (expression1)
        |  {
        |    if (expression2)
        |      statement1;  or  { statement(s)1 }
        |  }
        ---else
             statement2;    or  { statement(s)2 }

 8. Example 7.5
    -----------
    Program #1:
    /* if_else5.c -- if-else statement */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /* an else goes with the most recent if unless braces indicate
         otherwise */
      printf("Please enter an integer.  Type q to quit.\n");
      while (scanf("%d", &num) == 1)
      {
        if (num > 6)
          if (num < 12)
            printf("num>6 && num<12\n");
          else
            printf("num>6 && num>=12\n");
        printf("Please enter an integer.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #1:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: Please enter an integer.  Type q to quit.
    Input:  10
    Output: num>6 && num<12
    Output: Please enter an integer.  Type q to quit.
    Input:  15
    Output: num>6 && num>=12
    Output: Please enter an integer.  Type q to quit.
    Input:  q

    Program #2:
    /* if_else6.c -- if-else statement */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /* an else goes with the most recent if unless braces indicate
         otherwise */
      printf("Please enter an integer.  Type q to quit.\n");
      while (scanf("%d", &num) == 1)
      {
        if (num > 6)
        {
          if (num < 12)
            printf("num>6 && num<12\n");
        }
        else
          printf("num<=6\n");
        printf("Please enter an integer.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #2:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num<=6
    Output: Please enter an integer.  Type q to quit.
    Input:  10
    Output: num>6 && num<12
    Output: Please enter an integer.  Type q to quit.
    Input:  15
    Output: Please enter an integer.  Type q to quit.
    Input:  q

 9. If-Else-If Statement
    --------------------
    Form:
      if (expression1)
        statement1;    or  { statement(s)1 }
      else if (expression2)
        statement2;    or  { statement(s)2 }
      else if (expression3)
        statement3;    or  { statement(s)3 }
           ...
      else if (expressionn)
        statementn;    or  { statement(s)n }
      else  /* optional */
        statementn+1;  or { statement(s)n+1 }

    Meaning:
           |
           V
          / \  zero
         /exp\----------
         \ 1 / (false) |
          \ /          V
           | nonzero  / \  zero
           V (true)  /exp\----------
    ---------------  \ 2 / (false) |
    |statement(s)1|   \ /          V
    ---------------    | nonzero  / \  zero
           |           V (true)  /exp\----------
           |    ---------------  \ 3 / (false) |
           |    |statement(s)2|   \ /          V
           |    ---------------    | nonzero  ...
           |           |           V (true)    |
           |           |    ---------------    V
           |           |    |statement(s)3|   / \  zero
           |           |    ---------------  /exp\----------
           |           |           |         \ n / (false) |
           |           |           |          \ /          |
           |           |           |           | nonzero   |
           |           |           |           V (true)    |
           |           |           |    ---------------    |
           |           |           |    |statement(s)n|    |
           |           |           |    ---------------    |
           |           |           |           |           V
           |           |           |           |    -----------------
           |           |           |           |    |statement(s)n+1|
           |           |           |           |    -----------------
           |           |           |           |           |
           -------------------------------------------------
                               |
                               V

    Note:
    (1) Consider the following:
            if (expression) ... else if (expression)
            else if (expression)... else if (expression)
            else if (expression) ... else
        If you attempt to put more than one statement between any of the
        these without the enclosing braces, the compiler will generate
        an error message at the start of the second statement.
    (2) Consider the following:
            trailing else if (expression) without else
            trailing else
        (a) If there is no opening brace ({) immediately after the
            trailing else if (expression) or trailing else then the
            body of the trailing clause runs up to the first semicolon
            (;).
        (b) If there is an opening brace ({) immediately after the
            trailing else if (expression) or trailing else then the
            body of the trailing clause runs up to the matching closing
            brace (}).

10. Example 7.6
    -----------
    Program:
    /* ifelif1.c -- if-else-if statement */
    #include <stdio.h>
    int main(void)
    {
      int num;

      printf("Please enter an integer.  Type q to quit.\n");
      while (scanf("%d", &num) == 1)
      {
        if (num > 0)
          printf("num > 0\n");
        else if (num == 0)
          printf("num == 0\n");
        else
          printf("num < 0\n");
        printf("Please enter an integer.  Type q to quit.\n");
      }
      return 0;
    }

    Screen:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num > 0
    Output: Please enter an integer.  Type q to quit.
    Input:  0
    Output: num == 0
    Output: Please enter an integer.  Type q to quit.
    Input:  -5
    Output: num < 0
    Output: Please enter an integer.  Type q to quit.
    Input:  q

11. Example 7.7
    -----------
    Program #1:
    /* ifelif2.c -- if-else-if statement */
    #include <stdio.h>
    int main(void)
    {
      int score;
      char grade;

      /* preferred version because it does the minimum number of tests
         on a given score to get the desired result */
      /* tests a given score 1-4 times */
      printf("Please enter a test score.  Type q to quit.\n");
      while (scanf("%d", &score) == 1)
      {
        if (score >= 90)
          grade = 'A';
        else if (score >= 80)
          grade = 'B';
        else if (score >= 70) 
          grade = 'C';
        else if (score >= 60)
          grade = 'D';
        else
          grade = 'E';
        printf("grade = %c\n", grade);
        printf("Please enter a test score.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #1:
    Output: Please enter a test score.  Type q to quit.
    Input:  95
    Output: grade = A
    Output: Please enter a test score.  Type q to quit.
    Input:  85
    Output: grade = B
    Output: Please enter a test score.  Type q to quit.
    Input:  75
    Output: grade = C
    Output: Please enter a test score.  Type q to quit.
    Input:  65
    Output: grade = D
    Output: Please enter a test score.  Type q to quit.
    Input:  55
    Output: grade = E
    Output: Please enter a test score.  Type q to quit.
    Input:  q
    
    Program #2:
    /* ifelif3.c -- if-else-if statement */
    #include <stdio.h>
    int main(void)
    {
      int score;
      char grade;

      /* tests a given score 1-8 times, gives the same results */
      printf("Please enter a test score.  Type q to quit.\n");
      while (scanf("%d", &score) == 1)
      {
        if (score >= 90)
          grade = 'A';
        else if (score >= 80 && score < 90) /* score<90 is redundant */
          grade = 'B';
        else if (score >= 70 && score < 80) /* score<80 is redundant */
          grade = 'C';
        else if (score >= 60 && score < 70) /* score<70 is redundant */
          grade = 'D';
        else if (score < 60)                /* score<60 is redundant */
          grade = 'E';
        printf("grade = %c\n", grade);
        printf("Please enter a test score.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #2:
    Output: Please enter a test score.  Type q to quit.
    Input:  95
    Output: grade = A
    Output: Please enter a test score.  Type q to quit.
    Input:  85
    Output: grade = B
    Output: Please enter a test score.  Type q to quit.
    Input:  75
    Output: grade = C
    Output: Please enter a test score.  Type q to quit.
    Input:  65
    Output: grade = D
    Output: Please enter a test score.  Type q to quit.
    Input:  55
    Output: grade = E
    Output: Please enter a test score.  Type q to quit.
    Input:  q

    Program #3:
    /* ifelif4.c -- if-else-if statement */
    #include <stdio.h>
    int main(void)
    {
      int score;
      char grade;

      /* tests a given score 5-8 times, gives the same results */
      printf("Please enter a test score.  Type q to quit.\n");
      while (scanf("%d", &score) == 1)
      {
        if (score >= 90)
          grade = 'A';
        if (score >= 80 && score < 90)
          grade = 'B';
        if (score >= 70 && score < 80) 
          grade = 'C';
        if (score >= 60 && score < 70)
          grade = 'D';
        if (score < 60)
          grade = 'E';
        printf("grade = %c\n", grade);
        printf("\nPlease enter a test score.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #3:
    Output: Please enter a test score.  Type q to quit.
    Input:  95
    Output: grade = A
    Output: Please enter a test score.  Type q to quit.
    Input:  85
    Output: grade = B
    Output: Please enter a test score.  Type q to quit.
    Input:  75
    Output: grade = C
    Output: Please enter a test score.  Type q to quit.
    Input:  65
    Output: grade = D
    Output: Please enter a test score.  Type q to quit.
    Input:  55
    Output: grade = E
    Output: Please enter a test score.  Type q to quit.
    Input:  q

12. getchar() and putchar()
    -----------------------
    #include <stdio.h>
    (1) getchar()
        Returns the next character from input.
    (2) putchar(arg)
        Prints its character argument.

13. Example 7.8
    -----------
    Program #1:
    /* get_put1.c -- getchar() and putchar() */
    #include <stdio.h>
    int main(void)
    {
      char ch;

      printf("Please enter a line of text.\n");
      while ((ch = getchar()) != '\n')
        putchar(ch);
      putchar('\n');
      return 0;
    }

    Screen #1:
    Output: Please enter a line of text.
    Input:  Abc Def
    Output: Abc Def

    Program #2:
    /* get_put2.c -- getchar() and putchar() */
    #include <stdio.h>
    int main(void)
    {
      char ch;

      /*** error ***/
      /* the parentheses around ch=getchar() are necessary because !=
         has higher precedence than =:
         (1) ((ch=getchar())!='\n')
             (a) ch=getchar() -- character read assigned to ch
             (b) ch!='\n'      -- compares character read to '\n'
         (2) (ch=getchar()!='\n')
             (a) tmp = (getchar()!='\n') -- compares character read to
                                           '\n'
             (b) ch = tmp -- value of comparison assigned to ch */
      printf("Please enter a line of text.\n");
      while (ch = getchar() != '\n')
        putchar(ch);
      putchar('\n');
      return 0;
    }

    Screen #2:
    Output: Please enter a line of text.
    Input:  Abc Def
    Output:        

14. Example 7.9
    -----------
    Problem #1:
    Write a program that reads a line of text.  Have it count the
    number of letters.

    Program #1:
    /* count1.c -- counts letters */
    #include <stdio.h>
    int main(void)
    {
      char ch;
      int n_letters;

      printf("This program counts letters.\n");
      n_letters = 0;
      printf("Please enter a line of text.\n");
      while ((ch = getchar()) != '\n')
      {
        if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
          n_letters++;
      }
      printf("number of letters = %d\n", n_letters);
      return 0;
    }

    Screen #1:
    Output: This program counts letters.
    Output: Please enter a line of text.
    Input:  Ab1 Cde.
    Output: number of letters = 5

    Problem #2:
    Write a program that reads a line of text.  Have it count the 
    number of letters and other characters.

    Program #2:
    /* count2.c -- counts letters and other characters */
    #include <stdio.h>
    int main(void)
    {
      char ch;
      int n_letters, n_others;

      printf("This program counts letters and other characters.\n");
      n_letters = n_others = 0;
      printf("Please enter a line of text.\n");
      while ((ch = getchar()) != '\n')
      {
        if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
          n_letters++;
        else
          n_others++;
      }
      printf("number of letters = %d\n", n_letters);
      printf("number of others = %d\n", n_others);
      return 0;
    }

    Screen #2:
    Output: This program counts letters and other characters.
    Output: Please enter a line of text.
    Input:  Ab1 Cde.
    Output: number of letters = 5
    Output: number of others = 3

    Problem #3:
    Write a program that reads a line of text.  Have it count the
    number of uppercase letters, lowercase letters, and digits.

    Program #3:
    /* count3.c -- counts uppercase letters, lowercase letters, and
                   digits */
    #include <stdio.h>
    int main(void)
    {
      char ch;
      int n_upper, n_lower, n_digits;

      printf("This program counts uppercase letters, lowercase\n");
      printf("letters, and digits.\n");
      n_upper = n_lower = n_digits = 0;
      printf("Please enter a line of text.\n");
      while ((ch = getchar()) != '\n')
      {
        if (ch >= 'A' && ch <= 'Z')
          n_upper++;
        else if (ch >= 'a' && ch <= 'z')
          n_lower++;
        else if (ch >= '0' && ch <= '9')
          n_digits++;
      }
      printf("number of uppercase letters = %d\n", n_upper);
      printf("number of lowercase letters = %d\n", n_lower);
      printf("number of digits = %d\n", n_digits);
      return 0;
    }

    Screen #3:
    Output: This program counts uppercase letters, lowercase
    Output: letters, and digits.
    Output: Please enter a line of text.
    Input:  Ab1 Cde.
    Output: number of uppercase letters = 2
    Output: number of lowercase letters = 3
    Output: number of digits = 1

    Problem #4:
    Write a program that reads a line of text.  Have it count the
    number of uppercase letters, lowercase letters, digits, and other
    characters.

    Program #4:
    /* count4.c -- counts uppercase letters, lowercase letters, digits,
                   and other characters */
    #include <stdio.h>
    int main(void)
    {
      char ch;
      int n_upper, n_lower, n_digits, n_others;

      printf("This program counts uppercase letters, lowercase\n");
      printf("letters, digits, and other characters.\n");
      n_upper = n_lower = n_digits = n_others = 0;
      printf("Please enter a line of text.\n");
      while ((ch = getchar()) != '\n')
      {
        if (ch >= 'A' && ch <= 'Z')
          n_upper++;
        else if (ch >= 'a' && ch <= 'z')
          n_lower++;
        else if (ch >= '0' && ch <= '9')
          n_digits++;
        else
          n_others++;
      }
      printf("number of uppercase letters = %d\n", n_upper);
      printf("number of lowercase letters = %d\n", n_lower);
      printf("number of digits = %d\n", n_digits);
      printf("number of others = %d\n", n_others);
      return 0;
    }

    Screen #4:
    Output: This program counts uppercase letters, lowercase
    Output: letters, digits, and other characters.
    Output: Please enter a line of text.
    Input:  Ab1 Cde.
    Output: number of uppercase letters = 2
    Output: number of lowercase letters = 3
    Output: number of digits = 1
    Output: number of others = 2

15. Example 7.10
    ------------
    Problem:
    Write a program that reads input up to the # character.  Have it
    find the the average number of letters per word.

    Program #1:
    /* average1.c -- finds average number of letters per word */
    #include <stdio.h>
    int main(void)
    {
      char prev_ch, cur_ch;
      int n_letters, n_words;

      printf("This program finds the average number of letters per "
        "word.\n");
      prev_ch = ' ';
      n_letters = n_words = 0;
      printf("Please enter some text terminated by #.\n");
      while ((cur_ch = getchar()) != '#')
      {
        /* check to see if current character is not whitespace */
        if (cur_ch != ' ' && cur_ch != '\n' && cur_ch != '\t')
        {
          /* in a word, so count a letter */
          n_letters++;
          /* check to see if previous character is whitespace */
          if (prev_ch == ' ' || prev_ch == '\n' || prev_ch == '\t')
            /* first character in word, so count word */
            n_words++;
        }
        /* make current character previous character for next pass
           through loop */
        prev_ch = cur_ch;
      }
      /* calculate and print average number of letters per word */
      printf("letters = %d, words = %d\n", n_letters, n_words);
      printf("average letters per word = %.2lf\n",
        (double)n_letters/(double)n_words);
      return 0;
    }

    Screen #1:
    Output: This program finds the average number of letters per word.
    Output: Please enter some text terminated by #.
    Input:  I  am
    Input:  here#
    Output: letters = 7, words = 3
    Output: average letters per word = 2.33

    Comment #1:
    (1) This program counts words at the beginning of a word.  Thus, if
        there is no trailing whitespace the last word will still be
        counted.
    (2) This program checks a given input character for whitespace
        twice, once when it is the current character and once when
        it is the previous character.

    Problem #2:
    Make the previous program more efficient by only checking a given
    input character for whitespace once.

    Program #2:
    /* average2.c -- finds average number of letters per word */
    #include <stdio.h>
    int main(void)
    {
      char ch;
      int prev_white, cur_white;
      int n_letters, n_words;

      printf("This program finds the average number of letters per "
        "word.\n");
      prev_white = 1;
      n_letters = n_words = 0;
      printf("Please enter some text terminated by #.\n");
      while ((ch = getchar()) != '#')
      {
        /* set current character whitespace flag */
        cur_white = (ch == ' ' || ch == '\n' || ch == '\t');
        /* check to see if current character is not whitespace */
        if (!cur_white)
        {
          /* in a word, so count a letter */
          n_letters++;
          /* check to see if previous character is whitespace */
          if (prev_white)
            /* first character in word, so count word */
            n_words++;
        }
        /* make current character whitespace flag previous character
           whitespace flag for next pass through loop */
        prev_white = cur_white;
      }
      /* calculate and print average number of letters per word */
      printf("letters = %d, words = %d\n", n_letters, n_words);
      printf("average letters per word = %.2lf\n",
        (double)n_letters/(double)n_words);
      return 0;
    }

    Screen #2:
    Output: This program finds the average number of letters per word.
    Output: Please enter some text terminated by #.
    Input:  I  am
    Input:  here#
    Output: letters = 7, words = 3
    Output: average letters per word = 2.33

16. Example 7.11
    ------------
    Problem:
    Write a program to calculate electric bills based on the
    following rates.
        First 240 kWh: $0.11439 per kWh
        Next  300 kWh: $0.13290 per kWh
        Over  540 kWh: $0.14022 per kWh

    Program #1:
    /* bill1.c -- calculates electric bills */
    #include <stdio.h>
    #define RATE1 0.11439  /* rate for first 240 kwh */
    #define RATE2 0.13290  /* rate for next 300 kwh */
    #define RATE3 0.14022  /* rate for over 540 kwh */
    #define BREAK1 240.0   /* first breakpoint for rates */
    #define BREAK2 540.0   /* second breakpoint for rates */

    int main(void)
    {
      double kwh, kwh1, kwh2, kwh3, bill;

      printf("This program calculates electric bills.\n");
      printf("\nPlease enter the kwh used.  Type q to quit.\n");
      while (scanf("%lf", &kwh) == 1)
      {
        if (kwh <= BREAK1)
        {
          kwh1 = kwh;
          kwh2 = 0.0;
          kwh3 = 0.0;
        }
        else if (kwh <= BREAK2)
        {
          kwh1 = BREAK1;
          kwh2 = kwh - BREAK1;
          kwh3 = 0.0;
        }
	else
        {
          kwh1 = BREAK1;
          kwh2 = BREAK2 - BREAK1;
          kwh3 = kwh - BREAK2;
        }
        bill = RATE1*kwh1 + RATE2*kwh2 + RATE3*kwh3;
        printf("kwh = %.2lf\n", kwh);
        printf("bill = $%.2lf\n", bill);
        printf("\nPlease enter the kwh used.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #1:
    Output: This program calculates electric bills.
    Output:
    Output: Please enter the kwh used.  Type q to quit.
    Input:  100
    Output: kwh = 100.00
    Output: bill = $11.44
    Output:
    Output: Please enter the kwh used.  Type q to quit.
    Input:  340
    Output: kwh = 340.00
    Output: bill = $40.74
    Output:
    Output: Please enter the kwh used.  Type q to quit.
    Input:  640
    Output: kwh = 640.00
    Output: bill = $81.35
    Output:
    Output: Please enter the kwh used.  Type q to quit.
    Input:  q

    Program #2:
    /* bill2.c -- calculates electric bills */
    #include <stdio.h>
    #define RATE1 0.11439  /* rate for first 240 kwh */
    #define RATE2 0.13290  /* rate for next 300 kwh */
    #define RATE3 0.14022  /* rate for over 540 kwh */
    #define BREAK1 240.0   /* first breakpoint for rates */
    #define BREAK2 540.0   /* second breakpoint for rates */

    int main(void)
    {
      double kwh, bill;

      printf("This program calculates electric bills.\n");
      printf("\nPlease enter the kwh used.  Type q to quit.\n");
      while (scanf("%lf", &kwh) == 1)
      {
        if (kwh <= BREAK1)
          bill = RATE1*kwh;
        else if (kwh <= BREAK2)
          bill = RATE1*BREAK1 + RATE2*(kwh-BREAK1);
        else
          bill = RATE1*BREAK1 + RATE2*(BREAK2-BREAK1) +
                 RATE3*(kwh-BREAK2);
        printf("kwh = %.2lf\n", kwh);
        printf("bill = $%.2lf\n", bill);
        printf("\nPlease enter the kwh used.  Type q to quit.\n");
      }
      return 0;
    }

    Screen #2:
    Output: This program calculates electric bills.
    Output:
    Output: Please enter the kwh used.  Type q to quit.
    Input:  100
    Output: kwh = 100.00
    Output: bill = $11.44
    Output:
    Output: Please enter the kwh used.  Type q to quit.
    Input:  340
    Output: kwh = 340.00
    Output: bill = $40.74
    Output:
    Output: Please enter the kwh used.  Type q to quit.
    Input:  640
    Output: kwh = 640.00
    Output: bill = $81.35
    Output:
    Output: Please enter the kwh used.  Type q to quit.
    Input:  q

17. The Conditional Operator
    ------------------------
    Table of Operators
    ------------------
    Conditional Operator:
         Operator                          Meaning
    exp1 ? exp2 : exp3  The value of the whole expression equals the
                        value of exp2 if exp1 is true.  Otherwise, it
                        equals the value of exp3.

    Table of Operators in Order of Decreasing Precedence
    ------------------------------------------------------
    Operator                                 Associativity
    Table of Operators in Order of Decreasing Precedence
    ----------------------------------------------------
         Operator                                   Associativity
     (1) ++(postfix) --(postfix) () []              left to right
     (2) ++(prefix) --(prefix) +(unary) -(unary) !  right to left
         sizeof (type)(all unary)
     (3) * / %                                      left to right
     (4) +(binary) -(binary)                        left to right
     (5) < > <= >=                                  left to right
     (6) == !=                                      left to right
     (7) &&                                         left to right
     (8) ||                                         left to right
     (9) ?:(trinary operator)                       left to right
    (10) = *= /= %= += -=                           right to left
    (11) ,(comma operator)                          left to right

    Examples:
    (1) (5 > 3) ? 1 : 2  has the value 1
        (3 > 5) ? 1 : 2  has the value 2
    (2) absolute value
        x = (y < 0) ? -y : y;
            is equivalent to
        if (y < 0)
          x = -y;
        else
          x = y;
    (3) maximum
        max = (a > b) ? a : b;
            is equivalent to
        if (a > b)
          max = a;
        else
          max = b;

18. Example 7.12
    ------------
    Problem:
    Write a program that calculates how many cans of paint are needed to
    paint a given number of square feet.

    Program:
    /* paint.c -- calculates how many cans of paint are needed to paint
                  a given number of square feet */
    #include <stdio.h>
    #define COVERAGE 200  /* square feet per paint can */
    int main(void)
    {
      int sq_feet, cans;

      printf("This program calculates how many cans of paint are\n");
      printf("needed to paint a given number of square feet.\n");
      printf("\nPlease enter the number of square feet.\n");
      printf("Type q to quit.\n");
      while (scanf("%d", &sq_feet) == 1)
      {
        cans = sq_feet / COVERAGE;
        cans += (sq_feet % COVERAGE == 0) ? 0 : 1;
        printf("You need %d can%s of paint.\n", cans,
          cans == 1 ? "" : "s");
        printf("\nPlease enter the number of square feet.\n");
        printf("Type q to quit.\n");
      }
      return 0;
    }

    Screen:
    Output: This program calculates how many cans of paint are
    Output: needed to paint a given number of square feet.
    Output:
    Output: Please enter the number of square feet.
    Output: Type q to quit.
    Input:  200
    Output: You need 1 can of paint.
    Output:
    Output: Please enter the number of square feet.
    Input:  210
    Output: Type q to quit.
    Output: You need 2 cans of paint.
    Output:
    Output: Please enter the number of square feet.
    Output: Type q to quit.
    Input:  q

19. continue and break Statements
    -----------------------------
    Definitions:
    (1) continue
        The continue statement can be used with any of the three loop
        forms (while, for, and do-while).  It causes program control to
        skip the remaining statements in a loop.
        (1) while loop
            Starts next loop cycle.
        (2) for loop
            Does update expression and then starts next loop cycle.
        (3) do-while loop
            Does test expression and then, if necessary, starts next
            loop cycle.
    (2) break
        The break statement can be used with any of the three loop forms
        (while, for, and do-while).  It causes program control to skip
        the rest of the loop containing it and to resume with the next
        statement following the loop.

    Diagrams:
    (1) continue
           loop top
           {
             statement(s)1
             if (expression)
        -------continue;
        |    statement(s)2
        -->} loop bottem
           statement(s)3
    (2) break
           loop top
           {
             statement(s)1
             if (expression)
        -------break;
        |    statement(s)2
        |  } loop bottem
        -->statement(s)3

     Note:
     (1) A continue can always be replaced with an else.
         Continue version:       Else version:
         while (expression1)     while (expression1)
         {                       {
           statement(s)1           statement(s)1
           if (expression2)        if (expression2)
           {                       {
             statement(s)2           statement(s)2
             continue;             }
           }                       else
           statement(s)3           {
         }                           statement(s)3
                                   }
                                 }
     (2) If a continue or break is inside nested loops, it affects only
         the innermost loop containing it.
         Correct interpretation:    Incorrect interpretation:
          while (expression1)          while (expression1)
          {                            {
            statement(s)1                statement(s)1
            while (expression2)          while (expression2)
            {                            {
              statement(s)2                statement(s)2
              if (expression3)             if (expression3)
         -------break;              ---------break;
         |    statement(s)3         |      statement(s)3
         |  }                       |    }
         -->statement(s)4           |    statement(s)4
          }                         |  }
          statement(s)5             -->statement(s)5

20. Example 7.13
    ------------
    Program #1:
    /* cont1.c -- continue with a while loop */
    #include <stdio.h>
    int main(void)
    {
      int num, count;

      printf("Please enter an integer.  Type q to quit.\n");
      count = 0;
      while (scanf("%d", &num) == 1)
      {
        if (num < 0)
        {
          printf("Error: integer must be positive.  Please retry.\n");
          continue;
        }
        printf("num = %d\n", num);
        count++;
        printf("Please enter an integer.  Type q to quit.\n");
      }
      printf("count = %d\n", count);
      return 0;
    }

    Screen #1:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num = 5
    Output: Please enter an integer.  Type q to quit.
    Input:  -10
    Output: Error: integer must be positive.  Please retry.
    Input:  10
    Output: num = 10
    Output: Please enter an integer.  Type q to quit.
    Input:  q
    Output: count = 2

    Program #2:
    /* cont2.c -- continue with a for loop */
    #include <stdio.h>
    int main(void)
    {
      int num, count;

      printf("Please enter an integer.  Type q to quit.\n");
      for (count = 0; scanf("%d", &num) == 1; count++)
      {
        if (num < 0)
        {
          printf("Error: integer must be positive.  Please retry.\n");
          continue;
        }
        printf("num = %d\n", num);
        printf("Please enter an integer.  Type q to quit.\n");
      }
      printf("count = %d\n", count);
      return 0;
    }

    Screen #2:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num = 5
    Output: Please enter an integer.  Type q to quit.
    Input:  -10
    Output: Error: integer must be positive.  Please retry.\n");
    Input:  10
    Output: num = 10
    Output: Please enter an integer.  Type q to quit.
    Input:  q
    Output: count = 3

21. Example 7.14
    ------------
    Program #1:
    /* break1.c -- break with a while loop */
    #include <stdio.h>
    int main(void)
    {
      int num, count;

      printf("Please enter an integer.  Type q to quit.\n");
      count = 0;
      while (scanf("%d", &num) == 1)
      {
        if (num < 0)
        {
          printf("Error: num < 0.\n");
          break;
        }
        printf("num = %d\n", num);
        count++;
        printf("Please enter an integer.  Type q to quit.\n");
      }
      printf("count = %d\n", count);
      return 0;
    }

    Screen #1:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num = 5
    Output: Please enter an integer.  Type q to quit.
    Input:  -10
    Output: Error: num < 0.
    Output: count = 1

    Program #2:
    /* break2.c -- break with a for loop */
    #include <stdio.h>
    int main(void)
    {
      int num, count;

      printf("Please enter an integer.  Type q to quit.\n");
      for (count = 0; scanf("%d", &num) == 1; count++)
      {
        if (num < 0)
        {
          printf("Error: num < 0.\n");
          break;
        }
        printf("num = %d\n", num);
        printf("Please enter an integer.  Type q to quit.\n");
      }
      printf("count = %d\n", count);
      return 0;
    }

    Screen #2:
    Output: Please enter an integer.  Type q to quit.
    Input:  5
    Output: num = 5
    Output: Please enter an integer.  Type q to quit.
    Input:  -10
    Output: Error: num < 0.
    Output: count = 1
        
22. Example 7.15
    ------------
    Problem:
    Write a program that calculates the area of a rectangle.

    Problem #1:
    /* area1.c -- calculates the area of a rectangle */
    #include <stdio.h>
    int main(void)
    {
      double length, width;

      /* repeats prompt, combined read */
      printf("This program calculates the area of a rectangle.\n");
      printf("\nPlease enter the length and width.  Type q to quit.\n");
      while (scanf("%lf %lf", &length, &width) == 2)
      {
        printf("Length = %.2lf\n", length);
        printf("Width = %.2lf\n", width);
        printf("Area = %.2lf\n", length * width);
        printf("\nPlease enter the length and width.  Type q to "
          "quit.\n");
      }
      return 0;
    }

    Screen #1:
    Output: This program calculates the area of a rectangle.
    Output:
    Output: Please enter the length and width.  Type q to quit.
    Input:  5 10
    Output: Length = 5.00
    Output: Width = 10.00
    Output: Area = 50.00
    Output:
    Output: Please enter the length and width.  Type q to quit.
    Input:  2.5 q

    Program #2:
    /* area2.c -- calculates the area of a rectangle */
    #include <stdio.h>
    int main(void)
    {
      double length, width;

      /* does not repeat prompt, combined read */
      printf("This program calculates the area of a rectangle.\n");
      while (1)
      {
        printf("\nPlease enter the length and width.  Type q to "
          "quit.\n");
        if (scanf("%lf %lf", &length, &width) != 2)
          break;
        printf("Length = %.2lf\n", length);
        printf("Width = %.2lf\n", width);
        printf("Area = %.2lf\n", length * width);
      }
      return 0;
    }

    Screen #2:
    Output: This program calculates the area of a rectangle.
    Output:
    Output: Please enter the length and width.  Type q to quit.
    Input:  5 10
    Output: Length = 5.00
    Output: Width = 10.00
    Output: Area = 50.00
    Output:
    Output: Please enter the length and width.  Type q to quit.
    Input:  2.5 q

    Program #3:
    /* area3.c -- calculates the area of a rectangle */
    #include <stdio.h>
    int main(void)
    {
      double length, width;

      /* does not repeat prompt, separate reads */
      printf("This program calculates the area of a rectangle.\n");
      while (1)
      {
        printf("\nPlease enter the length.  Type q to quit.\n");
        if (scanf("%lf", &length) != 1)
          break;
        printf("Length = %.2lf\n", length);
        printf("Please enter the width.  Type q to quit.\n");
        if (scanf("%lf", &width) != 1)
          break;
        printf("Width = %.2lf\n", width);
        printf("Area = %.2lf\n", length * width);
      }
      return 0;
    }

    Screen #3:
    Output: This program calculates the area of a rectangle.
    Output:
    Output: Please enter the length.  Type q to quit.
    Input:  5
    Output: Length = 5.00
    Output: Please enter the width.  Type q to quit.
    Input:  10
    Output: Width = 10.00
    Output: Area = 50.00
    Output:
    Output: Please enter the length.  Type q to quit.
    Input:  2.5
    Output: Length = 2.50
    Output: Please enter the width.  Type q to quit.
    Input:  q

23. Example 7.16
    ------------
    Problem:
    Write a program that calculates the minimum, the maximum, and the
    average of a set of test scores.  Screen out invalid test scores.

    Program:
    /* scores.c -- calculates the minimum, the maximum, and the average
                   of a set of test scores */
    #include <stdio.h>
    #define MIN 0
    #define MAX 100
    int main(void)
    {
      int score, min, max, total, n_scores;

      printf("This program calculates the minimum, the maximum, and\n");
      printf("the average of a set of test scores.\n");
      min = MAX; max = MIN;
      total = n_scores = 0;
      while (1)
      {
        printf("\nPlease enter a test score.  Type q to quit.\n");
        if (scanf("%d", &score) != 1)
          break;
        if (score < MIN || score > MAX)
        {
          printf("Error: invalid test score.  Please retry.\n");
          continue;
        }
        min = (score < min) ? score : min;
        max = (score > max) ? score : max;
        total += score;
        n_scores++;
      }
      if (n_scores == 0)
        printf("\nNo valid scores were entered.\n");
      else
      {
        printf("\nMinimum = %d, Maximum = %d\n", min, max);
        printf("Average of %d scores = %.2lf\n", n_scores,
          (double)total/(double)n_scores);
      }
      return 0;
    }

    Screen:
    Output: This program calculates the minimum, the maximum, and
    Output: the average of a set of test scores.
    Output:
    Output: Please enter a test score.  Type q to quit.
    Input:  85
    Output:
    Output: Please enter a test score.  Type q to quit.
    Input:  150
    Output: Error: invalid test score.  Please retry.
    Input:  95
    Output:
    Output: Please enter a test score.  Type q to quit.
    Input:  74
    Output:
    Output: Please enter a test score.  Type q to quit.
    Input:  q
    Output:
    Output: Minimum = 74, Maximum = 95
    Output: Average of 3 scores = 84.67

24. goto Statement
    --------------
    Definition:
    A goto statement causes program control to jump to a statement
    bearing the indicated label.  A colon is used to separate a labeled
    statement from its label.  Label names follow the rules for variable
    names.  The labeled statement can come either before or after the
    goto.

    Diagram:
    ---goto label;     or   -->label:
    |  statement(s)1;       |  statement(s)1
    -->label:               ---goto label;
       statement(s)2;          statement(s)2

    Note:
    In principle, you never need to use the goto in a C program.
    However, there is one use of goto that is tolerated by many C
    programmers -- getting out of a nested set of loops.
    Sample code:
    while (expression1)
    {
      statement(s)1;
      for (i = 0; i < limit; i++)
      {
        statement(s)2;
        if (expression2)
          goto next;
        statement(s)3;
      }
      statement(s)4;
    }
    next:
    statement(s)5;

25. switch Statement
    ----------------
    Form:
        switch (expression)
        {
          case constant1:
            statement(s)1;
            break;
          case constant2:
            statement(s)2;
            break;
             ...
          case constantn:
            statement(s)n;
            break;
          default:  /* optional */
            statement(s)n+1;
            break;
         }

    Meaning (without default):
                                   |
                                   V
                                  / \
                                 /exp\
                                 \   /
                                  \ /
                                   |
       constant1       constant2  ...    constantn       otherwise
           ---------------------------------------------------
           |               |                 |               |
           V               V                 V               |
    --------------- ---------------   ---------------        |
    |statement(s)1| |statement(s)2|   |statement(s)n|        |
    --------------- ---------------   ---------------        |
           |               |                 |               |
           ---------------------------------------------------
                                   |
                                   V

    Meaning (with default):
                                   |
                                   V
                                  / \
                                 /exp\
                                 \   /
                                  \ /
                                   |
       constant1       constant2  ...    constantn       otherwise
           ---------------------------------------------------
           |               |                 |               |
           V               V                 V               V
    --------------- ---------------   --------------- -----------------
    |statement(s)1| |statement(s)2|   |statement(s)n| |statement(s)n+1|
    --------------- ---------------   --------------- -----------------
           |               |                 |               |
           ---------------------------------------------------
                                   |
                                   V

    Note:
    (1) The expression and constants can be type char, int, or long.
    (2) You can use multiple case labels for a given set of statements.
    (3) If you omit the break at the end of a case it falls into the
        next case.
    (4) (a) If your constants are grouped closely together you should
            use a switch statement.
        (b) If your constants are far apart you should use an if-else-if
            statement.
        This is because a switch statement is usually implemented with
        a jump table.  This is more efficient than an if-else-if at
        run time.  However, a switch with constants that are far apart
        generates a large jump table with few valid entries, which
        wastes memory.              

26. Example 7.17
    ------------
    Program #1:
    /* switch1.c -- switch */
    #include <stdio.h>
    int main(void)
    {
      char ch;

      /* switch: character expression and no default */
      while (1)
      {
        /* prompt user for input and get input */
        printf("Please enter a character.  Type q to quit.\n");
        scanf(" %c", &ch);

        /* handle quit case */
        if (ch == 'q')
          break;

        /* process input */
        switch (ch)
        {
          case 'a':
            printf("Doing action for 'a'.\n");
            break;
          case 'b':
            printf("Doing action for 'b'.\n");
            break;
          case 'c':
            printf("Doing action for 'c'.\n");
            break;
        }
      }
      return 0;
    }
  
    Screen #1:
    Output: Please enter a character.  Type q to quit.
    Input:  a
    Output: Doing action for 'a'.
    Output: Please enter a character.  Type q to quit.
    Input:  b
    Output: Doing action for 'b'.
    Output: Please enter a character.  Type q to quit.
    Input:  c
    Output: Doing action for 'c'.
    Output: Please enter a character.  Type q to quit.
    Input:  d
    Output: Please enter a character.  Type q to quit.
    Input:  q

    Program #2:
    /* switch2.c -- switch */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /* switch: integer expression and default */
      while (1)
      {
        /* prompt user for input */
        printf("Please enter an integer.  Type q to quit.\n");

        /* get input and handle quit case */
        if (scanf("%d", &num) != 1)
          break;

        /* process input */
        switch (num)
        {
          case 1:
            printf("Doing action for 1.\n");
            break;
          case 2:
            printf("Doing action for 2.\n");
            break;
          case 3:
            printf("Doing action for 3.\n");
            break;
          default:
            printf("Doing action for all others.\n");
            break;
        }
      }
      return 0;
    }

    Screen #2:
    Output: Please enter an integer.  Type q to quit.
    Input:  1
    Output: Doing action for 1.
    Output: Please enter an integer.  Type q to quit.
    Input:  2
    Output: Doing action for 2.
    Output: Please enter an integer.  Type q to quit.
    Input:  3
    Output: Doing action for 3.
    Output: Please enter an integer.  Type q to quit.
    Input:  4
    Output: Doing action for all others.
    Output: Please enter an integer.  Type q to quit.
    Input:  q

    Program #3:
    /* switch3.c -- switch */
    #include <stdio.h>
    int main(void)
    {
      char ch;

      /* switch: character expression, default, and multiple case
                 labels */
      while (1)
      {
        /* prompt user for input and get input */
        printf("Please enter a character.  Type q to quit.\n");
        scanf(" %c", &ch);

        /* handle quit case */
        if (ch == 'Q' || ch == 'q')
          break;

        /* process input */
        switch (ch)
        {
          case 'A':
          case 'a':
            printf("Doing action for 'A' and 'a'.\n");
            break;
          case 'B':
          case 'b':
            printf("Doing action for 'B' and 'b'.\n");
            break;
          case 'C':
          case 'c':
            printf("Doing action for 'C' and 'c'.\n");
            break;
          default:
            printf("Doing action for all others.\n");
            break;
        }
      }
      return 0;
    }

    Screen #3:
    Output: Please enter a character.  Type q to quit.
    Input:  A
    Output: Doing action for 'A' and 'a'.
    Output: Please enter a character.  Type q to quit.
    Input:  a
    Output: Doing action for 'A' and 'a'.
    Output: Please enter a character.  Type q to quit.
    Input:  B
    Output: Doing action for 'B' and 'b'.
    Output: Please enter a character.  Type q to quit.
    Input:  b
    Output: Doing action for 'B' and 'b'.
    Output: Please enter a character.  Type q to quit.
    Input:  C
    Output: Doing action for 'C' and 'c'.
    Output: Please enter a character.  Type q to quit.
    Input:  c
    Output: Doing action for 'C' and 'c'.
    Output: Please enter a character.  Type q to quit.
    Input:  D
    Output: Doing action for all others.
    Output: Please enter a character.  Type q to quit.
    Input:  d
    Output: Doing action for all others.
    Output: Please enter a character.  Type q to quit.
    Input:  Q

    Program #4:
    /* switch4.c -- switch */
    #include <stdio.h>
    int main(void)
    {
      int num;

      /* switch -- integer expression, no default, and case without
                   break */
      /* if you omit the break at the end of a case it falls into the
         next case */
      while (1)
      {
        /* prompt user for input */
        printf("Please enter an integer.  Type q to quit.\n");

        /* get input and handle quit case */
        if (scanf("%d", &num) != 1)
          break;

        /* process input */
        switch (num)
        {
          case 1:
            printf("Doing action for 1.\n");
            break;
          case 2:
            printf("Doing action for 2.\n");
          case 3:
            printf("Doing action for 3.\n");
            break;
        }
      }
      return 0;
    }

    Screen #4:
    Output: Please enter an integer.  Type q to quit.
    Input:  1
    Output: Doing action for 1.
    Output: Please enter an integer.  Type q to quit.
    Input:  2
    Output: Doing action for 2.
    Output: Doing action for 3.
    Output: Please enter an integer.  Type q to quit.
    Input:  3
    Output: Doing action for 3.
    Output: Please enter an integer.  Type q to quit.
    Input:  4
    Output: Please enter an integer.  Type q to quit.
    Input:  q
	
27. Example 7.18
    ------------
    Problem:
    Write a program that prints animal names.

    Program:
    /* animals.c -- prints animal names */ 
    #include <stdio.h>
    int main(void)
    {
      char letter;

      printf("This program prints animal names.\n");
      while (1)
      {
        printf("\nPlease enter the first letter.  Type # to quit.\n");
        scanf(" %c", &letter);
        if (letter == '#')
          break;
        switch (letter)
        {
          case 'A':
          case 'a':
            printf("Argali: a wild sheep of Asia.\n");
            break;
          case 'B':
          case 'b':
            printf("Babirusa: a wild pig of Malay.\n");
            break;
          case 'C':
          case 'c':
            printf("Coati: racoonlike mammal.\n");
            break;
          case 'D':
          case 'd':
            printf("Desman: aquatic, molelike critter.\n");
            break;
          case 'E':
          case 'e':
            printf("Echidna: the spiny anteater.\n");
            break;
          default:
            printf("That's a stumper!\n");
            break;
        }
      }
      return 0;
    }

    Screen:
    Output: This program prints animal names.
    Output:
    Output: Please enter the first letter.  Type # to quit.
    Input:  B
    Output: Babirusa: a wild pig of Malay.
    Output:
    Output: Please enter the first letter.  Type # to quit.
    Input:  d
    Output: Desman: aquatic, molelike critter.
    Output:
    Output: Please enter the first letter.  Type # to quit.
    Input:  F
    Output: That's a stumper!
    Output:
    Output: Please enter the first letter.  Type # to quit.
    Input:  #
 



1



