Recent

Author Topic: Feature suggestion - repeat while.. until  (Read 763 times)

rvk

  • Hero Member
  • *****
  • Posts: 7045
Re: Feature suggestion - repeat while.. until
« Reply #15 on: May 19, 2026, 04:53:50 pm »
This is correct only with short-circuit evaluation of the OR, yes?

Indeed. When  {$B+}  is on for some reason (full boolean evaluation), the field access will raise an exception once EOF is reached.
Hey... it's pascal... we can make a function for that  :P (you could even use fieldnames instead of [ 0 ] )

Code: Pascal  [Select][+][-]
  1. function DataSetDoneOrReachedLimit(DataSet: TDataSet; Limit: Integer): Boolean;
  2. begin
  3.   Result := DataSet.EOF;
  4.   if not Result then
  5.     Result := DataSet.Fields[0].AsInteger >= Limit;
  6. end;
  7.  
  8. // usage:
  9.  
  10. until DataSetDoneOrReachedLimit(DataSet, 10000);

BTW. In practice the previous code with "DataSet.eof or (dataSet.Fields[0].AsInteger >= 10000)" won't crap out because even if EOF is true, the dataset still has the values from the previous record. So there wouldn't be an error (and because EOF is true the loop will end).

But that's only in practice... there could be situations (other than .Next / .EOF) where this kind of loop does crap out so it's still a good habit to use short-circuit evaluation (or a separate function in case of full evaluation).

Warfley

  • Hero Member
  • *****
  • Posts: 2067
Re: Feature suggestion - repeat while.. until
« Reply #16 on: May 19, 2026, 07:38:40 pm »
If you are for whatever reason unsure if short circuit evaluation is off, you can also do the following:
Code: Pascal  [Select][+][-]
  1. {$PUSH}
  2. {$B-}
  3. until ...
  4. {$POP}
But because short circuit eval is the default behavior I would assume that all places that do require full evaluation just do a temporary enabling (Push -> B+ -> Code -> Pop)

 

TinyPortal © 2005-2018