5 - Advanced Boolean Logic & Lists

advertisement
Programming with App Inventor
Computing Institute for K-12 Teachers
Summer 2012 Workshop
Advanced Boolean Expressions
and Lists
Session SA-5
Boolean Review
Boolean Logic Operators

The App Inventor provides for several implementations of Boolean expression shown
below. The are found under the Built-In Logic section in the Blocks Editor, in the Math and
Logic drawers. There are some Text comparison operators under the Text section as well.

A Boolean variable must have a value of either

You can negate

You create a Boolean variable as you would any normal variable but you must set the
variable’s value to either the true object, the false object (
,
)
or the result of a Boolean expression.

The operands for the Boolean operators (not , and , or) must use a Boolean value (true,
false, a Boolean variable, or a Boolean expression).

The Boolean and (
Boolean variables.

This sample show the use of the and operator comparing two Boolean variables. The and
operator return true if all the ‘test’ are true. The test is done from top to bottom until the a
false value is encountered at which time a false value will be returned.
, or
, or and
or
.
Boolean variables.
) will compare a series of Boolean values (true, false, or
Boolean Review
Boolean Logic Operators

The or operator compares a series of Boolean values or Boolean expressions until
a true result is found. At that time the or block will return a true value. A false value will
only be return if all the comparison are false.

Boolean Expressions are calculations that evaluate to either True or False. The comparison
block returns a Boolean result. The variables ‘a’ and ‘b’ represent any expression that
return a result that can be used by the comparison operators.

The comparison operator block can be found in Built-In tab of Blocks Editor under the Logic
and Math Sections as well.

If you need to compare text strings use the ‘text=‘ comparison block show below. The
comparison operators for strings are in the Built-In Definition under the Text section.
Lists
 Lists (also known as Arrays) are an essential programming tool.
Lists can store multiple values (called elements) that the
programmer may need later in their program. Creating a List, in a
way, is like creating multiple variables. For example, if we were to
have a List of length 5. That means we have 5 separate values
held within the List.
 Each value in the List is indexed by a number. With the App
Inventor, indices begin at 1, so the first element is indexed with
the number 1 and the second element is indexed with the
number 2. Indexing is the way we keep track of where a specific
value is located in the List.
 The length of a List needs is determined by the number of value
added to the List. There many routine available in the Built-In Tab
of the Blocks Editor under the Lists section (or drawer).
 For example, is the length of list function
will return the number of items in the given List.
which
Creating an List
 Creating an List is similar to creating a variable. Use the
Blocks Editor Built-In Definitions to create a variable.
 Once you have created the variable you can assign its
initial value to the function make a list. The List functions
are found in the Built-In tab under the Lists section in the
Blocks Editor.
 The blocks below show a variable created from the Built-In
Definitions, renamed to numList and then assigned to an
initial value from the function call make a list, thus making
an empty List that can be later populated with items in you
program.
Lists
 The are many functions that can be used with a List. The
graphic below shows the predefined blocks used to work
with Lists. These are located in the Blocks Editor Built-In
Tab under the Lists section.
Lists
 The follow block show an simple example of adding items to a List.
 The for range loop cycles from 1 to 10.
The index variable ‘i’ increments by 1
increasing from 1 to 10 with each
iteration.
 Each value of ‘i’ (1 to 10) will be added
as an item in the List named numList.
When the for loop exits, numList will
have 10 items with the value of 1
through 10. The first item will have a
value of 1, the 2nd item will a value of
2 and so on.
 Although, this example has all the items in the list as a number, List
items may be of any type.
Project 6
 Following the samples example, create your own quiz with
a few differences. Add an image for each question you ask,
and have one of the questions have multiple answers.
Day 3 Questions
1. In the Blocks Editor, how (or where) do you define a procedure? (Hint: same place
you define a variable).
2. You have a program that has an If/Else structure, and two variables, A and B. The
only time you want the alternate part of the If/Else structure to execute is when
both variables equal 10. What Boolean expression could you use in this situation?
3. What are the two types of procedure calls in App Inventor? What is the difference?
4. Can an argument be used with both types of Procedures (___yes/___ no)?
5. What structure in App Inventor could you use to store a series of items?
6. Name two control blocks that can be used in the App Inventor?
7. What block is used to define an argument passed into a procedure?
Day 3 – Answer Key
1. In the Blocks Editor, how (or where) do you define a procedure? (Hint: same place
you define a variable).
In the Blocks Editor from the Definitions section (drawer)
2. You have a program that has an If/Else structure, and two variables, A and B. The
only time you want the alternate part of the If/Else structure to execute is when
both variables equal 10. What Boolean expression could you use in this situation?
(not ((A = 10 ) and (B = 10)))
3. What are the two types of procedure calls in App Inventor? What is the difference?
One that returns a value and one that does not.
4. Can an argument be used with both types of Procedures (_X__yes/___ no)?
5. What structure in App Inventor could you use to store a series of items?
a List object
6. Name two control blocks that can be used in the App Inventor?
if, ifelse, foreach loop , for range loop , while loop
7. What block is used to define an argument passed into a procedure?
the named argument object , created from the Built-In Definitions in the
Blocks Editor
Download