## Casio Graphical Calculator Encyclopaedia

 Construct: For..To..Step..Next Type: Looping construct Usage: ``` For -> To [Step ] Next```

### Definition:

Carries out this process:

• 1) Assign <start> to <var>.
• 2) If <start> is greater then <end> (or less than <end> if <step> is negative) then goto step (7).
• 3) Carry out <statements>.
• 4) If <var> + <step> is greater than <end> (or less than <end> if <step> is negative) then goto step (7).
• 5) Increment <var> by <step>.
• 6) Goto step (3).
• 7) Exit the loop and pass control to the statement following the Next.
If <step> is not specified, it is taken to be 1.

### Other information:

The variable <var> is not read-only during the loop, so it is possible to severely alter the way in which this construct works. For example, this code will result in an infinite loop:

```  For 1 -> A To 2
A - 1 -> A
Next
```

Changing the value of <step> from within the loop causes similar results.

Note also that unlike many other programming languages, in a simple For..Next loop, when the calc exits the loop the value of <var> is between <end> (inclusive) and <end> + <step> (exclusive).

### Bugs and features

This is the most complicated looping structure and as a result has a large number of bugs.

The basic problems which have been found so far with For..Next are detailed below.

#### Using Break in nested Fors

The Break command transfers control to after the first Next it finds. This will often not take you out of the current For..Next, as shown here:

```  For 1 -> A To 2
Break
For 1 -> B To 2
"FOO"
Next
"BAR"
Next
```

This will result in the output "BAR", followed by a syntax error caused by the second Next, which it considers to be unmatched, having jumped to after the first Next, rather than the second. This also affects the other looping constructs, but as it is most common to use nested Fors I have mentioned this here.

#### Skipped Fors

If <end> is initially less than <start> (or greater than <start> if <step> is negative) then the loop will be skipped. As in the previous bug, however, it will jump to after the first Next which it finds (even if it is commented out!).

#### Jumping out of Fors using Goto

Once the calculator decides that it is in a For..Next loop, the only things which will convince it otherwise are:
• Exiting the For..Next normally, due to <var> being greater than (or less than if <step> is negative) <end>.
• Executing a Return command.
• Executing a Stop command.
• Encountering an error or having [AC] pressed.
This means that code such as this will cause an error:

```  For 1 -> A To 10
A = 4 => Goto 0
Next
Lbl 0
```

This is because when the calculator gets to the Lbl 0 it still thinks that it is in the loop, so it expects to find a Next. This is not strictly speaking a bug, as it makes logical sense and may even be desirable.

In a simple loop such as the one shown above, this is not a problem, as you can replace the Goto 0 with a Break. Problems arise if:

• You want to jump out of a nested For..Next construct.
In either of these cases, you are usually best to try to arrange your code so that the calc exits the loop properly. In the first case, this would probably involve something of the form:

```  For 1 -> A To 10
A = 4 => 99 -> A
Next
A = 99 => Goto 0
...
...
Lbl 0
```

Here 99 is not just being used as a flag, but also to exit the loop. When the calc reaches the Next, it realises that A+1 is more than 10 and exits. Because of this, you would need to jump to just before the Next using an If or a Goto if you had other statements between the 99 -> A and the Next.

In the second case, a similar but slightly more complicated method should be used:

```  For 1 -> A To 10
If A = 4
Then 99 -> A
Else For 1 -> B To 2
"FOO"
Next
"BAR"
IfEnd
Next
```

This demonstrates the use of the jumping to before the Next using an If as mentioned above.

The problems described here are caused by a bug in the calculator which affects all of the looping constructs. It is currently explained under Break, but I may move it to its own section at some point.