Logistics
- Roll
- Questions? Wonderings? Comments?
Unit II Review
The grade book is posted. (Those who haven't sent me a code may wish to do so.) Remember that I am checking for "competency". That means I was able to conclude that you were able to carry out the tasks as specified. There is also a measure of being able to perform in the time allowed—that you are able to use the skill/knowledge.
Please come to office hours or set up an appointment to go over the competency demo. Some of you need to do so to resolve question marks.
Remember, you must "pass" CD II before you can retake later competency demos
Questions? Comments? Wonderings?
Boolean Data & Operators—Conditional Expressions
As I indicated last time, the next topic is conditional expressions (or Boolean expression— capitalized because the name comes from the mathematician George Boole). Boolean data is another kind of data that can have only two values—true or false. (Actually, Boolean variables can be undefined also, which should cause an error.) We use Boolean expressions to determine the state of program data (and, thus, our program). Then, eventually we (our code) will make decisions based on the program state as indicated by the Boolean expression.
Boolean variables or expressions appear in three places—assignment statements, selection/if
statements, and repetition/do
statements.
- assignment statement
An assignment statement involving Boolean data will have the form
booleanVariable = booleanExpression
where
booleanVariable
has been declared as a Boolean variable, i.e.,Dim booleanVariable as Boolean
and
booleanExpression
is a literal (True or False), (Boolean) variable, or collection of numeric (or string) variables, operators, and functions with some relational or logical operators (see below). - selection/
if
statementA Boolean expression will replace true in the following code.
If true Then 'perform actions located here Else 'perform other actions located here End If
Note that the "Else" part of the
If
statement is optional.Your job as a programmer will be to produce a Boolean expression that produces the desired result (of true or false) that will be true when you want the first set of actions to be performed.
- repetition/
Do
statementA Boolean expression will replace true in either of the following code segments,
Do Until true 'perform actions located here Loop
or
Do While true 'perform actions located here Loop
Your job as a programmer will be to produce a Boolean expression that produces the desired result (of true or false) that will be true when you want the loop to stop (
Do Until
) or continue (Do While
).
This unit focusses on the evaluating and producing Boolean expressions that indicate some program state. Questions? Comments? Wonderings?
Program State
Data in a program represents the problem we are attempting to solve. At any given point in time, the data values represent the state of the program. Our program will want to manipulate the data (change the program state), and/or take some action depending on the state (If
or Do
statements). When Boolean data is involved we can think of the situation as asking (and answering) questions of the data. The number and kind of program states (questions we might need to ask) is unlimited. That makes it a little hard to talk about.
To determine whether a particular program state exists, we need to be able to think about the program state in terms of the variables (data) that define problem we are working on, and on "manipulations" of that data. The manipulations are arithmetic operators, numeric functions, and string operators & functions that we have seen in Unit II. And now, we need to become familiar with additional operators—Boolean operators.
Boolean operators
There are two types of Boolean operators: 1) relational operators that compare two values (two numeric values or two string values) and 2) logical operators that combine two (Boolean) values.
- Relational operators
Compare two values of the same kind, i.e., compare two numeric values or compare two string values. They produce a value of true or false.
They are: <, <=, =, >=, >, and <> [ Name, discuss, and respond to questions. ]
- Logical operators
(Most often) Combine two Boolean values. They produce a Boolean value (true or false).
They are: And, Or, and Xor There is also the Not operator that works on a single Boolean value much like the – (negative) operator works on numbers. [ Name, discuss, and respond to questions. ]
- Precedence of operators
Relational operators have a higher precedence than the logical operators and a lower precedence than the arithmetic and string operators. They each have the same precedence.
Logical operators have the lowest precedence and are not the same. Not has the highest precedence, followed by And followed by Or.
As with arithmetic operators you can use parentheses to clarify or change the order in which elements of the whole expression would get evaluated.
Boolean/Conditional Expressions
As with numeric and string data, Boolean data has "expressions"— combinations of literals, variables, operations, functions, and parentheses that produce a single value. Examples for the questions notes above are:
- Can pay be calculated the "regular" way (without overtime)?
hoursWorked <= 40
- Is "this" the greatest value seen (so far)?
currentValue >= greatestValue
- Does "this" word come after "that" word alphabetically?
word1 > word2
- Does the quantity of items ordered merit a discounted price?
itemsOrdered >= 10
- Did the user check the "contact me" check box?
chkContact.Checked
- Did the user decide on a large pizza (click its radio button)?
rdoLarge.Checked
- Is the user a senior citizen?
age >= 65
- Did the user enter anything at all?
txtInput.Text.Length > 0
- Is this student failing the course?
scoreTotal < totalPossible * (4 / 14)
- Did the user enter an even number?
Val(txtInput.Text) Mod 2 = 0
- Am I obese?
Val(txtWeight.Text) / Val(txtHeight.Text) ^ 2 * 703 > 30
- Does this person meet the requirements for the scholarship?
isNativeAmerican.Checked And (Val(txtGPA.Text) >= 3.2 Or Val(txtACT.Text) >= 29)
- Is the craps game, do we remain in or move to the come-out phase of play?
(inComeOut And _
(roll = 2 Or roll = 7 Or roll = 11 Or roll = 12)) _
Or (Not inComeOut And Not (roll = point Or roll = 7))
Any questions/wonderings about the material above? [ Respond to questions/comments. ]
Unit III Practice/Learning Activity
[ Display and discuss the learning/practice activity. Respond to questions/comments. ] (When no further questions/discussion, students work on the activity ]