![]() ![]() These include the Pascal family (Object Pascal, Modula, Oberon, Ada, etc.) as well as PL/I, modern forms of Fortran and BASIC dialects influenced by Pascal, most functional languages, and many others. In many languages, only the matching block is executed, and then execution continues at the end of the switch statement. This distinction is referred to as the treatment of fallthrough, which is elaborated below. The second form are unstructured switches, as in C, where the cases are treated as labels within a single block, and the switch functions as a generalized goto. This functions as a generalized if–then–else conditional, here with any number of branches, not just two. The first form are structured switches, as in Pascal, where exactly one branch is taken, and the cases are treated as separate, exclusive blocks. Semantically, there are two main forms of switch statements. In others, like PL/I, an error is raised. In some languages, such as C, if no case matches and the default is omitted the switch statement simply exits. This executes when none of the other cases match the control expression. ![]() In many languages, every case must also be preceded by a keyword such as case or when.Īn optional default case is typically also allowed, specified by a default, otherwise, or else keyword. The value (or list/range of values) is usually separated from the corresponding statement sequence by a colon or by an implication arrow. In some languages, e.g., Rexx, no control expression is allowed and each alternative begins with a WHEN clause containing a boolean expression and a match occurs for the first case for which that expression evaluates to true.Įach alternative begins with the particular value, or list of values (see below), that the control variable may match and which will cause the control to goto the corresponding sequence of statements.This usage is similar to the if/then/elseif/else structures in some other languages, e.g., Perl. In some languages, e.g., PL/I, the control expression is optional if there is no control expression then each alternative begins with a WHEN clause containing a boolean expression and a match occurs for the first case for which that expression evaluates to true. ![]()
0 Comments
Leave a Reply. |