Executes one or more blocks of statements.


       SWITCH SwitchExp
          CASE LiteralExp1
         [CASE LiteralExp2]
         [CASE LiteralExpn]
       END [SWITCH]


The <LiteralExp> must be a compiled time resolvable numeric or character expression, and may involve operators, as long as such operators involve compile time static value.

CASE <LiteralExp1> .. <LiteralExp> is a constant value of the same data type as <SwitchExp>.

The EXIT optional statement is the equivalent of the C statement break, and if present, execution of the SWITCH structure will end when the EXIT statement is reached, otherwise it will continue with the first statement below the next CASE statement (fall through)


SWITCH is a Harbour construct inspired by the C implementation of switch().

The SWITCH statement compares a constant value against a series of constant values. It is similar to the DO CASE statement but outperforms it to a great extent due to the restrictions introduced with values permitted for comparison. As a general rule, only literal values in form integer or character constants may follow the CASE clauses.

The SWITCH statement evaluates <SwitchExp> and then searches for a first match between the resulting value and <LiteralExp>. When a match is found, the statements following the corresponding CASE clause are executed down to the END statement. To suppress execution of statements of the next CASE clause, the EXIT statement must be explicitely used. This is a major difference to the DO CASE statement where subsequent CASE clauses are skipped once a first match is found.

If no initial match is found with the CASE clauses, the statements following OTHERWISE are executed, if present.


        // The example demonstrates the SWITCH control structure
        // Try with and without EXIT statements
        PROC Main()
           LOCAL cSName := "Doe"
           SWITCH cSName

              CASE "Abba"
                 ? "Abaneoer"

              CASE "Bo"
                 ? "Boeing"
              CASE "Doe"
                 ? "John"

                 ? "Guest"

            ?? '', cSName
           WAIT "EOF SwitchTest.prg"



One response to “SWITCH

  1. Pingback: Harbour Statements | Viva Clipper !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.