Dot Net Fluke: Getting by on C# for iSeries RPG Developers

Useful tutorials on C# and .NET for RPG iSeries AS/400 developers. Brought to you from the folks at AranRock Consulting

2/11/08

Comparing Loops in RPG and C#

Looping operations are quite similar in C# and RPG. Note that RPG uses 'EndDo' or 'EndFor' and C# uses curly braces {} as the beginning and end of a loop (or any code block for that matter).

Here's the different loops
A = 0; B=10
'Statements' is all the code in the loop that you wish to iterate over.

RPG Loop C# Loop
DOW (A<b); statements EndDo; While (a <b)  {statements}
FOR A = 1 to 10;  statements  EndFor; For (int a=0; a<10; a++)  {statements}
DOU A > B; statements  EndDo; Do   {statements}
while (A >B);
   

 

Both languages have ways to skip statements or break out of the loop

iter; Continue;
Leave; break;

 

Here's sample code showing the C# while loop (same as RPG DOW)
Note that the While operation code has {} braces to denote the beginning and end of the loop.

The Do While loop...

            // The while loop
// The while statement is equivalent to the RPG 'DOW'
int ebayJunk = 0;
int CreditLeft = 500;
bool HaveMoney = true;
// The loop will execute as long as HaveMoney is true;
while (HaveMoney) // checks before going into the loop
{ // beginning of while loop
ebayJunk += 1;
CreditLeft -= 100;
Console.WriteLine("Useless Gadgets={0}", ebayJunk);
Console.WriteLine("Money left=${0}", CreditLeft);

if (CreditLeft <= 0) { HaveMoney = false; } // Set condition for while loop
// Entire loop is executed a
} // end of while loop (same as EndDO in RPG
Console.WriteLine("You're Broke!!");
Console.ReadLine();
And the For loop....
// For loop example. Finds the first blank space in a string
// The block of code between the curly braces gets iterated
// over. The loop decrements from the length



String field = "To the Galaxy and Beyond";



for(int i = field.Length; i > 0; i--)


{
Console.WriteLine("Letter is not blank: "+ field[i - 1]);



if(field[i - 1] == ' ')   // note how I can treat a string like an array


    break; // exit once I reach a blank space


}


 














Share this post :

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home