Board index » delphi » While...do vs repeat..until

While...do vs repeat..until

These two procedures do the same thing but I want to know which one to use
in the example below or if it doesnt matter at all.

example 1.
table1.first;
While not table1.eof do
begin
     memo1.lines.add(table1.fieldbyname('Field one').asstring);
     taRapport.next;
end;

example 2.
table1.first;
repeat
begin
     memo1.lines.add(table1.fieldbyname('Field one').asstring);
     taRapport.next;
end;
until table1.eof = true;

All answers are appreciated, please also reply by e-mail if possible.

Thanks, Fredrik Larsson.

:o) >>>>>>>>>>>>>>>>>>> mail2f...@hotmail.com <<<<<<<<<<<<<<<<<<< (o:

 

Re:While...do vs repeat..until


Hi. The first one is better since You might have an empty table. It is not
good practise to do a Table1.Next if Table1.Eof is true ( which it would be
with the empty table ) .

While we are at it, You dont need a begin ... end when in a repeat ...
until, it is implied in the loop-construct.

/Jacob Granert

Fredrik Larsson <Mail2F...@Hotmail.com> skrev i inl?gg
<01bc219b$a6c05000$0100007f@fredde>...

Quote
> These two procedures do the same thing but I want to know which one to
use
> in the example below or if it doesnt matter at all.

> example 1.
> table1.first;
> While not table1.eof do
> begin
>      memo1.lines.add(table1.fieldbyname('Field one').asstring);
>      taRapport.next;
> end;

> example 2.
> table1.first;
> repeat
> begin
>      memo1.lines.add(table1.fieldbyname('Field one').asstring);
>      taRapport.next;
> end;
> until table1.eof = true;

> All answers are appreciated, please also reply by e-mail if possible.

> Thanks, Fredrik Larsson.

> :o) >>>>>>>>>>>>>>>>>>> mail2f...@hotmail.com <<<<<<<<<<<<<<<<<<< (o:

Re:While...do vs repeat..until


Quote
Fredrik Larsson wrote:
> example 2.
> table1.first;
> repeat
> begin
>      memo1.lines.add(table1.fieldbyname('Field one').asstring);
>      taRapport.next;
> end;
> until table1.eof = true;

This will cause trouble if your table is empty.  You first need to
detect eof, and then do something with the table, so your example1 is
the right way to go.

Just remember :
  while...do      : you can *check* something before you *do* something
  repeat...until  : you *do* something, and then you *check*

(I hope it's clear enough )

Just my $.02

Pascal

--------------------------------------------------------------------
ing. Pascal Dutilleul

{*word*137} software engineer (also via internet)

Pascal.Dutill...@ping.be                  Borland Connections Member
--------------------------------------------------------------------

Re:While...do vs repeat..until


Quote
Fredrik Larsson wrote:
> =
> These two procedures do the same thing but I want to know which one to =
use
> in the example below or if it doesn=B4t matter at all.

Not the some thing .

Quote
> example 1.
> table1.first;
> While not table1.eof do
> begin
>      memo1.lines.add(table1.fieldbyname('Field one').asstring);
>      taRapport.next;
> end;

if table is empty, after command "table1.first" the "table1.eof" return
TURE and the loop body will be not  executed.

Quote
> =
> example 2.
> table1.first;
> repeat
> begin
>      memo1.lines.add(table1.fieldbyname('Field one').asstring);
>      taRapport.next;
> end;
> until table1.eof =3D true;

In this case memo1...... will be executed at least once time and this
examle will cause error on empty database.
 So, first examle is better.

-- =

Vidmantas Matelis
http://www.netrover.com/~vidma

Re:While...do vs repeat..until


I have only used maybe 1 or 2 repeat-until loops - ever.  In example 2, what if
you have 0 records?  In that case, you probably wouldn't want the stuff in the
repeat loop to execute at all, would you?

Just some input...
Hope it helps
Chris <csu...@acxiom.com>

In article <01bc219b$a6c05000$0100007f@fredde>, Mail2F...@Hotmail.com says...

Quote

>These two procedures do the same thing but I want to know which one to use
>in the example below or if it doesnt matter at all.

>example 1.
>table1.first;
>While not table1.eof do
>begin
>     memo1.lines.add(table1.fieldbyname('Field one').asstring);
>     taRapport.next;
>end;

>example 2.
>table1.first;
>repeat
>begin
>     memo1.lines.add(table1.fieldbyname('Field one').asstring);
>     taRapport.next;
>end;
>until table1.eof = true;

>All answers are appreciated, please also reply by e-mail if possible.

>Thanks, Fredrik Larsson.

>:o) >>>>>>>>>>>>>>>>>>> mail2f...@hotmail.com <<<<<<<<<<<<<<<<<<< (o:

Re:While...do vs repeat..until


Quote
Fredrik Larsson wrote:
> =
> These two procedures do the same thing but I want to know which one to =
use
> in the example below or if it doesn=B4t matter at all.
> =
> example 1.
> table1.first;
> While not table1.eof do
> begin
>      memo1.lines.add(table1.fieldbyname('Field one').asstring);
>      taRapport.next;
> end;

If there are no records, The above will correctly not execute the add,
next.

Quote
> =
> example 2.
> table1.first;
> repeat
> begin
>      memo1.lines.add(table1.fieldbyname('Field one').asstring);
>      taRapport.next;
> end;
> until table1.eof =3D true;

The above will incorrectly attempt to add from an empty dataset.

Quote
> Thanks, Fredrik Larsson.
> =

-- =

-----------------------------------------
Software Services - Making Windows Scream
http://www.invsn.com/softserv/
bry...@thevision.net
-----------------------------------------

Re:While...do vs repeat..until


Quote
Fredrik Larsson wrote:
> =
> These two procedures do the same thing but I want to know which one to =
use
> in the example below or if it doesn=B4t matter at all.
> =

Use repeat-until for loops that always should be run at least once.
Use while-do for loops with any loop count.

-- =

Svante Granqvist                 Speech, Music and Hearing
Phone +46-8-790 7561             Box 700 14 =

Fax   +46-8-790 7854             S-100 44 Stockholm
mailto:sva...@speech.kth.se      http://www.speech.kth.se/~svante

Other Threads