## Re: [Maxima-discuss] How to break out of a loop with a block?

 Re: [Maxima-discuss] How to break out of a loop with a block? From: Jorge Calvo - 2014-04-28 02:38:09 ```I believe the writer of the documentation meant a return inside a do loop inside a block, not a return inside a block inside a do. The first case behaves as expected, iterating only twice and returning 7: (%i1) RetVal:block(for k in [1,2,3] do (print(k), if k=2 then return(7))); 1 2 (%o1) 7 (%i2) RetVal; (%o2) 7 The second, which is essentially your example, iterates three times and returns "done": (%i3) RetVal:for k in [1,2,3] do block(print(k), if k=2 then return(7)); 1 2 3 (%o3) done (%i4) RetVal; (%o4) done The problem is that return() is exiting the block (which it would have done anyway) but not the loop. Note that if we move the contents of the block around, we end up skipping the second print(): (%i5) RetVal:for k in [1,2,3] do block(if k=2 then return(7), print(k)); 1 3 (%o5) done Hope that clarifies things! Jorge -- Dr. Jorge Alberto Calvo Associate Professor of Mathematics Department of Mathematics and Physics Ave Maria University Phone: (239) 280-1608 Email: jorge.calvo@... Web: http://sites.google.com/site/jorgealbertocalvo From: Helmut Jarausch > Subject: [Maxima-discuss] How to break out of a loop with a block? Date: April 27, 2014 5:49:00 AM EDT To: > The documentation says: Note however that a return within a do that occurs in a block will exit only the do and not the block. The following do does not occur within a block, so why does RetVal:for k in [1,2,3] do block( if k=2 then return(7))\$ print(RetVal)\$ print 'done' instead of 7 ? Thanks for an explanation and a work around, Helmut ```

 Re: [Maxima-discuss] How to break out of a loop with a block? From: Jorge Calvo - 2014-04-28 02:38:09 ```I believe the writer of the documentation meant a return inside a do loop inside a block, not a return inside a block inside a do. The first case behaves as expected, iterating only twice and returning 7: (%i1) RetVal:block(for k in [1,2,3] do (print(k), if k=2 then return(7))); 1 2 (%o1) 7 (%i2) RetVal; (%o2) 7 The second, which is essentially your example, iterates three times and returns "done": (%i3) RetVal:for k in [1,2,3] do block(print(k), if k=2 then return(7)); 1 2 3 (%o3) done (%i4) RetVal; (%o4) done The problem is that return() is exiting the block (which it would have done anyway) but not the loop. Note that if we move the contents of the block around, we end up skipping the second print(): (%i5) RetVal:for k in [1,2,3] do block(if k=2 then return(7), print(k)); 1 3 (%o5) done Hope that clarifies things! Jorge -- Dr. Jorge Alberto Calvo Associate Professor of Mathematics Department of Mathematics and Physics Ave Maria University Phone: (239) 280-1608 Email: jorge.calvo@... Web: http://sites.google.com/site/jorgealbertocalvo From: Helmut Jarausch > Subject: [Maxima-discuss] How to break out of a loop with a block? Date: April 27, 2014 5:49:00 AM EDT To: > The documentation says: Note however that a return within a do that occurs in a block will exit only the do and not the block. The following do does not occur within a block, so why does RetVal:for k in [1,2,3] do block( if k=2 then return(7))\$ print(RetVal)\$ print 'done' instead of 7 ? Thanks for an explanation and a work around, Helmut ```