Thursday, July 4, 2013

SELECT in CL

IBM i (OS400) V5R3 brought what I like to call the Select group of commands, SELECT, WHEN, OTHERWISE, and ENDSELECT. If you are familiar with the Select operation codes in RPG/RPGLE you need little or no introduction to their equivalent in CL. Their introduction has allowed me to write, what I consider to be, better looking and structured code.

Before the Select group of commands I would write some ugly thing like this:

01         IF         COND(&FLAG = 'A') THEN(DO)
02               CHGVAR VAR(&STS) VALUE('ACTIVE')
03               GOTO CMDLBL(ENDTEST)
04         ENDDO
05         IF         COND(&FLAG = 'I') THEN(DO)
06               CHGVAR VAR(&STS) VALUE('INACTIVE')
07               GOTO CMDLBL(ENDTEST)
08         ENDDO
09         CHGVAR     VAR(&STS) VALUE('ERROR')
10         CHGVAR     VAR(&ERROR) VALUE('1')
11  ENDTEST:

Line 1 tests if the field &FLAG is ‘A’, if it is then line 2 is executed, and line 3 goes to the label ENDTEST. I have the GOTO as if &FLAG was equal to ‘A’ it does need to execute any of the code before the ENDTEST label. If I did not have the GOTO it would execute the IF at line 5, and I would have had to put another IF statement between lines 8 and 9 too.

Lines 9 and 10 are only executed if &FLAG is not equal or ‘A’ or ‘I’.

Messy? Yes.

With the Select group the same looks like:

01         SELECT
02             WHEN       COND(&FLAG = 'A') THEN(+
03               CHGVAR VAR(&STS) VALUE('ACTIVE'))
04             WHEN       COND(&FLAG = 'I') THEN(+
05               CHGVAR VAR(&STS) VALUE('INACTIVE'))
06             OTHERWISE  CMD(DO)
07               CHGVAR VAR(&STS) VALUE('ERROR')
08               CHGVAR VAR(&ERROR) VALUE('1')
09             ENDDO
10         ENDSELECT

That’s much easier to understand.

Why do I do that?   As I mentioned in a previous post I indent my code to make it, in my opinion, easier to read.

For those of you who are not familiar with the Select group of statement if works like this:

  • When &FLAG = ‘A’ line 3 is executed and processing goes to the ENDSELECT command.
  • Line is 4 is only executed if &FLAG is not = ‘A’. When &FLAG = ‘I’ line 5 is executed and processing goes down to the ENDSELECT.
  • The OTHERWISE is the equivalent as saying “none of the above”. It is only executed if &FLAG is not = ‘A’ and is not = ‘I’. As I want to execute two commands I have a DO command in the OTHERWISE, then the two statements, lines 7 and 8, and an ENDDO, line 9.
  • The ENDSELECT signifies the end of the group.

You can learn about the Select Group commands from the IBM website:

 

This article was written for IBM i 7.1, and it should work with earlier releases too.

12 comments:

  1. I use Select statements all the time in CL. I tend to use CL probably more than I should. Now that you can read more than one file with CL it makes using CL to do processing much easier without having to chain a bunch of CLs together or using RPG to do the same thing that CL can do.

    With the ability to use SUBR and Select statements, CL is pretty robust for doing what it was meant for...Job Control.

    ReplyDelete
  2. One of the most helpful developers at IBM who got these CL enhancements through, Guy Vig, is retiring.
    Rob Berendt.

    ReplyDelete
  3. Guy was instrumental in getting this changes implemented but he has been working with Jennifer (Zhen) Liu for more than two years to take over the mantle of CL. She is the developer to created the latest four BIFs that came as PTFs for IBM i 7.1. She also has a significant list of additional functions being considered as for the next CL enhancements.

    That said I use Select in nearly every CL Program these days. It is absolutely the best enhancement for readability followed closely by the subroutine.

    ReplyDelete
  4. I was also very happy to see SELECT/WHEN/OTHERWISE added to CL. The enhancements to CL over the past few years have made life better. A few years ago, there were things I would reluctantly write in RPG because (due to limitations of CL), it was easier to have a CL pgm & and an RPG pgm than to keep it all to one CL pgm.

    ReplyDelete
  5. Regardless of the enhancements made for CL programming, it's still just a control language and not intended to replace high level language programming. The Select statement is great, I've been using it for years however it has it's place as well as places where it doesn't belong. I've seen far too many instances where a programmer discovers the Select statement in CL, goes hog wild with its usage, then a business requirement is added or changed and the overall structure and design the developer used wasn't flexible enough to easily incorporate the business requirements, hence they almost had to start over from scratch. Wasted time, money, and unhappy end-users and clients.

    ReplyDelete
  6. Great post - short, clear, concise.

    ReplyDelete
  7. I really do not understand why you don't use RPGLE instead of CL. Functionality is far superior and one does not need to maintain 2 skill sets. I have not written a CL program in 15 years.
    Tommy Atkins

    ReplyDelete
    Replies
    1. While I agree that RPGLE is a far superior language to CL, there are times where I cannot do what I need to do easily in RPGLE. I could use APIs, but in the IT department I currently work in the extensive use of APIs would mean that I would be the one maintaining the code forever. I believe that code should be written that any IBM i coder should be able to understand & maintain it.

      Delete
  8. I used "select" statement in many programs, is very usefully and the other tools like DoFor, DoUntil and DoWhile, combined with subroutines (CallSubR) has converted CL in a great tool to programmer.

    ReplyDelete
  9. Robert PietrzykowskiSeptember 14, 2013 at 5:43 PM

    I use the SELECT statement in my CL. It is a nice enhancement to CL. I also use DOUNTIL, DOWHILE and ITERATE commands to replace the GOTO tags in my loop structures in CL. These new commands all work together to provide good structure and clarity to my CL code.

    ReplyDelete
    Replies
    1. You can also read my post entitled CL does DO about the DO commands in CL.

      Delete

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.