7

Here is one for you. Although such a language construction does not make much sense I would like to know why the nested LOOP AT SCREEN cause infinite loop (recursion?).

Let us take the following simple program.

REPORT yyy.

PARAMETERS:
  p_x1 TYPE abap_bool.

AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.
    BREAK-POINT.
  ENDLOOP.

The BREAK-POINT statement will be executed only 4 times. This leads to an assumption that such a nested loop would run 16 times. Instead the below mentioned program runs forever and ends with a timeout exception.

REPORT yyy.

PARAMETERS:
  p_x1 TYPE abap_bool.

AT SELECTION-SCREEN OUTPUT.
  LOOP AT SCREEN.
    LOOP AT SCREEN.
      ASSERT 1 = 1.
    ENDLOOP.
  ENDLOOP.

It looks like nesting LOOP AT SCREEN causes either an infinite loop or some kind of an infinite recursion.

Why is it so? Is it documented somewhere? The extended check does not report anything regarding the loop. The same applies to Code Inspector as well.

EDIT

I have also checked whether this is a general problem for internal tables with a header line. It seems not.

REPORT YYY.

DATA: gt_t000 TYPE t000 OCCURS 10 WITH HEADER LINE.

START-OF-SELECTION.
  SELECT * FROM t000
    INTO TABLE gt_t000[].

  LOOP AT gt_t000.
    LOOP AT gt_t000.
      WRITE / gt_t000-mandt.
    ENDLOOP.
  ENDLOOP.
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Jagger
  • 10,350
  • 9
  • 51
  • 93
  • 1
    What is the purpose of a nested loop for screen elements? – József Szikszai Jun 25 '19 at 07:30
  • As I mentioned, there is no purpose. I run into this issue by having made a bug where there was such a nested loop and I had no idea why the program ended with a timeout. According to the documentation such a loop should behave as any "normal" loop over an internal table. – Jagger Jun 25 '19 at 07:33
  • 6
    Although the syntax seems identical, [`LOOP AT SCREEN`](https://help.sap.com/doc/abapdocu_753_index_htm/7.53/en-US/index.htm?file=abaploop_at_screen.htm) should not be regarded as a [loop at an internal table](https://help.sap.com/doc/abapdocu_753_index_htm/7.53/en-US/index.htm?file=abaploop_at_itab.htm). These are 2 distinct docs. SAP could admit a bug; maybe they saw it but thought a correction was useless, a nested loop is non-sense. Ask SAP for a correction. PS: you can imagine what happens internally: it's not an internal table, so there's an element counter which is reset at ENDLOOP. – Sandra Rossi Jun 25 '19 at 07:33
  • So it looks like this is a undocumented behaviour. Even a non-sense constructions should not lead to a runtime error which has to do with timeout. Especially if there is no explicit infinite loop here. – Jagger Jun 25 '19 at 07:36
  • 3
    The keyword documentation of `LOOP AT SCREEN` says: "The statement LOOP AT SCREEN behaves like the statement LOOP in a loop across an internal table, where a system table is used instead of an internal table." So sounds like a nested LOOP should work, and you found a bug. :-) – Florian Jun 26 '19 at 12:34
  • 3
    I opened a ticket at SAP support with the approval from Jagger; SAP seems to positively react and is investigating on a solution that will trigger a runtime error. I will post an answer here as soon as I get one. – Sandra Rossi Jul 06 '19 at 12:06
  • 2
    For information, SAP support is still "working" on it... – Sandra Rossi Sep 17 '19 at 11:28
  • It's still open but SAP support doesn't seem to work on it (I asked for some news 2 months ago and no news...) so I close the ticket, sorry! – Sandra Rossi Jan 12 '20 at 20:49
  • 1
    @SandraRossi It's a pity but thanks for your help though! – Jagger Jan 12 '20 at 21:10

1 Answers1

1

There is no practical use of this nested construction, but I have raised a ticket (387728 / 2023) and got an offical answer from SAP:

Nested LOOP AT SCREENS are not supported. Unfortunately this is not captured by an error but usually results in strange behavior of the system, most likely an endless loop.

we will check whether we can (incompatibly) change the system behavior in a way that nested LOOP AT SCREENs will create a runtime error.

So we should conclude that it is officially unsupported feature and as a consequence using it leads to an undefined system behaviour.

AlexSchell
  • 953
  • 1
  • 1
  • 18