2

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..

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
manchanda
  • 21
  • 2

2 Answers2

3

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");
Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
0

Here is something I tried. It works but it's still not very elegant though:

flag:1;
for k : 1 while k<=10*flag do 
    if (k>3) then (
        print("break !"),
        flag:0)
else 
    print (flag,k);
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Zetanos
  • 1
  • 1