Board index » delphi » Newbie:How to break out of a FOR loop

Newbie:How to break out of a FOR loop

It's not much different. Like this...

var
  i : integer;
begin
  for i := 1 to 9 do
  begin
    if buffer[i] = aValue then
      break;
    if buffer[i] = anotherValue then
      continue;
  end;
end;

Note that I've put a begin/end around this because the declaration of "i"
needs to be placed in the "var" section, which comes before the procedural
section of a function of procedure.

It's probably also worth noting that the two "if" statements could be mroe
efficiently coded as a case structure, but since this is obviously just an
abstract example I suppose that doesn't matter.

 tfo...@patrick.com.au (Tze Weng Foong) writes:

Quote
>Sorry for such a basic question but I have not been
>able to find the answer in the Online Help.
>The question is how do break out or continue a for
>loop. I guess I am showing my 'C' background with
>this question.
>Specifically in 'C' I could write the following code
>int i
>for ( i = 1; i < 10 ; i++)
>  {
>  if (buffer[i] == aValue)
>    break;
>  if (buffer[i] == anotherValue)
>    continue;
>  }
>How would that be translated to Delphi 3.
>Thanks in advance.
>Tze-Weng Foong
>tfo...@patrick.com.au

--
Luke Webber

* Note: The opinions expressed by Luke Webber are in no way supported *
*       by his employers, Luke Webber Consulting Services             *

 

Re:Newbie:How to break out of a FOR loop


Would something like this help:

procedure TfrmBransje.Button1Click(Sender: TObject);
var
  i:integer;
begin
 AbortLoop := false ;
  for i:=0 to 1000 do begin
    Application.ProcessMessages ;
    if AbortLoop then
      Exit
    else
      LabelLoop.Caption := 'Index= ' + IntToStr(i);
  end ;
end;

procedure TfrmBransje.Button2Click(Sender: TObject);
begin
  AbortLoop := true ;
end;

Re:Newbie:How to break out of a FOR loop


On Mon, 29 Sep 1997 00:43:13 GMT, tfo...@patrick.com.au (Tze Weng

Quote
Foong) wrote:
>Hello,

>Sorry for such a basic question but I have not been
>able to find the answer in the Online Help.

>The question is how do break out or continue a for
>loop. I guess I am showing my 'C' background with
>this question.

>Specifically in 'C' I could write the following code

>int i
>for ( i = 1; i < 10 ; i++)
>  {
>  if (buffer[i] == aValue)
>    break;
>  if (buffer[i] == anotherValue)
>    continue;
>  }

>How would that be translated to Delphi 3.

>Thanks in advance.

>Tze-Weng Foong
>tfo...@patrick.com.au

Well, the simple answer would seem to be

var i: integer;
{...}
for i := 1 to 9 do
begin
   if (buffer[i] = aValue) then break;
   if (buffer[i] = anotherValue) then continue;
end;

Could be a bit more complicated depending on the type of the buffer
variable.

hth
David

------------------
David A. Schweizer

