[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
switch                       switch Statement



 switch ( expression_list ) {
     [local ...]
     case expr_list:
         [statement;
           .
           .
           .}
         [break;]
     [default:
         statement;
           .
           .
           .}
         break;]
 }

    switch  provides   a  multi-point   branch  based   on  the   value  of
    `expression_list'.  switch  evaluates  `expression_list',  then  passes
    control to the case that matches `expression_list'.  If no case matches
    the value of expression_list, control  is passed to the default  label,
    if one exists.  Otherwise, control is passed to the statement following
    the switch.

    The QTAwk, switch is an expanded form of the C switch/case  statements.
    In  C,  a  case  label  must  be  an  integer or character compile time
    constant or a constant expression and the switch expression list QTAwk,
    the case  label may  contain any  valid QTAwk  expression or expression
    list:

    The  expression_lists  of  the  case  labels  are  evaluated in turn at
    execution time.   The resultant value  is checked against  the value of
    the switch expression_list using the following logic.

         if ( case_label a regular expression ) switch_expr ~~ case_label;
           else switch_expr == case_label;

    Thus if a case label is a regular expression, a match operation,  '~~',
    is performed.   If  a case  label is  a string,  a string comparison is
    performed.   If a  case label  is a  numeric, a  numeric comparison  is
    performed.  It is possible to have case labels with differing types  in
    the same switch statement and the proper comparison is made.


      Notes:    case labels must be unique.

                A case label may evaluate to a regular expression, a string
                or a numeric.

                When control is passed to a case label, execution continues
                on through the statements  following that label.   Be aware
                that execution does not automatically end at the next  case
                label,  but  continues  on  until  either  a  break, return
                statement  (or  the  end   of  the  switch  statement)   is
                encountered.  For this reason, you may wish to put a  break
                after each case's  statements--unless, of course,  you want
                to flow into the next case.  In falling through, case label
                expressions are not evaluated.

                Multiple case labels may prefix the the same statement.

                The optional `default:'  label need not be the last label
                in a switch.

  -------------------------------- Example ---------------------------------

           while (TRUE) {
               switch ( expr_list ) {
                   case 'a':
                   case 'A':
                       statement1;
                       break;

                   case 'd':
                   case 'D':
                       statement2;
                       break;

                   .
                   .
                   .


                   case /[A-Za-z]+/: # test for Alphabetic
                       statement3;
                       exit 2;

                   case /[0-9]+/: # test for numeric
                       statement3;
                       exit 2;

                   default:
                       error("Don't recognize that expression.");
                       break;
                   }
           }

See Also: break return
This page created by ng2html v1.05, the Norton guide to HTML conversion utility. Written by Dave Pearson