Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.

Similar presentations


Presentation on theme: "Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop."— Presentation transcript:

1 Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop

2 Conditional Branching - Booleans Session SA-2

3 Conditional Branching We encounter situations that employ conditional branching techniques every day. It all comes down to choices. We often have two or more things to do at a time and must decide upon one and only one. When faced with choices such as these, we must evaluate the reasoning for each and make a decision based on a condition. For example, say you bring $300 to the mall to buy a new outfit. You narrow it down to two selections; the first costs $500, but the second costs $200 and isn’t as nice. You think to yourself “If I have more than $500 I will buy the nicer outfit. Otherwise, I will buy the other outfit.” This is a perfect example of a conditional branching.

4 Conditional Branching Conditional branching is used to alter the normal flow of execution depending on the value of a Boolean expression (Boolean expressions are explained in the following slide). The use of conditional branching allows programs to become much more flexible. The primary block associated with this behavior is the if/then-do block or the ifelse/then-do/else-do block The test “If” or “ifelse is called the predicate or conditional part of the structure and is where the Boolean expression is placed. This is what determines if the consequent (internal), or alternate (else) blocks are executed.

5 Boolean Expressions If blocks are controlled by Boolean expressions located in the predicate area. The ‘test’ component will evaluate to either true or false. By default, the if block is created with an empty test. The test must be set to True, False, a Boolean variable or a Boolean expression. It can also be set to a procedure that returns a Boolean result (True or False). The Boolean Logic operators for Boolean expressions are found in the Built-In Logic in the Blocks Editor. If the expression in the predicate evaluates to “True,” the blocks located below “If” ARE executed, and the blocks located below “Else” are NOT executed. Conversely, if the expression evaluates to “False”, the blocks located below “If” are NOT executed, and the blocks located below “Else” ARE executed. ‘=‘ can be replaced with these operator

6 Conditional Branching – If/Else Block Let’s take a look at an example: This if/then-do/else-do block compares two numbers using the ‘=‘ operator (compares the number 1 to itself). In the example above, 1 does equal 1 and the test evaluation is true. Since the ‘test’ is true, the “then-do” branch will be executed (set the text of a Label to ‘This is true’). The branch ‘else-do’ will not be executed.

7 Conditional Branching – If/Else Block Let’s take a look at an the same example that evaluates false: This if/then/else block compares two numbers (asks if 1 is greater than itself). In the example above, 1 is not greater than 1 and the test evaluation is false. Since the ‘test’ is false, the “then-do” branch will not be be executed. The branch ‘else-do’ will be executed, setting the label text to ‘This is false’

8 Conditional Branching – If/Else Block In many cases, the if/then/else blocks compare two variables whose value are set by your program and/or user input. It is important to keep track of what is being compared. The App Inventor will allow comparisons of different types of data if they are compatible but will raise an error condition if they are not.

9 Conditional Branching – Nesting The if/then/else decision making can be nested, several comparisons maybe needed in your program to obtain the desired result and can be nested several layers deep. These nested if/then/else blocks determine if two variables are equal, greater or less than one another and set the label text accordingly.

10 Sample 2 Conditional Branching Coin Flip

11 Project 2 Conditional Branching Ask a question of the user and have them input the answer into a textbox, then tell the user if the answer was correct or not.


Download ppt "Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop."

Similar presentations


Ads by Google