Statements and loops

Statements and loops are used to control the execution flow of a program and most programming tasks would be impossible without them.

In AX, you have access to the following statements and loops:

  • The for loop
  • The continue statement
  • The break statement
  • The while loop
  • The do-while loop
  • The if-else if-else loop
  • The switch statement

The for loop

A for loop can be used if the code at runtime knows how many times it should loop through a piece of code before the loop starts. The pseudocode of a for loop in AX is the same as any for loop in most programming languages derived from the C programming language. It is actually called a three-expression for loop, since it is made up of three steps:

  1. Initialization of the variable used to increment the for loop.
  2. Initialization of the test statement.
  3. Setting the increment or decrement values for the variable.

As the following example shows, the variable i is initiated to 1, then it tells the for loop to keep on looping as long as i is less than or equal to 10. Then, it increments i by one for each loop:

int i,
for (i=1; i <= 10; i++)
{
    info (strfmt("This is the %1 Toyota", i));
}


The continue statement

If you would like the code to jump straight to the next iteration of the code, you can use the continue statement inside any loop.

The break statement

The break statement can be used to jump out of the loop even when there are more iterations to execute as per the loop condition.

In the preceding example, the info function was used rather than the print function from the Hello World example. The info function is much more convenient and looks much better from a user point of view. The print window should only be used to display messages to the developers when executing a test job or something similar.

The while loop

You can use the while loop to execute a piece of code many times until a certain condition is met. The while loop will only execute if the while condition is met. Here is an example:

int carsAvailable=10;
while (carsAvailable != 0)
{
    info (strfmt("Available cars at the moment is %1", carsAvailable));
    carsAvailable --;
}

The do-while loop

The do-while loop is pretty much the same as the while loop, except that it will execute once even though the while condition is not met. In a do-while loop, the loop is executed at least once, as the while expression is evaluated after the first iteration. Here is an example:

int carsAvailable=10;
do
{
    info (strfmt("Available cars at the moment is %1", carsAvailable));
    i--;
} while (carsAvailable != 0);

The if-else if-else loop

The execution flow of an if-else if-else loop is as follows:

  1. The if statement checks to see if the condition used in the statement returns true. If it does, the system executes the body of the if statement.
  2. If the if statement returns false, it is checked if the condition inside the else-if statement returns true. If it does, the system executes the body of the else-if statement.
  3. The last else statement can be used directly with the if statement or after an else-if statement. The system will execute the body inside the last else statement only if the if statement and all the else-if statements return false.

The following is an example of the if-else if-else loop:

if (carGroup == CarGroup::Economy)
{
    info("Kia Picanto");
}
else if (carGroup == CarGroup::Compact)
{
    info("Toyota Auris");
}
else if (carGroup == CarGroup::MidSize)
{
    info("Toyota Rav4");
}
else if (carGroup == CarGroup::Luxury
{
    info("BMW 520");
}
else
{
    info("Standard cars");
}

You can of course add as many else-if statements as you'd like and you can also nest them to have another if statement inside the body of an if statement, but this might not be the best way of getting things done. Instead, you should consider using the switch statement. The reason for this is that the condition has to be evaluated for each if and else-if statement, while the switch statement evaluates the condition once and then finds the correct hit.

The switch statement

The switch statement is similar in function to the if statement; however, the switch statement syntax makes the code far more legible. Note that you have to use the break statement at the bottom of each case. If it is not used, the system will continue to execute the next case as well.

The default statement can be used in similar way as the else statement to say that if none of the other cases contained the correct value, execute the default instead. Here is an example:

switch (carGroup)
{
    case CarGroup::Economy :
        info("Kia Picanto");
        break;
    case CarGroup::Compact :
    info("Toyota Auris");
        break;
    case CarGroup::MidSize :
        info("Toyota Rav4");
        break;
    case CarGroup::Luxury :
        info("BMW 520");
        break;
    default
        info("Standard cars");
        break;
}

Exception handling

As a developer it is always important to expect the unexpected. One way of making sure that your program can handle abnormal situations is using exception handling. In AX, that means using the following statements: try, catch, throw, and retry.

The try and catch statements should always go together. Using a try statement without a catch statement or vice versa will result in a compiler error.

When you use the try statement, you are indicating that whatever code is inside the try block, it might generate an abnormal situation that should be handled. The situation is handled in the catch block by specifying what kind of exception the catch block is taking care of. The following example shows how to catch an error exception and a deadlock exception. A deadlock will never occur in this example, but it is here just to show you how you can use the retry statement:

static void ExceptionHandling(Args _args)
{
    try
    {
        // Do something
        info("Now I'm here");
        // A situation that causes an error occur and you would
        // like to stop the execution flow
        if (true)
            throw error("Oops! Something happened");
        info("Now I'm there");
    }
    catch (Exception::Error)
    {
        // Handle the error exception
        info ("I would like to inform you that an error occurred");
    }
    catch (Exception::Deadlock)
    {
        // Handle the deadlock exception
        // Wait for 10 seconds and try again
        sleep(10000); 
        retry;
    }
    info ("This is the end");
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.231.155