Information Technology Reference
In-Depth Information
go to end-program
end-if
end-program: end
At first glance this may look similar to what we had earlier and it appears to be a valid set
of statements. Note though that we can never get to the label
end-program
if the file has any records because the variable
acct-status
will always be 0. If the file was empty, then and only then would the program end
because the status would not be 0 and the branch to the last statement would be taken. If
there is at least one record on the file, the status would be 0 and the print would be done
and then a branch to the label
start
would take place. At this point, no new record will be read and the field
file-status
would still have a value of 0 and once again we would print out the same account number
preceded by the appropriate label.
What we have is an infinite loop, as the program will continue to print out this
same account number with the literal
account number:
preceding it. The program would never end unless we somehow interrupted it. The
problem is we need to do the read of the file each time we get to the label
start.
A simple change will get us out of this mess. All we need is to move the label up one line
to the read statement and then we would eliminate this looping. Thus our statements
become:
start:
read acctfile
if acct-status = 0
print “account number ” account-number
go to start
else
go to end-program
end-if
end-program: end
and now there is no difficulty and the program will eventually end without us having to
interrupt it.
This program could also have been written as
start:
read acctfile
if acct-status = 0
print “account number ” account-number
Search WWH ::




Custom Search