Comparison of if and switch Statements
Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012
With X++ there are situations where the switch statement can be a good alternative to the if statement.
Example 1
When you need your X++ code to branch based on one of several values of a single variable, a switch statement can be a less verbose alternative to the if statement. The following table contains an example of each statement.
if statement |
switch statement |
---|---|
|
|
Example 2
When your X++ code must branch based on groups of values for a variable, the switch statement can be a less verbose alternative to the if statement. The following table contains an example of each statement.
if statement |
switch statement |
---|---|
|
|
break Statement
If you do not use the break statement, the program flow in the switch statement continues to the next case. The two code segments in the following table have the same behavior.
break omitted |
Values are comma-delimited |
---|---|
|
|
See also
Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.