Spring 2023 Enhancements to SELECT/WHEN

Joke of the month:

Crow: Excuse me, ma’am, would you like to donate to my charity?

Woman: Maybe, what is it called?


Woman: I don’t understand!

Crow: It’s four good caws!

Spring 2023 Enhancements to SELECT/WHEN

The RPG SELECT/WHEN statements have been enhanced to allow for a single condition with multiple values, which in some cases is a better code.  Let me explain…

Many programming languages (C, C++, Java, JavaScript, PHP and Python to name a few) have a SWITCH/CASE statement or similar (for example, in Python it’s called MATCH/CASE instead) that looks like this:

switch (myVar) {

    case 1:

      /* code when myVar = 1 */

    case 2:

      /* code when myVar = 1 */


Historically, however, the closest RPG had was SELECT/WHEN and it looks like this:


when myVar = 1;

  // code when myVar = 1

when myVar = 2;

  // code when myVar = 2


You may be thinking “that’s the same thing”… but it isn’t for a few reasons:

  • You have to type the variable on each line, which is tedious.
  • The compiler has to generate code to read the myVar variable multiple times, which is very fast, but does use a little bit of extra CPU time.
  • You can potentially use a different condition on each ‘when’
  • If expression you’re evaluating contains a call to a routine, you have the potential to repeat the code.

For example, consider a situation where you call a routine to write a new item to a database. It returns an error number, or 0 If all is well.  Consider how you’d code this in other languages with switch/case:

switch ( writeNewItem() ) {

    case 0:

      /* success */

    case 1000:

      /* error code 1000 – file not found */

    case 2000:

      /* error code 2000 – item not found */


What would happen if you did the same thing in RPG with SELECT/WHEN?


when writeNewItem() = 0;

  // success

when writeNewItem() = 1000;

  // error code 1000 – file not found

when writeNewItem() = 2000;

  // error code 2000 – item not found


Oops, this is a problem!  It calls the subprocedure 3 times, potentially writing 3 items instead of one!  Naturally you could call the writeNewItem() routine before the SELECT clause, save the result into a variable, and then check the variable value with WHEN, but it illustrates the difference between SWITCH/CASE and SELECT/WHEN very nicely.

Indeed, SELECT/WHEN doesn’t do anything you couldn’t also do with IF/ELSEIF.

Thankfully in Spring 2023, IBM added some new functionality to SELECT/WHEN so that you can now use it more like you might’ve used SWITCH/CASE in the other environments.  The enhancement adds the ability to code an expression on the SELECT opcode, and adds the WHEN-IS and WHEN-IN opcodes.

With the enhancement, you can write your SELECT/CASE like this:

select writeNewItem();

when-is 0;

  // success

when-is 1000;

  // error code 1000 – file not found

when-is 2000;

  // error code 2000 – item not found


Using an expression with SELECT and checking single values with WHEN-IS makes it comparable to SWITCH/CASE – you no longer need to repeat the condition, the compiler only needs to evaluate the variable once, and if a routine is called, it is only called once!

Furthermore, the WHEN-IN function lets you use the types of conditions you might’ve used with the IF VARIABLE IN syntax that was added to RPG back in Fall 2020.  For example:

select askForPrice();

when-in %range(5: 10);

  // code for when a price is within a range of 5 – 10.

when-in %list(7: 21: 4291);

  // this code is run if the price is 7, 21 or 4291


WHEN-IN can also check if the value is in an array, or it can check a portion of an array if you use the %SUBARR built-in function.

If you code both WHEN-IS and WHEN-IN in the same SELECT group, WHEN-IS will take precedence over WHEN-IN.

To find the PTFs that are needed to add these features to your IBM i 7.4 or 7.5 system, see the following link: https://www.ibm.com/support/pages/node/6982199

Scott Klement

Scott Klement
Midrange Dynamics Development & Solutions Architect

Scott Klement is an IT professional with a passion for both programming and mentoring. He joined Midrange Dynamics at the beginning of October 2022. He formerly was the Director of Product Development and Support at Profound 

Logic and the IT Manager and Senior Programmer at Klement’s Sausage Co., Inc. Scott also serves on the Board of Directors of COMMON, where he represents the Education, Innovation, and Certification teams. He is an IBM Champion for Power Systems.

Subscribe to our newsletter and join us next month to see what is happening in Scott’s Corner. Add a great dad joke to your arsenal and gain an even better IT insight from this recognized industry expert as he continues his quest to educate and support the IBM i community.

Similar Posts