How to break from a loop in Maxima

Viewed 799

I am new to Maxima. I am trying to write a loop in that I am checking if some condition met then exit from the loop.

cp:for i:1 step 1 thru 10 do
block(if(i>6) then break()
else
print(i,"is less than 6"));

I want output:
1 is less than 6
2 is less than 6
3 is less than 6
4 is less than 6
5 is less than 6
6 is less than 6

But when I am running the above code :

after printing 6 is less than 6, it is prompting Entering a Maxima break point. Type 'exit;' to resume.
and after typing exit; it will again show the above msg

I want the code will come out completely from that loop rather than asking to type exit;

Thank you in advance..

1 Answers

Try return(i) instead of break(). Also, return only returns from the block which encloses it, so you need to remove the block(...) in your example (it's unneeded anyway). I think this works:

cp: for i:1 step 1 thru 10 
      do if(i>6) then return(i) else print(i,"is less than 6");
Related