AT BREAK behaviour doubt

Hello! I’m using AT BREAK in a code, and at somepoint, it’s behaviour is not making sense. AT BREAK block statements execute when the value checked is different from the previous one read. But in my code, when the 7th register is read in a FIND loop, its behaviour is not making sense (at least for me). The code is:

1 BILL VIEW OF DD-BILL-DETAILS
  2 CODE  (N11)
  2 YEAR  (N04)

FIND BILL WITH CODE = 12345678903
  WRITE '=' YEAR
  AT BREAK OF YEAR
     WRITE ' --- THE YEAR CHANGED ---'
  END-BREAK
END-FIND

The output was like this:

YEAR: 2017
YEAR: 2017
YEAR: 2017
YEAR: 2017
YEAR: 2017
YEAR: 2017
YEAR: 2017
— THE YEAR CHANGED —
YEAR: 2018
— THE YEAR CHANGED —

DOUBT: When the 7th record is READ, with value of YEAR is 2017, but the value is checked in 6th record to verify if there was a change… since the YEAR is the same on 6th and 7th record, why does AT BREAK execute its statements when the 7th record is read? It makes sense when it is executed in 8th record, when YEAR is 2018 (because in the previous record is 2017).

Thanks in advance.

You need to review the documentation for the AT BREAK statement:

“This statement is non-procedural (that is, its execution depends on an event, not on where in a program it is located).”

Regardless of where you place the statement, think of it as executing between the FIND and the first WRITE.

1 Like

at the time the AT BREAK is executed, the current record has been read (in your case, the 8th record, the first YEAR 2018 record). You can use the OLD function to see the prior record values. Change your AT BREAK to show the new and old values:

AT BREAK OF YEAR
  WRITE '-- The year changed from ' OLD(YEAR) ' to ' YEAR ' ---'
END-BREAK

Check out the other functions you can use in the AT BREAK (and AT END) such as SUM, TOTAL, etc.

1 Like

Hi Ralph, I saw this on documentation.
I’m looking up exactly for the event, that is, when the year changes… that’s why I didn’t understand why the phrase inside AT BREAK was printed in the 7th record read if from 6th to 7th record they have the same year, you know?
But I will try Douglas tip, to see the prior and actual values in AT BREAK block.
Thank you.

As Ralph tried to explain - the WRITE inside AT BREAK is not triggered on the 7th record but when the value changes on reading the 8th record.

As AT BREAK is “non procedural” it is triggered immediately when the value changes, independently of any code inside the FIND loop, consider the AT BREAK block as not being part of the FIND loop, maybe put it outside of it to get a better feeling for what non-procedural means in this case.

Take ON ERROR as another example, you can place it anywhere inside an object and it will get triggered by an event, in this case an error.

After the AT BREAK code has been executed triggered by the changed value the code inside the FIND loop gets executed.

1 Like