May 28


Learn C# for RPG ILE Programmers

By NickLitten

May 28, 2017

c, learn, programming

If you are an RPG Programmer thern you can quite easily add C# to the list of progamming languages you have in your brainbox. It’s a very similar syntax.

Checkout the latest C# For RPGers TechTip and check out the similarities between C# and RPG loops:


The missing piece to the basic C# language puzzle will be presented here: looping structures. You already know FOR, but there’s more!

few TechTips ago, I talked about the FOR loop and how it works in C#. It’s now time to explain the other looping structures that this amazing language has to offer. I’ll be using the exact same test task in all of the code snippets: I’ll print a simple count from 1 to 5 using a loop.

Meet DoW’s Cousin: WHILE

Let’s start with the WHILE loop. This looping structure is very similar to its RPG counterpart, DoW: the statement starts with the looping instruction, which is followed by the condition and a block of code to execute. Naturally, there are syntactical differences, but the general ideal is exactly the same. Here’s the code snippet necessary to perform my test task using the WHILE loop:

            int myNumber = 1;

            while(myNumber <= 5)


                // print out myNumber's current value


                // add 1 to myNumber



As you can see, if you replace while with DoW, remove the opening curly bracket, and replace the closing one with an EndDo, the C# code becomes its RPG equivalent. Well, almost, because there’s no Console.WriteLine in RPG, but you get the idea. There’s also that “++” notation that you might not be used to, but that’s equivalent to myNumber = myNumber + 1.

As expected, this will print the numbers from 1 to 5 in the console or, in other words, the code within the loop will be executed while the condition evaluates to true. More formally, DoW and WHILE belong to the “check first and execute if true” looping structure family.

Do First, Check Later: the Do…While Loop

As you know, even though DoW is arguably the most used RPG loop, it’s not the only one. There’s also DoU, with its “execute first and then check if true” modus operandi. As you’d expect, DoU also has a C# cousin. It’s the DO…WHILE loop. RPG’s DoU syntax is similar to DoW, but the same doesn’t apply to WHILE and DO…WHILE in C#. I actually think C#’s DO…WHILE syntax makes more sense, because it follows the order in which things get done. Let’s go over an example, and you’ll see what I mean:

            int anotherNumber = 1;



                // print out anotherNumber's current value


                // add 1 to anotherNumber


            } while (anotherNumber <= 5);

It’s easier on the eyes than DoU because the operations are in their natural order: “do this while the condition is true”, as opposed to DoU’s “until the condition is false, do this.” Other than that syntactical difference, DO…WHILE and its RPG counterpart DoU offer the same functionality.

And There’s One More: FOR

Then there’s also the FOR loop and its two variants, which I discussed in depth a few TechTips ago (you can read them here and here), so I won’t repeat it here. Just for consistency’s sake, here’s the same example using FOR:

            for(int yetAnotherNumber = 1; yetAnotherNumber <= 5; yetAnotherNumber++)




FOR’s advantage in this particular context is that the counter variable is defined in the loop’s definition and you don’t need to declare an additional variable or increment it in each iteration. The looping mechanism does that for you.

As I mentioned earlier, there’s also another FOR variant, known as For Each, which is particularly useful to loop over a certain family of objects called “collections.”

Next Steps

Speaking of objects, that would be the next logical step in this series, because this TechTip about the looping structures closes the basic language syntax. I’ve discussed data types and their (arithmetic and other) operations, flow control structures, and (this time around) looping structures. In the future, I hope to get the chance to explain object-oriented programming, C#’s rich graphical user interface, and how to manipulate IBM i’s database from a C# program. I firmly believe that opening up the i’s database to “external” languages (like C#) is one way to go, because it keeps the platform alive, without being held hostage by RPG and its limitations, particularly now that IBM seems to be directing its R&D investment to SQL rather than RPG.

I hope I managed to help you learn and appreciate something new, something outside your comfort zone, yet strangely familiar. Thank you all for reading!

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join the IBM i Community for FREE Presentations, Lessons, Hints and Tips