In the real life we are constantly making different decision based on different factors. For example, making a decision based on weather: if there will be raining I'll stay at home, otherwise I'll play outside. I guess that without any problems you could give some other examples making decisions based on various conditions.
As you can see, every of these examples rely on a piece of information that will let us figure out whether condition (is it raining?) is fulfilled or not.
- If there is a mess in my room I have to clean it up
- If I am hungry I have a quick snack.
- If I am sick I will stay in bed
Similarly in programming - to make our program smarter, we let it decide how to act if certain situations. In Scratch we have if ... then and if ... then ... else command blocks which can be translated to the humans speach as in following example:
To make our example way more colorful, lets move to the magical world of Franky the Mouse. I have provided you with a file containing a scene and Franky standing in the middle you can fetch from here.
First thing, after loading the scene make sure that we have selected Franky's sprite, since we are going to create a code for our mouse.
Here he is, standing in the front of signpost, Franky is getting back from his Grandmas house early this morning. Since he did not woke up entirely, he is wondering what he should do since in the week he goes to the school and during the weekend then he should go drectly to his home, where his parents are waiting for him. Fortunatelly, he has you to tell him whether it is Weekend or not, so that he can make a right decision.
First, we should allow us to answer Franky's question. Let's jump to the code editor and drag Following list of blocks to the code area of Franky:
You can enter response to the input box, but after hiting enter nothing is happening. This is because we did not said Frank what to do in certain cases. As a next step we add if-else command block which will let Franky to check our answer. If we will say yes then Franky will go home. Otherwise our small mouse will go directly to the school to meet his friends.
Hopefully you did have some fun with letting Franky make his choices. Stay tuned for a next bunch of scratchy news!









.gif)
Comments
Post a Comment