Search code examples
ti-basic

For loop only going through one iteration


I am trying to create a program that simplifies radicals in TI-BASIC. However, one FOR loop seems to be only completing one iteration while the rest work fine. Here is the code:

Input "Root=",A
Input "Radical=",B
B→Z
ClrList L₃
prgmPRMNTOL1
prgmGETPRIME

The FOR loop in question starts here...

For(Y,1,10)
0→Z
For(X,1,dim(L₂))
If L₁(Y)=L₂(X)
Then
Z+1→Z
End:Disp Z
End
If Z≥A
Then
int(Z/A)*A→C
int(Z/A)→D
For(T,1,D)
L₁(Y)→L₃(1+dim(L₃))
End
For(R,1,C)
ClrList L₄
For(S,1,dim(L₂))
If L₂(S)=L₁(Y) and C>0
Then
–1→L₂(S)
C-1→C
End
End
For(Q,1,dim(L₂))
If L₂(Q)≠–1
Then
L₂(Q)→L₄(1+dim(L₄))
End
End
ClrList L₂
For(Q,1,dim(L₄))
L₄(Q)→L₂(Q)
End
End
End

...and ends here.

1→E
For(M,1,dim(L₃))
E*L₃(M)→E
End
1→F
For(N,1,dim(L₂))
F*L₂(N)→F
End
Disp "OUTSIDE",E,"ROOT",A,"INSIDE",F

The program works perfectly besides the fact that this one loop only runs once. I will post prgmPRMNTOL1 and prgmGETPRIME as well if necessary, though they probably do not have any impact as they modify only L₁ and L₂.

EDIT: Added a indented version.

Input "Root=",A
Input "Radical=",B
B→Z
ClrList L₃
prgmPRMNTOL1
prgmGETPRIME

For(Y,1,10)
    0→Z
    For(X,1,dim(L₂))
        If L₁(Y)=L₂(X)
        Then
            Z+1→Z
        End
    End
    If Z≥A
    Then
        int(Z/A)*A→C
        int(Z/A)→D
        For(T,1,D)
            L₁(Y)→L₃(1+dim(L₃))
        End
        For(R,1,C)
            ClrList L₄
            For(S,1,dim(L₂))
                If L₂(S)=L₁(Y)
                Then
                    –1→L₂(S)
                End
            End
        For(Q,1,dim(L₂))
            If L₂(Q)≠–1
            Then
                L₂(Q)→L₄(1+dim(L₄))
            End
        End
        ClrList L₂
        For(Q,1,dim(L₄))
            L₄(Q)→L₂(Q)
        End
    End
End

1→E
For(M,1,dim(L₃))
    E*L₃(M)→E
End

1→F
For(N,1,dim(L₂))
    F*L₂(N)→F
End

Disp "OUTSIDE",E,"ROOT",A,"INSIDE",F

Solution

  • Close Your Loops

    Your issue seems to stem from incorrectly matching your If statements, loops and their End statements.

    What's Happening

    The behavior of TI-Basic when end of file is reached before all loops and If statements have been closed is to fail silently, terminating the program, rather than alerting the users of an error.

    This behavior makes certain odd pieces of code syntactically valid. For instances this snippet would run without error:

     For(A,1,10
         Disp A
    

    The results being

                   1
                Done
    

    Obviously this is the same as running Disp 1 so, there's no reason to use this an your code. It serves only to make silent and annoying errors appear in code.

    The Fix

    Assuming the indented version of your code represents how you want your loops to be nested, the code below should fix your problem.

    Input "Root=",A
    Input "Radical=",B
    B→Z
    ClrList L₃
    prgmPRMNTOL1
    prgmGETPRIME
    For(Y,1,10)
    0→Z
    For(X,1,dim(L₂))
    If L₁(Y)=L₂(X)
    Z+1→Z
    End
    If Z≥A:Then
    int(Z/A)*A→C
    int(Z/A)→D
    For(T,1,D)
    L₁(Y)→L₃(1+dim(L₃))
    End
    For(R,1,C)
    ClrList L₄
    For(S,1,dim(L₂))
    If L₂(S)=L₁(Y)
    –1→L₂(S)
    End
    End:"Inserted this End
    For(Q,1,dim(L₂))
    If L₂(Q)≠–1
    L₁(Q)→L₄(1+dim(L₄))
    End
    ClrList L₂
    For(Q,1,dim(L₄))
    L₄(Q)→L₂(Q)
    End
    End
    End
    1→E
    For(M,1,dim(L₃))
    E*L₃(M)→E
    End
    1→F
    For(N,1,dim(L₂))
    F*L₂(N)→F
    End
    Disp "OUTSIDE",E,"ROOT",A,"INSIDE",F
    

    If that doesn't fix it, the missing End is elsewhere in your code.