Chapitre 6-6 : the last becomes before-last trick

 The last becomes before-last trick 

This last trick is used to detect two specific events in a series that follow each other exactly. For example, in a sequence of words, if the word "Hello" is followed exactly 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 will also be copied into last, deleting the word read just before.

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

The last important point is the very first copy of last into before-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, which 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 before-last
4.      Display ("Give me a word")
5.      Read the word and places it in the last variable
6. UNTIL before-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 work.