Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2010-08

Break in 'case...of' block.

6 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dave Coventry

I have a function that tests for a series of different conditions according to a value in a 'case...of' block.

Result:=false;
mylist:=TStringlist.Create;
case i of
  1:
   begin
      if condition1 then
      begin
         Result:=true;
         break;
      end;
      if condition2 then
      begin
         Result:=true;
         break;
      end;
      if condition3 then
      begin
         Result:=true;
      end;
   end;
   2:....;
   3:....;
end;
mylist.free;

The above does not work as the 'case...of' is not considered to be a loop. Error: BREAK not allowed.

I could just exit the function, using 'exit' instead of 'break' but I think I need to free the mylist TStrings.

Is there a way of doing this or is my best way just to replace 'break' with 'mylist.free' and 'exit'?

Reply by email

ik
to Dave Coventry

You can do several thins:

1. use Else !!
2. Surround (I hope that the proper spelling) everything in try finally and then in the finally place the free statement.
3. Make a shorter code:

Result := (condition1) OR (condition2) OR (condition3)
Will be the same as your if statment, but shorter.

Ido

Dave Coventry wrote 43 lines
I have a function that tests for a series of different conditions according to a value in a 'case...of' block. Result:=false; mylist:=TStringlist.Create; case i of 1: begin if condition1 then begin Result:=true; break; end; if condition2 then begin Result:=true; break; end; if condition3 then begin Result:=true; end; end; 2:....; 3:....; end; mylist.free; The above does not work as the 'case...of' is not considered to be a loop. Error: BREAK not allowed. I could just exit the function, using 'exit' instead of 'break' but I think I need to free the mylist TStrings. Is there a way of doing this or is my best way just to replace 'break' with 'mylist.free' and 'exit'? -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20100822/928f89a7/attachment.html>

Reply by email

patspiper
to Dave Coventry: I have a function that tests for a series of different conditions according to a value in…
Dave Coventry wrote 39 lines
I have a function that tests for a series of different conditions according to a value in a 'case...of' block. Result:=false; mylist:=TStringlist.Create; case i of 1: begin if condition1 then begin Result:=true; break; end; if condition2 then begin Result:=true; break; end; if condition3 then begin Result:=true; end; end; 2:....; 3:....; end; mylist.free; The above does not work as the 'case...of' is not considered to be a loop. Error: BREAK not allowed. I could just exit the function, using 'exit' instead of 'break' but I think I need to free the mylist TStrings. Is there a way of doing this or is my best way just to replace 'break' with 'mylist.free' and 'exit'? --

You should use Else. But if you insist on break, you can insert a dummy repeat/until loop:

Result:=false;
mylist:=TStringlist.Create;
repeat
case i of
   1:
    begin
       if condition1 then
       begin
          Result:=true;
          break;
       end;
       if condition2 then
       begin
          Result:=true;
          break;
       end;
       if condition3 then
       begin
          Result:=true;
       end;
    end;
    2:....;
    3:....;
end;
until true
mylist.free;

Reply by email

patspiper
to patspiper
You should use Else. But if you insist on break, you can insert a dummy repeat/until loop:

One more thing, since you are creating a TStringList, it is recommended to enclose your code anyway in a try/finally block irrespective whether you use the dummy loop or not. This guarantees the freeing of the list in case of an exception.

Reply by email

Michael Van Canneyt
to Dave Coventry: I have a function that tests for a series of different conditions according to a value in…
Dave Coventry wrote 36 lines
I have a function that tests for a series of different conditions according to a value in a 'case...of' block. Result:=false; mylist:=TStringlist.Create; case i of 1: begin if condition1 then begin Result:=true; break; end; if condition2 then begin Result:=true; break; end; if condition3 then begin Result:=true; end; end; 2:....; 3:....; end; mylist.free; The above does not work as the 'case...of' is not considered to be a loop. Error: BREAK not allowed. I could just exit the function, using 'exit' instead of 'break' but I think I need to free the mylist TStrings. Is there a way of doing this or is my best way just to replace 'break' with 'mylist.free' and 'exit'?

If you use a try/finally block (and you always should when allocating resources) then an exit will go through the finally block. So you can simply use exit.

mylist:=TStringlist.Create;
try
   Case i of
     // Use exit
   end;
finally
   MyList.Free;
end;

Reply by email

Dave Coventry
to Michael Van Canneyt

Great answers!

Thanks, guys! :)

Michael Van Canneyt wrote 60 lines
On Sun, 22 Aug 2010, Dave Coventry wrote: > I have a function that tests for a series of different conditions > according to a value in a 'case...of' block. > > Result:=false; > mylist:=TStringlist.Create; > case i of > ?1: > ?begin > ? ? if condition1 then > ? ? begin > ? ? ? ?Result:=true; > ? ? ? ?break; > ? ? end; > ? ? if condition2 then > ? ? begin > ? ? ? ?Result:=true; > ? ? ? ?break; > ? ? end; > ? ? if condition3 then > ? ? begin > ? ? ? ?Result:=true; > ? ? end; > ?end; > ?2:....; > ?3:....; > end; > mylist.free; > > The above does not work as the 'case...of' is not considered to be a loop. > Error: BREAK not allowed. > > I could just exit the function, using 'exit' instead of 'break' but I > think I need to free the mylist TStrings. > > Is there a way of doing this or is my best way just to replace 'break' > with 'mylist.free' and 'exit'? If you use a try/finally block (and you always should when allocating resources) then an exit will go through the finally block. So you can simply use exit. mylist:=TStringlist.Create; try ?Case i of ? ?// Use exit ?end; finally ?MyList.Free; end; -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email