Share via


if and if ... else Statements

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

The if statement evaluates a question (a condition) and executes a statement or set of statements if the condition is true. You can provide an alternative statement or set of statements that are executed if the condition is false (the else statement).

if and if...else statements are the most simple conditional statements in X++. You can also use switch statements and ternary operators.

You can nest if statements, but if the nesting of if statements becomes too deep, consider using a switch statement.

Syntax

if ( expression ) statement [ else statement ]

Both statements can be compound statements. The expression in the parentheses (the condition) can be any valid expression that is evaluated to true or false. (All numbers different from zero are true; all non-empty strings are also true).

Examples

Without else

With else

if (a>4)

{

print a;

}

if (a>4)

{

print a;

}

else

{

print "a is less than or equal to 4";

}

See also

Comparison of if and switch Statements

Switch Statements

Ternary Operator (?)

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