Introduction to Programming Logic/Lesson 2: Conditionals and Logic
Conditionals and logic
Let'as take the robot idea. You have a set of commands for him. If you tell him anything else other than the commands he knows, he does his "do the locomotion dance"
The commands available are Options 1-4
*Option 1 - the robot follows you and has a light that fills the area right in front of you *Opton 2 - the robot follows you and plays your favorite theme song ("Bright side of Life" from Monty Python's Life of Brian) *Option 3 - he writes down everything you say into a file taht can be uploaded to your computer using his USB port *Option 4 - He goes to his power base and parks in order to recharge his batteries.
Remember - if you say anything other than 1, 2, 3, or 4, he will do the locomotion dance.
This means you actually have 5 options: The 4 commands and the "anything else" for a total of 5 options. Each one of these options contains a conditional expression, and an action based upon the result.
An IF statemnet allows you to program the robot to think about the command, and make the correct decision. Verbally, this might be expressed as "If I say 1, the robot will follow and shine his light. If I say 2, the robot will follow me and play my song. If I say 3, the robot will write down everything I say and store it. If I say 4, the robot will recharge. Otherwise, the robot dances the locomotion."
Each of these expressions can be answered with a TRUE or a FALSE (A Boolean value) if rephrased as "IF it's TRUE that I said 1"... IF it's TRUE that I said 2"... If it's TRUE I said something else..."
Now, each expression has a condition - they MUST evaluate to TRUE or FALSE. Now the robot can be taught to make decisions.
As for the mysterious option 5, we'll just refer to that as an "ELSE" ....
input user (voice recognition command+case_num) ( for (case_num);
( if case 1 then do lighting routine if case 2 then play <theme_song_user> if case 3 then run note taking program if case 4 then move home connect to power enter sleep mode else do <do the locomotion routine> )
) return 0;
___________________________________________________ now scrappy has a definite action to take for any "case" command given to him
assuming his "do the locomotion routine" has a do until condition
and scrappy has voice recognition where he has specific values recorded for those allowed