Wednesday 19 September 2012

Comparison between IF and SWITCH statement in Axapta

X++ code for IF and SWITCH


IF Statement

static void Job1(Args _args)
{
    int itemid = 1001;

    if (itemid == 3005)
    {
        print "Product 3005";
    }
    else if (itemid == 5001)
    {
        print "Product 5001";
    }
    else if ((itemid == 1001) || (itemid == 1002) || (itemid == 1003)
    {
        print "Product in range of 1001-1003";
    }
    else
    {
        print "Product nothing";
    }
    pause;
}
SWITCH Satement
Required break otherwise depends on the value it may select appropriate case, so break exit the pointer out of the switch case statment, you can see switch statement work batter in some of the situation than if statement

static void Job2(Args _args)
{
    int itemid = 1001;
    switch (itemid)
    {
        case 3005:
            print "Product 3005";
            break;
        case 5001:
            print "Product 5001";
            break;
        case 1001, 1002, 1003:                                  // it work like 1001 OR 1002 OR 1003
            print "Product in range of 1001 - 1003";
            break;
        default:
            print "Product nothing";
            break;
    }
    pause;
}

No comments:

Post a Comment