X++, C# Comparison: Loops
Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012
This topic compares the loop features between X++ and C#.
X++ to C# Comparisons
Similarities
The following features are the same in X++ and C#:
Declarations for variables of the int primitive data type. Declarations for other primitive types are almost the same, but the types might have different names.
while statement for loops.
break statement to exit a loop.
continue statement to jump up to the top of a loop.
<= (less than or equal) comparison operator.
Differences
The following table lists X++ features that are different in C#.
Features |
X++ |
C# |
Discussion |
---|---|---|---|
The for statement. |
The for statement is available for loops. |
The C# for statement is slightly different from for in X++. |
In C# you can declare the counter integer in the for statement. But in X++ the counter must declared outside the for statement. |
++ increment operator. |
An ++ increment operator is available in X++. But an int variable that is decorated with ++ can only be used as a statement, not as an expression. For example, the following lines of X++ code would not compile: int age=42; print age++; However, the following lines of X++ code would compile: int age=42; age++; print age; |
The C# ++ operator is more flexible than in X++. |
The following lines of code are the same in both languages:
But the following lines of code have a different effect from each other, and are valid only in C#:
|
modulo operator. |
In X++ the modulo operator is mod. |
In C# the modulo operator is %. |
The symbols for the modulo operator are different, but their behavior is the same in both languages. |
Temporarily suspend a console program that has already begun. |
The pause statement. |
In C#, a command line program can be paused by the following line of code:
|
In X++ you continue by clicking an OK button on a modal dialog box. In C# you continue by pressing any keyboard on the keyboard. |
Display a message. |
In X++, the print statement displays a message in the Print window. |
In C# a message can be displayed on the console by the following line of code:
|
The X++ print function is used only when you test. An X++ program that uses print almost always uses the pause statement somewhere later in the code. For production X++ code, use the Global::info Method instead of print. The strfmt function is often used together with info. There is no reason to use pause after info. |
Make a sound. |
The beep function makes a sound that you can hear. |
In C# a sound that you can hear is issued by the following line of code:
|
The statements each produce a short tone. |
Print and Global::info
The X++ code samples in this topic use the print function to display results. In X++ you can use the print statement can display any primitive data type without having to call functions that convert it to a string first. This makes print useful in quick test situations. Generally the Global::info method is used more often than print. The info method can only display strings. Therefore the strfmt function is often used together with info.
A limitation of print is that you cannot copy the contents of the Print window to the clipboard (such as with Ctrl+C). Global::info writes to the Infolog window which does support copy to the clipboard.
Example 1: The while Loop
The while keyword supports looping in both X++ and C#.
X++ Sample of while
static void JobRs002a_LoopsWhile(Args _args)
{
int nLoops = 1;
while (nLoops <= 88)
{
print nLoops;
pause;
// The X++ modulo operator is mod.
if ((nLoops mod 4) == 0)
{
break;
}
++ nLoops;
}
beep(); // Function.
pause; // X++ keyword.
}
Output
The output in the X++ Print window is as follows:
2
3
4</pre>```
### ![Cc967439.collapse\_all(en-us,AX.60).gif](images/Gg863931.collapse_all(en-us,AX.60).gif "Cc967439.collapse_all(en-us,AX.60).gif")C\# Sample of while
``` csharp
using System;
public class Pgm_CSharp
{
static void Main( string[] args )
{
new Pgm_CSharp().Rs002a_CSharp_ControlOFlowWhile();
}
void Rs002a_CSharp_ControlOFlowWhile()
{
int nLoops = 1;
while (nLoops <= 88)
{
Console.Out.WriteLine( nLoops.ToString() );
Console.Out.WriteLine( "(Press any key to resume.)" );
// Paused until user presses a key.
Console.In.Read();
if ((nLoops % 4) == 0) break;
++ nLoops;
}
Console.Beep();
Console.In.Read();
}
}
Output
The console output from the C# program is as follows:
>> Rosetta_CSharp_1.exe
1
(Press any key to resume.)
2
(Press any key to resume.)
3
(Press any key to resume.)
4
(Press any key to resume.)</pre>```
## Example 2: The for Loop
The for keyword supports looping in both X++ and C\#.
### ![Cc967439.collapse\_all(en-us,AX.60).gif](images/Gg863931.collapse_all(en-us,AX.60).gif "Cc967439.collapse_all(en-us,AX.60).gif")X++ Sample of for
In X++ the counter variable cannot be declared as part of the for statement.
```X++
static void JobRs002a_LoopsWhileFor(Args _args)
{
int ii; // The counter.
for (ii=1; ii < 5; ii++)
{
print ii;
pause;
// You must click the OK button to proceed
// beyond a pause statement.
// ii is always less than 99.
if (ii < 99)
{
continue;
}
print "This message never appears.";
}
pause; // X++ keyword.
}
Output
The output in the X++ Print window is as follows:
2
3
4</pre>```
### ![Cc967439.collapse\_all(en-us,AX.60).gif](images/Gg863931.collapse_all(en-us,AX.60).gif "Cc967439.collapse_all(en-us,AX.60).gif")C\# Sample of for
``` csharp
using System;
public class Pgm_CSharp
{
static void Main( string[] args )
{
new Pgm_CSharp().Rs002a_CSharp_ControlOFlowFor();
}
void Rs002a_CSharp_ControlOFlowFor()
{
int nLoops = 1,
ii;
for (ii = 1; ii < 5; ii++)
{
Console.Out.WriteLine(ii.ToString());
Console.Out.WriteLine("(Press any key to resume.)");
Console.In.Read();
if (ii < 99)
{
continue;
}
Console.Out.WriteLine("This message never appears.");
}
Console.Out.WriteLine("(Press any key to resume.)");
Console.In.Read();
}
}
Output
The console output from the C# program is as follows:
(Press any key to resume.)
2
(Press any key to resume.)
3
(Press any key to resume.)
4
(Press any key to resume.)
(Press any key to resume.)</pre>```
## See also
[X++, C\# Comparisons](x-csharp-comparisons.md)
[Loops](loops.md)
[Do...while Loops](do-while-loops.md)
[Operators](operators.md)
[X++ Keywords](x-keywords.md)
**Announcements:** New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the [MS Press Store](https://www.microsoftpressstore.com/store/inside-microsoft-dynamics-ax-2012-r3-9780735685109).