Share via


Booleans

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

The boolean data type contains a value that evaluates to either true or false. You can use the X++ reserved literals true and false where ever a Boolean expression is expected.

Boolean expressions are also named logical expressions.

Boolean Values are Represented as Integers

In X++ the internal representation of a boolean is an integer. You can assign any integer value to a variable declared of type boolean. The integer value 0 (zero) evaluates to false, and all others evaluate to true.

The X++ literal false is the integer value 0, and true is 1.

The following table lists several expressions and indicates whether they evaluate to true or false.

Expression

Boolean value

1

True

44

True

true

True

(false == 0)

True

(true == 1)

True

(true == 8)

False

false

False

0

False

Declaring Booleans

Boolean declaration

=

boolean variable { , variable } ;

variable

=

identifier [ option ]

option

=

Arrayoptions | initialization

    // Simple declaration of a boolean variable, b
    boolean b; 
     
    // Multiple declaration
    boolean b1,b2;
     
    // Boolean variable is initialized to true 
    boolean b3 = true; 
     
    // Declares a dynamic array of Booleans
    boolean b4[]; 

Automatic Conversions

Because the internal representation of a boolean is an integer, boolean values are automatically converted into integers and reals.

Using Booleans in Expressions

You usually use Booleans in conditional statements, or as parts or results of logical expressions. The following example shows both.

    void main()
    {
        //Declares a boolean called exprValue
        boolean exprValue; 
     
        //Assigns ExprValue the truth value of (7*6 == 42)
        exprValue = (7*6 == 42);
        ;
     
        if (exprValue)
        { 
            print "OK";
        } 
    }

Here, the variable exprValue contains the value true, because 7*6 is equal to 42; so, the expression is true. The conditional statement is true; the word "OK" is displayed on the screen.

Overview of Booleans

Keyword

boolean

Size of data type

Byte

Scope of data type

false (0) and true (1)

Default value

false

Implicit conversions

Automatically converted to int, date, and real

Explicit conversions

none

See also

Data Types in X++

Variables

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