1. Students understand the general purpose of conditional (if-then) logic in real-life and programmatic situations
2. Students understand the block syntax of if-then logic within App Inventor
3. Students can apply if-then logic to create a choice of outcomes within their program
4. Students understand AND and OR boolean functions,
5. Students understand the block syntax of AND and OR booleans in App inventor
6. Students can apply AND and OR boolean functions to create more complex choices of outcomes within their program
Teacher should understand the basics of IF-THEN conditionals and BOOLEAN operators as outlined below. The teacher should be able to model all AppInventor Coding activity for the students shown in the slides.
If-then conditionals are used to create different outcomes within a program, depending on the state of one or more variables. For example, if the player score is more than 5 they win and the game is over, otherwise they keep playing. In code, IF Score > 5, THEN end game. As another example, if the timer equals 0, then the game is over. In code, IF Timer = 0, THEN end game.
The IF part of the code tests a condition, for example Score > 5, or Timer = 0. The ">" and "=" are comparison operators that return a value of either TRUE or FALSE. If the condition is TRUE the THEN part of the code block will execute, if the condition is FALSE, the THEN part of the code block with NOT execute. Conditionals can compare a variable with a number, a variable with a string, for example IF Input = "Quit", or it can compare two variables, for example IF Score > MaximumScore.
The THEN part of the code, determines what to do if that condition is met, in both of the above cases, end the game. In app inventor, the THEN block allows for one or more lines of code (blocks) to be executed. Whatever lines of code (blocks) are inside the THEN block will get executed if the IF statement evaluates to TRUE.
We can test multiple conditions using AND and OR with are BOOLEAN operators. Each operator takes a condition on the left, and a condition on the right, or example "Score > 5 AND Timer = 0". The AND operator evaluates to TRUE only if the both conditions on either side evaluate to TRUE. The OR operator evaluates to TRUE if either condition evaluates to TRUE. The teacher should understand the TRUTH TABLE for AND and OR operators.
* Advanced: There is a variable type called a BOOLEAN variable. This type of variable takes the value of TRUE or FALSE. So an IF statement can just check the BOOLEAN variable instead of using a comparison operation. We often refer to these variables as FLAGS that are either on (TRUE) or off (FALSE). If "EndGameFlag" is a BOOLEAN variable, we can set it to TRUE or FALSE, and we can have an if statement that reads IF EndGameFlag THEN end game.
Activity 4.12.b.1 (Budget 50 minutes)
Students learn to apply conditional if-then blocks to create alternative outcomes in their program.
1. Present the first slide of the Conditionals PowerPoint and play a short game of Simon Says with the class. The teacher should ask the students how the game works, eliciting a WHEN-DO, and and IF-THEN. Here are the rules of Simon says in code:
WHEN Simon speaks
DO
IF Simon starts with "Simon says"
THEN player perform the action Simon specifies
WHEN player does something
DO
IF Simon did NOT start with "Simon says"
THEN player is out
2. Walk students through slide 11 to show the coding process for adding an IF-THEN block in APP INVENTOR. Then model this in APP Inventor live for the students, making sure the students follow in AppInventor. Make sure students they can find and apply the IF-THEN and CONDITIONAL blocks.
3. Test student knowledge of conditionals in class with slide 12.
4. Where do we put the IF-THEN? In the Simon Says game we "nest" the IF-THEN inside of an event (a WHEN-DO). As a rule of thumb: whenever an EVENT (WHEN-DO) change a VARIABLE THAT IS INVOLVED IN A VICTORY CONDITION (like the score goes up), we want to check right after that happens inside of that EVENT.
See the example for SPACE PONY, slide 13. Note, that example provides a nice way to finish the game with a message, instead of just closing the app. We will cover how to do that in sections c and d.
Activity 4.12.b.2 (Budget 40 minutes)
Students learn to use AND and OR blocks to make more complex IF-THEN statements
1. Present the first 9 slides of the AND-OR PowerPoint to motivate the need for and show the coding process for the BOOLEAN operators AND and OR. Then model this in APP Inventor live for the students, making sure the students follow in AppInventor. Make sure students they can find and apply the AND and OR blocks inside of an IF-THEN statement.
2. Test student knowledge of AND and OR operators in class with slide 10. The teacher may want students to work on this independently or as pairs at first, and write their answers down, and then discuss as a class. Since each answer is either TRUE or FALSE, the class can also vote on which they think the correct answer is.
After the exercise explain the TRUTH TABLE on slide 11. The TRUTH TABLE will help clarify the difference between when AND is TRUE versus when OR is TRUE. From the TRUTH TABLE It should be clear that OR is TRUE more often than AND is TRUE.
4.12b.1 Conditionals PowerPoint (Teacher Resource)
4.12b.2 AND-OR PowerPoint (Teacher Resource)