Chapter 6-6 : The Last Becomes the Second To Last Trick

 The Last Becomes the Second To Last Trick 

This last trick is used to detect two specific events in a series that follow each other directly. An example: in a sequence of words, if the word "Hello" is followed directly by the word "Goodbye".

The word read from the keyboard will be transferred to a variable we can call last. When the next word is read, this word is be copied into last, deleting the word read just before.

To avoid this, we use a second variable that we can call second to last. BEFORE reading the word that will go into last, we turn a copy of last into second to last so as not to erase the last word read.

The last important point is when the very first copy of last is turned into second to last when no word has yet been read. last must be initialized at the start of the program to avoid reading an empty variable. However, don't write just anything to last. There's a non-zero risk that you'll choose the word "Hello" from our example. If the first word read afterwards is "Goodbye", the program will stop even though only one word has been read, and this wouldn't make sense. If you're sure that these are the words that will be read, you can initialize last with signs such as "---" or "???".

Let's see how this could be done.

1. Initialize the variable last with the value "???"
2. REPEAT
3.      Copies the contents of last into the variable second to last
4.      Displays ("Give me a word")
5.      Reads the word and places it in the last variable
6. UNTIL second to last = "Hello" AND last = "Goodbye"
7. Display ("The two words have been read successively in order")

Now complete your challenge illustrating this trick by clicking on the word reader robot icon.

Good luck!