Smalltalk Fibonacci
for-loop, gnu-smalltalk, smalltalk, syntax
Solution
You have an extra set of brackets in your loop body, which makes it create (but not execute) a block on each loop iteration. Here's what you meant to write:
2 to: n do: [ :i |
result := (parent + gparent).
gparent := parent.
parent := result.
'come on, do something' displayNl.
].
Problem
I have to use Smalltalk to return the nth Fibonacci number, I haven't used this language before. This program returns 1 to any input and I don't know why. It didn't even iterate the for loop I think. Could someone help me? Thanks. ``` 'Which fibonacci number do you want? (n>2)' printNl. n := stdin nextLine asInteger. (n <= 2) ifTrue: ['Type a larger number, F(1) and F(2) equals 1!' displayNl.] ifFalse: [ result:= 1. parent := 1. gparent := 1. 2 to: n do: [ :i | [ result := (parent + gparent). gparent := parent. parent := result. 'come on, do something' displayNl. ] ]. result displayNl. ]. ```