I just converted the Fibonacci's sequence from python3 to TI Basic but its acting kind of weird when I try to execute the function similar to "+=" operator to the iterator variable in python where I use the store function. Here is my TI code below:
Input "Enter number of terms: ",N
0→A
1→B
0→I
If N≤0:Then
Disp "Please enter a positive integer"
End
If N=1:Then
Disp "Fibonacci sequence upto",N
Else:
Disp "Fibonacci sequence:"
For(I,0,N-1)
Disp A
A+B→C
B→A
C→B
I+1→I
End
End
where when I input 3, it gives me an output of:
0
1
rather than
0
1
1
The code works if I either remove or change this line:
I+1→I
to
I→I
Is there a reason why this causes the for loop to ignore one iteration instead of starting from this iteration? Here is the corresponding python code:
#Fibonacci Sequence
N = int(input("Enter number of terms: "))
A= 0
B = 1
I = 0
if N <= 0:
print("Please enter a positive integer")
if N == 1:
print("Fibonacci sequence upto",N)
else:
print("Fibonacci sequence:")
for I in range(N):
print(A)
C = A + B
A = B
B = C
I += 1
(I know that the I += 1 is unnecessary but it just made me curious why it doesn't work in the TIBasic language)
Original python fibonacci sequence code: https://www.programiz.com/python-programming/examples/fibonacci-sequence
The for loop in ti basic iterates i by doing i = i+1.
The for loop in python sets i to a value from a sequence generated by range.
in other words the ti code is more similar to
for(let i=0; i<n; i++){
}
and the python script is more similar to
let n = [1,2,4,5, ... n]
for(let ni=0; ni<n.length; ni++){
let i=n[ni];
}
You can see how in the python example above, 'i' gets reset to a value from a list each iteration. where as in the ti example 'i' is used to keep track of the iteration state.