Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Note
Community interest groups have now moved from Yammer to Microsoft Viva Engage. To join a Viva Engage community and take part in the latest discussions, fill out the Request access to Finance and Operations Viva Engage Community form and choose the community you want to join.
This article describes loop statements in X++.
There are three loop statements: for, while, and do...while. A loop repeats its statement until the condition that you set for the loop is false. Within the loop statements, you can use break and continue statements.
for loops
The syntax of a for loop is:
for ( initialization ; test ; increment ) { statement }
The for loop repeatedly executes statement for as long as the conditional expression test is true. statement can be a block of statements. The body of the for loop (statement) might be executed zero or more times, depending on the results of test.
A for loop differs from other loops because you can assign an initial value to a control variable, and because there's a statement for incrementing or decrementing the variable. These additions make a for loop especially useful for traversing lists, containers, and arrays because they have a fixed number of elements.
You can also apply a statement to each element and increment your way through the elements, setting the condition to test for the last element.
Example of a for loop
In the following code example, the items in an array of integers are printed.
int integers[10];
for (int i = 0; i < 10; i++)
{
info(int2str(integers[i]));
}
// The output is a series of 0's.
while loops
The syntax of a while loop is:
while ( expression ) statement
A while loop repeatedly executes statement for as long as the conditional expression is true. You can replace statement with a block of statements. The loop executes statement as many times as the condition is met (zero to many).
Example of a while loop
The following code example demonstrates a while loop that traverses a container and prints out the contents of the container.
container cont = ["one", "two", "three"];
int no = 1;
while (no <= conlen(cont))
{
info(conPeek(cont, no));
no++;
}
// The output is "one", "two", "three".
do...while loops
The syntax of the do...while loop is:
do { statement } while ( expression ) ;
The do...while loop is similar to the while loop, but the condition appears after the statement that must be executed. statement can be a block of statements. The statement is always executed at least one time, because the condition is tested after statement is executed. The do...while loop is well-suited to tasks that must always be done at least one time, such as getting parameters for a report.
Example of a do...while loop
The following code example finds the smallest power of 10 that is larger than realNumber.
int FindPower(real realNumber)
{
int exponent = -1;
real curVal;
do
{
exponent++;
curVal = power(10, exponent);
}
while (realNumber > curVal);
return exponent;
}
continue statement
The continue statement causes execution to move directly to the next iteration of a for, while, or do...while loop. For do or while, the test is executed immediately. For a for statement, the increment step is executed.
Example of a continue statement
In the following code example, if Iarray[i] <= 0, the loop doesn't execute the remaining statements. The loop increments i before it tries the if statement again.
int Iarray[100];
for (int i = 0; i < 100; i++)
{
if (Iarray[i] <= 0)
{
Info("Will continue.");
continue;
}
info("Did not continue.");
}
// The output is "Will continue." for all 100 interations.
break statement
Use the break statement within a loop to terminate that loop. Execution then moves to the first statement after the loop.
Example of a break statement
This example uses a break statement within a while loop. When used within a loop, the break statement terminates the loop and execution continues from the statement following the loop. This behavior works for do... while and for loops as well.
var mainMenu = SysDictMenu::newMainMenu();
var enum = mainMenu.getEnumerator();
var found = false;
while (enum.moveNext())
{
var menuItem = enum.current();
if (menuItem.label() == "StringOfInterest")
{
found = true;
break;
}
}
if (found)
{
// do something
}