I have the following statements : FOR i := 1 TO 10 IF i = 4 THEN BREAK; MESSAGE(FORMAT(i)); This code will NOT show the message “4”, because the BREAK will jump out of the section instead of jumping out of the for-loop. Is there a way to EXIT/ESCAPE/BREAK the loop ?
Only a dirty way. That is to assign the control variable the exit value. In this case it would be FOR i := 1 to 10 DO BEGIN IF i = 4 THEN i := 10 END; Dirty, but it works. Lars Strøm Valsted ------------------------- Why can’t programmers tell the difference between Christmas and Halloween? Because OCT(31) = DEC(25)
Or you could set a boolean value to true at some point in your code when the desired condition has been reached…then each time you run through the loop just check that the boolean value isn’t true yet…
Hi try this code FOR i := 1 TO 5 DO IF i = 4 THEN BEGIN MESSAGE(FORMAT(i)); EXIT; END; MK
In any case, NEVER use the FOR … TO statement if you plan to exit by testing a value, it always exists a better solution using a REPEAT … UNTIL, WHILE … LOOP or a function.
Jean-Marc is 100% correct in what he says.
Thnx for all the solutions, but none of them was what I was looking for. 1) “IF i=4 THEN i:=10” doesn’t work if you later changes the code to “FOR i := 1 TO 20” 2) I know that REPEAT-UNTIL can do the same, but I have a lot of code where the FOR-loop is used, and I just needed a command to exit the loop (in Delphi BREAK does this, in Visual Basic EXIT FOR does is, and so on). 3) EXIT will (like BREAK) NOT continue after the loop : FOR i := 1 TO 5 DO IF i = 4 THEN BEGIN MESSAGE(FORMAT(i)); EXIT; END; MESSAGE('THE LOOP IS FINISHED, AND NOW I = ’ + FORMAT(I)); // This line will never be executed…
I don’t really understand where the problem is but … I’m quite sure that you can do It with REPEAT or WHILE but i’m sure that you can do it if you put your LOOP in a function because EXIT will exit you from the function and return to the calling code. If there are a lot of code, operate the function with global data. Code … … … looping message(‘Out of Loop’); … … Func Looping for i:=1 to 5 do begin … if i=4 then exit; … … end; of course you can use return value.
You’re quit right, the best way to solve this is by REPEAT or WHILE, but my problem is that it is a lot of old source code, so the easyest way was to ‘EXIT’ the loop (in a proper way), but since this not is possible I have to ‘convert’ all the stuff to REPEART of WHILE Thanx for your time
Moved from End User Questions to Development Forum.
If you place your loop in a Function, then you can use EXIT if the condition(s) has been reached.
I run into this on once in a while and I agree witht he others that a repeat - until loop is best. However, if you know the number of maximum loops but, it may be less you can do: Where FoundCondition is a Boolean variable: repeat … … FoundCondition := Some Condition; i += 1; until (i >= 100) or FoundCondition Bill Benefiel Manager of Information Systems Overhead Door Company firstname.lastname@example.org (317) 842-7444 ext 117
Originally posted by ResoundMichael: 2) I know that REPEAT-UNTIL can do the same, but I have a lot of code where the FOR-loop is used, and I just needed a command to exit the loop (in Delphi BREAK does this, in Visual Basic EXIT FOR does is, and so on). …
You will be much better of to fix the code rather than just patch wrong code. If originally there was a FOR loop, and now you need a WHILE loop, then there must have been either a/ a chnage in business procedures defining a new requirement, or b/ the analysis was wrong in the begining. If A/ Then a new requirement needs a new solution. If B/ Then you have to throw every thing out and start again, otherewise you are just asking for troubles down the track. _________________________ David Singleton Navision Consultant since 1991 email@example.com___________