View previous topic :: View next topic
|
Author |
Message |
sh_moni Currently Banned New User
Joined: 30 Apr 2007 Posts: 7 Location: pune
|
|
|
|
Code: |
READ BOOKSALESFILE
AT END SET ENDOFBSF TO TRUE
END-READ
PERFORM UNTIL ENDOFBSF
IF NORMALSALE
RELEASE WORKREC FROM BSF-RECORD
END-IF
READ BOOKSALESFILE
AT END SET ENDOFBSF TO TRUE
END-READ
END-PERFORM
|
Could somebody please explain me this part of code,why we are using here two READ for the same file,if i remove the first one the output file is empty one and if i remove the internal read the program goes in infinite loop.
Please help me out. |
|
Back to top |
|
|
TG Murphy
Active User
Joined: 23 Mar 2007 Posts: 148 Location: Ottawa Canada
|
|
|
|
sh_moni,
It is very common to see 2 READS as shown in your email. In fact that is the way I code it myself. Reason why is that the PERFORM UNTIL is actually a DOWHILE construct - not a DOUNTIL as the COBOL syntax would suggest. The condition gets tested first and then the logic within the loop only executes if the condition is true.
The alternative coding that would let you code just a single READ would look something like this:
PERFORM WITH TEST AFTER UNTIL EOF
PERFORM READ
IF NOT EOF
PERFORM PROCESS-RECORD
END-IF
END-PERFORM
And this is also a perfectly fine way to do it.
However, there are few common types of programs where you need special logic that executes ONLY for the first record that you read. (ie control break logic) For these programs, the "2 READ" approach is perfectly suited to doing this. So there is a good reason why you bump into the "2 READ" approach so often. |
|
Back to top |
|
|
ramfrom84
New User
Joined: 23 Aug 2006 Posts: 93 Location: chennai
|
|
|
|
Hi,
Mostly READ can be used for TWO times , only if we want the prevoius record value for comparison or rolling up the records , then they will read one time at the begining , for next time onwards u can read and do the process with operation(having previous value).
It is suggestion since u have mention only few code, u can trace the program for better understanding. |
|
Back to top |
|
|
socker_dad
Active User
Joined: 05 Dec 2006 Posts: 177 Location: Seattle, WA
|
|
|
|
To put it in ancient terminology (cause I am an old fart and read and write this kinf od code all day!), this is what your code does.
The first read is called an initial read. It reads the file and loads the first record for subsequent processing. If there is no first record, the end of file condition is set, in your case, ENDOFBSF. The END-READ closes this statement.
The PERFORM UNTIL is a separate piece of logic. It basically states that we are going to test the NORMALSALE condition and Release the Workrec from the BSF-RECORD if it is true. Then we read the next record in the Book Sale File, setting ENDOFBSF to true when we reach the end of the file.
Once the condition ENDOFBSF is true, we drop out of the PERFORM UNTIL statement. This could happen on the first record or on the ninety-first record.
The idea is that you use the first Read once to open the file and get the first record, perform processing on the record, then read the next record. The UNTIL logic keeps your program from attempting to perform operations on records that do not exist. And the Read at the bottom of the Perform logic allows you to read the next record and set an end of file condition before attempting to do any work on the record.
As previously mentioned, this is very, very common logic and if you work a lot with batch programs, you will find many of them use this basic logic.
Hope that was clear. |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
To add to Dad's post.....
When you program, there are three parts of the logic. The main and primary is the central logic - the read/write sort of thing. Before and after that are the "boundary conditions", how to start and how to finish....the initial read set up the program so that it could loop through the central section seamlessly.
Personally, when I code such logic, I always start with the loop and then figure out how to start and stop it....
Good luck. |
|
Back to top |
|
|
sh_moni Currently Banned New User
Joined: 30 Apr 2007 Posts: 7 Location: pune
|
|
|
|
hi all,
many thanks for your explanations,its quite clear to me know. |
|
Back to top |
|
|
|