The Fibonacci Sequence 3

To make this program, we need to learn about something called variables. A variable can store a value, you can update it, and you can access it again later. For example, let's say I have a variable named x. I say x = 5, then I say x = x+1. Then, the value of x at the end will have become 6. Variables are very useful when programming.

In this case we want several variables: one to store the second-to-last element in our sequence so far, one to store the last one, and one to put the new value we calculate in! In Scratch, variables can be found under the red variables tab.

Your task: Create three variables, called "second to last", "last", and "new". When the green flag is clicked, set "second to last" to 0 and "last" to 1, which are the first two numbers in the sequence.

Now what?

Back to the index