.st0{fill:#FFFFFF;}

Upgrade old RPG ‘DO’ loops to sexy new RPGLE ‘FOR’ loops 

 January 7, 2016

By  NickLitten

Upgrading old source code — aka refactoring — is a fun past time and there are frequent little code snippets that I refactor over and over again. Some look similar but some look very different in modern language style. It’s perhaps worth documenting them in case someone else is trying to figure out alternative ways of refactoring old style RPG3 or RPG400 code. I frequently upgrade old RPG programs and ‘DO’ loops always get converted to RPGLE ‘FOR’ loops.

I just refactored an old RPG program that had a tasty little DO loop that looked like this:

RPG400

upgrade old RPG
C START DO END COUNT ... do lots of logic and stuff C ENDDO

This can be refactor to look like this:

RPG4 – also known as RPGLE /FreeFormat

For Count = Start to End;
... do lots of logic and stuff
EndFor;

I prefer the syntax and layout of FOR loops – just more readable in my brain.

Plus we have some other nice and very readable techniques to control the count up and down. Lets say we wanted to count from 1 to 200 in steps of 5

for Count = 1 by 5 to 200;

or we can do that in reverse

for Count = 200 by 5 downto 1;

NickLitten


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"cff50":{"name":"Main Accent","parent":-1},"a344d":{"name":"Accent Transparent","parent":"cff50"}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"cff50":{"val":"var(--tcb-skin-color-0)"},"a344d":{"val":"rgba(46, 138, 229, 0.85)","hsl_parent_dependency":{"h":210,"l":0.54,"s":0.78}}},"gradients":[]},"original":{"colors":{"cff50":{"val":"rgb(0, 178, 255)","hsl":{"h":198,"s":1,"l":0.5}},"a344d":{"val":"rgba(0, 178, 255, 0.85)","hsl_parent_dependency":{"h":198,"s":1,"l":0.5}}},"gradients":[]}}]}__CONFIG_colors_palette__

Get In Touch

Iā€™m always looking for awesome input, feedback and critique!

>