Compartir a través de


Switch Statements

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

The switch statement is a multi-branch language construct. You can create more than two branches by using the switch statement. This is in contrast to the if statement. You have to nest statements to create the same effect.

The general format of a switch statement is as follows.

switch ( Expression )

{

case  Constant :

 Statement ;

break;

...

default:

 Statement ;

break;

}

The switch expression is evaluated and checked against each of the case compile-time constants. If a constant matches the switch expression, the case statement is executed. If the case also contains a break statement, the program then jumps out of the switch. If there is no break statement, the program continues evaluating the other case statements.

If no matches are found, the default statement is executed. If there are no matches and no default, none of the statements inside the switch are executed.

Each of the previous Statement lines can be replaced with a block of statements by enclosing the block in {...} braces.

Syntax

Switch statement = switch (  expression ) { {case } [ default:  statement  ] }

case = case  expression  { ,  expression  } :  statement

Examples

switch (Debtor.AccountNo)

{

case "1000" :

do_something;

break;

case "2000" :

do_something_else;

break;

default :

default_statement;

break;

}

It is possible to make the execution drop through case branches by omitting a break statement. For example:

switch (x)

{

case 10:

a = b;

case 11:

c = d;

break;

case 12:

e = f;

break;

}

Here, if x is 10, b is assigned to a, and d is assigned to c, the break statement is omitted after the case 10: statement. If x is 11, d is assigned to c. If x is 12, f is assigned to e.

See also

Comparison of if and switch Statements

if and if ... else Statements

Ternary Operator (?)

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.