iec ProGAMMA, The Netherlands
d.a.schweizer[OK, i don't want any more spam]gamma.rug.nl
guess where the '@' goes ?

Re:Newbie:How to break out of a FOR loop


Quote
Tze Weng Foong wrote:

> The question is how do break out or continue a for
> loop. I guess I am showing my 'C' background with
> this question.

They are the same: break and continue.

odair from brazil

Re:Newbie:How to break out of a FOR loop


Quote
Tze Weng Foong wrote:

> Hello,
> ...
> The question is how do break out or continue a for
> loop. I guess I am showing my 'C' background with
> this question.

> Specifically in 'C' I could write the following code

> int i
> for ( i = 1; i < 10 ; i++)
>   {
>   if (buffer[i] == aValue)
>     break;
>   if (buffer[i] == anotherValue)
>     continue;
>   }

> How would that be translated to Delphi 3.

Hi,
same thing in Pascal would look like:

var
  i: Integer;
...
begin
  ...
  for i := 1 to 10 do
  begin
   if (buffer[i] = aValue) then
     Break;
   if (buffer[i] = anotherValue) then
     Continue;
   ...
  end;
  ...
end;

If you want to find this in the online help, click on 'Loops' in the
Delphi 2.0 help index.

-Vladimir

Re:Newbie:How to break out of a FOR loop


Hello,

Sorry for such a basic question but I have not been
able to find the answer in the Online Help.

The question is how do break out or continue a for
loop. I guess I am showing my 'C' background with
this question.

Specifically in 'C' I could write the following code

int i
for ( i = 1; i < 10 ; i++)
  {
  if (buffer[i] == aValue)
    break;
  if (buffer[i] == anotherValue)
    continue;
  }

How would that be translated to Delphi 3.

Thanks in advance.

Tze-Weng Foong
tfo...@patrick.com.au

Re:Newbie:How to break out of a FOR loop


Quote
Tze Weng Foong wrote:

> Specifically in 'C' I could write the following code

> int i
> for ( i = 1; i < 10 ; i++)
>   {
>   if (buffer[i] == aValue)
>     break;
>   if (buffer[i] == anotherValue)
>     continue;
    // do something
>   }

> How would that be translated to Delphi 3.

-------
  var
    i : Integer;

  i := 1;
  while (i < 10) and (buffer[i] <> aValue) do
  begin
    if (buffer[i] <> anotherValue) then
    begin
      // do something
    end;
    i := i + 1; // assuming a value of 11 is ok
  end;
-------

In Pascal you are not supposed to break out of for-loops. The value
of i is (if I am not mistaken) undefined after the for-loop.

RJ

Quote

> Thanks in advance.

> Tze-Weng Foong
> tfo...@patrick.com.au

--
Roy Jonker, PhD @ MagicLogic Optimization Inc.
tel (604) 535 5133 (BC, Canada) - fax (on request)
email roy_jon...@magiclogic.com - www http://www.magiclogic.com/

Re:Newbie:How to break out of a FOR loop


Thanks for all the replies.

However as bit of interest most of your kind replies
had a little bug in it  :-).

Quote
>> int i
>> for ( i = 1; i < 10 ; i++)
>>   {
>>   .....
>>   }

was translated to.

Quote
>  for i := 1 to 10 do
>  begin
>  ......
>  end;

And the bug is  .....

Well the FOR loop control statement in Pascal should
have been

    for i := 1 to 9

;-)

Best Regards
Tze-Weng Foong

PS I did really appreciate the help.

Re:Newbie:How to break out of a FOR loop


Quote
Tze Weng Foong wrote in message <60n0v8$nl...@merki.connect.com.au>...

>int i
>for ( i = 1; i < 10 ; i++)
>  {
>  if (buffer[i] == aValue)
>    break;
>  if (buffer[i] == anotherValue)
>    continue;
>  }

>How would that be translated to Delphi 3.

>Thanks in advance.

>Tze-Weng Foong
>tfo...@patrick.com.au

Note that although you can use Break and Continue within Delphil 'for'
loops, the value of the loop control variable (i) is undefined after the
loop has terminated - Delphi 1 implements for loops C style and,
incidentaly, allows you to change the value of I within the loop.

Delphi 2 and 3 don't.

D A Stocks

Re:Newbie:How to break out of a FOR loop


Quote
> Tze Weng Foong wrote:

> > Specifically in 'C' I could write the following code

> > int i
> > for ( i = 1; i < 10 ; i++)
> >   {
> >   if (buffer[i] == aValue)
> >     break;
> >   if (buffer[i] == anotherValue)
> >     continue;
>     // do something
> >   }

> > How would that be translated to Delphi 3.

How about this?

    var i : integer;
    begin
       for i := 1 to 9 do
       begin
          if buffer[i] = aValue then
             break;

          if buffer[i] = anotherValue then
             continue;
          { do something }
       end
    end;

Re:Newbie:How to break out of a FOR loop


D A Stocks <dasto...@channel4.co.uk> wrote in article
<3433657...@news.access1.net>...

Quote

> Tze Weng Foong wrote in message <60n0v8$nl...@merki.connect.com.au>...

> >int i
> >for ( i = 1; i < 10 ; i++)
> >  {
> >  if (buffer[i] == aValue)
> >    break;
> >  if (buffer[i] == anotherValue)
> >    continue;
> >  }

Break and Continue are functionally identical in Pascal

Regards,

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Laurie Bisman    lbis...@ihug.co.nz (ICQ 1458799)
Web page          http://homepages.ihug.co.nz/~lbisman/index.html
Company           http://www.addlink.ac.nz/Home.htm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Other Threads