For Casio Graphic Calculators 9750/9850/9950 and compatibles

Breaking Out Of Nested 'For' Loops

A nested 'For' loop looks like this:
For 0->A To 59
For 0->B To 59
{A,B}_			; use curly brackets
Next
Next
The above prints pairs of numbers like a timer. However there can be problems. Remember from the manual that if the end number is less than the start number that the loop is not executed.

So in the example below if you entered 0 at the input you might expect it to skip the whole loop.

?->A
For 1->K To A
For 1->L To 5
Next
Next
But it doesn't. What it actually does when it sees A=0 is look for the first 'Next' statement it can find and it then says Syn Error when it comes across the second 'Next' because it had no corresponding 'For'.
Similarly the 'Break' statement won't break out of a whole loop but just look for the nearest 'Next' statement. e.g:
For 1->K To 4
K=3=>Break
For 1->L To 5
{K,L}_
Next
Next
Instead of breaking out of the 'K' loop the above just goes to the nearest 'Next' and then says 'Syn Error' at the last 'Next' statement.

To avoid this you can put the break statement after the inner loop, where the nearest 'Next' command is the correct 'Next' command. e.g:

For 1->K To 4
For 1->L To 5
{K,L}_
Next
K=3=>Break
Next
Also the break command works fine to break out of innermost loop e.g:
For 1->K To 4
For 1->L To 5
L=3=>Break
{K,L}_
Next
Next

You might be tempted to use a 'Goto' command to jump out of the loops. This will work, but when the program ends there will be an error because the program is still looking for a closing 'Next' statement. If you are aware that this will happen are willing to put up with it, then jumping out of a 'For' loop using a 'Goto' command is fine.


(c) Roy F. A. Maclean 28Feb1997, 17Sep1999, 28Sep1999
rfamgm at gmail
http://www.spiderpixel.co.uk/caspro