[IMAGE]
[IMAGE][IMAGE]
Andrew Borts
[IMAGE]
[IMAGE]
When was the last time you wanted to write a subroutine in your CL program to cut down the amount of code you're writing? Or use a logic more complex then "" within your CL program? When was the last time you wanted to loop through something without the use of 's and 's in the clunkiest use of code you've seen in a while. For me, it's daily. Who wants to clean up some code so our business rules and our code look related?
Starting in V5R3, there were significant changes to the organization of CL programming languages:
- New looping methods
- Until loop – DOUNTIL (ENDDO)
- For loop – DOFOR (ENDDO)
- While Loop – DOWHILE (ENDDO)
- Logical end and restart of loops
- ITERATE (same as RPG ITER)
- LEAVE – (same as RPG LEAVE)
- Multiple file declaration (V5R3)
- RCVF for up to five files
- Flow changes
- SELECT when
- Subroutines (V5R4)
DO/ENDDO vs DOUNTIL
Let me show you what you've been missing. In our old routines, we needed to group commands together using DO/ENDDO
Again:
In a word: Yuck. What if this looked like this?
Program flow is cleaned up. checks the logic after the first iteration so we'll try and read the file, and leave the loo...
To continue reading for free, register below or login
To read more you must become a member of Search400.com
');
// -->

p when the file is completed.
Controlling patterns using DOWHILE
You can control the patterns of your CL programs using as follows:
I'm never going to perform the loop unless JHANCOCK is found. Once I reach the end of file in all of these examples, I leave the loop. I can pop back up to the top of the loop if my logic requires me to start with, for example, the next record in the file.
Setting the number of loops
is for looping a set amount of times, then falling out of the loop. Say if you need to do something 15 times:
The static values can be substituted for field names – from to is a valid loop as well.
The examples I gave above also show how to uniquely identify up to five files – display or database files.
Using SELECT/WHEN/END/SELECT to clean up programs
So how many of our programs look like this?
A little ugly. So we can clean it up:
The grouping of code above may be a bit exaggerated. But, you can see that using the your code looks so much easier to understand.
Using CL subroutines
Subroutines clean up the code even more.
You see that subroutine is called AAA is in the prefix of the field &FILENM. (%SST is the substring command.)
Using this technique, your code will clean up, and you'll be writing shorter and shorter CL programs in no time.