GoalCompare a received word with an expected word, and know that capital letters matter.
New words
string · text, such as the word comecase-sensitive · capital and small letters count as different, so Come is not the same as comenested if · an if block placed inside the else part of another if
Explanation
A micro:bit can send words as well as numbers. radio send string sends the word, and the receiver holds it in receivedString. A receiver can compare the word with the one it expects, for example receivedString = come. The comparison is case-sensitive: Come, with a capital C, is a different string. To test for several words, put another if inside the else part. The receiver tests the first word. If that is false, it tests the second. If both are false, a final else can handle every other message, for example by showing the word. Only one branch runs for each message, and the order of the tests is the order in which they are checked.
receivedString
Same as come?
come
true
Come
false (capital C)
COME
false
come (with a space after it)
false
Worked examples
A dinner signal between the garden and the kitchen. Button A sends the word come and button B sends the word wait. The receiver shows a yes icon for come, a no icon for wait, and the word itself for anything else.
Who sends
Word sent
Receiver shows
press A
come
yes icon
press B
wait
no icon
a third micro:bit sends
Come
string Come
Tracing three messages. The third message is sent by another micro:bit, in the same group, by mistake with a capital C.
The same decision as a flowchart. The second diamond is the nested if.
Class activity
Word cards (no computer). Write the words come, wait, Come and stop on separate cards. One player is the receiver with the rule from the lesson: come gives the yes sign (thumbs up), wait gives the no sign (a cross with arms), anything else gives the word read out aloud. The other player holds up the cards in a mixed order. Write the answers in a table. Check that Come with a capital letter does not give the thumbs up. Then change the rule to use the words start and finish and play again. If you have a computer or tablet, ask an adult to help you build the first receiver.
Practice
1
Using the dinner receiver, what is shown when the message is wait? What when it is stop?
wait shows the no icon. stop shows the word stop, because it is neither come nor wait.
2
A sender sends the word Wait with a capital W. The receiver tests for wait. What is shown, and why?
It shows the word Wait. The comparison is case-sensitive, so Wait is not equal to wait, and the else branch runs.
3
Write the receiver blocks for this rule: the word start shows the yes icon, the word finish shows the no icon, and any other word shows the sad icon.
on radio received (receivedString): if <(receivedString) = [start]> then show icon [yes v], else if <(receivedString) = [finish]> then show icon [no v], else show icon [sad v].
4
A pupil puts the test for wait before the test for come. Does this change what happens to the words come and wait? Explain.
No. The two words are different, so only one test can be true, and the result is the same. The order matters only when more than one test could be true for the same message.