title | description | author | ms.author | ms.topic | ms.date | ms.reviewer | audience | ms.search.region | ms.search.validFrom | ms.dyn365.ops.version | ms.devlang |
---|---|---|---|---|---|---|---|---|---|---|---|
X++ loop statements |
Learn about loop statements in X++, including outlines and examples for the for, while, and do...while loop statements. |
josaw1 |
josaw |
article |
06/17/2019 |
johnmichalak |
Developer |
Global |
2016-02-28 |
AX 7.0.0 |
xpp |
[!include banner]
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 is set for the loop is false. Within the loop statements, you can use break and continue statements.
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 an initial value can be assigned to a control variable, and because there is 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.
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.
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. statement can be replaced by a block of statements. statement is executed as many times as the condition is met (zero to many).
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".
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.
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;
}
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.
In the following code example, if Iarray[i] <= 0
, the remaining statements in the loop are not executed, and i
is incremented before the if statement is tried 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.
The break statement within a loop is used to terminate that loop. Execution then moves to the first statement after the loop.
This example uses a break statement within a while loop. When used within a loop, the loop is terminated and execution continues from the statement following the loop. This 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
}
[!INCLUDEfooter-include]