Switch Statement Example
Previous Topic  Next Topic 

Below is an example of a simple switch statement which is checking for which DTMF digits have been entered:


// Switch example using Aculab speech functions from CXACUDSP.DLL

switch (Digit)

        case "1":

                SMplay(vox_chan, "one.vox");

        case "2":

                SMplay(line, "two.vox");

        case "#":

                SMplay(line, "pound.vox");

        default:

                SMplay(line, "invalid.vox");

endswitch

       

There are two important things to note about the switch statement:


a) The comparison between the switch expr and the case expr values is carried out using the streq comparator NOT the == (or eq) comparator.      This means that the values are compared as strings not as integer values.      So in the following example the second case will be carried out NOT the first:


switch ("01")

    // This is actually doing a streq between "1" and "01"

    case 1:

         applog("This wont be executed");

    case "01":

         applog("This will be executed");

endswitch



b) Unlike in the C programming language you do not need to include a break_statement between each case statement.      In the TE language the case statements do NOT drop through after a case has been matched and the statement_block for that case has started execution, as soon as the next case statement is encountered  the program will jump to the endswitch statement.     In C if a break_statement is not encountered then the statement_block for the next case will be executed as well.      If you want to drop through in the TE language then you should explicitly use a goto_statement to do so.