Day 08 (02/02)— Final Questions on Unit II; Unit III Introduction
Logistics
- Roll
- Questions? Wonderings? Comments?
Ready for Unit II Competency Demo?
Are there any questions about the Unit II practice items or the sample competency demo? The competency demo will be next Tuesday, I hope.
Boolean Data & Operators—Conditional Expressions
I did not follow these notes during class but they should be helpful.
In past discussion we have mentioned the "types" of data. Mostly data is either numeric or string in type. But there is a third type that is also critical or required for programming. Boolean data allows us to ask questions about the data and to make decisions based on the answers to our questions. But, we can only ask yes or no questions. Questions that can be answered as true or false
We want to ask questions such as:
- Am I failing the course?
- Do I fall below the obese threshold?
- Can I calculate pay the "regular" way?
- Is this the greatest value (seen so far)?
- Does this word come after that word alphabetically?
- Does the quantity of items ordered merit a discounted price?
- Did the user check the "contact me" check box?
- Does this person meet the requirements for the scholarship?
- Did the user enter an even number? (first, did s/he enter anything?)
- Did the user decide on a large pizza (check its radio button)?
- Did the user pick a size for her pizza?
- Is the user a senior citizen?
- ...
But we cannot ask the questions like that. We have to represent them in a way the computer can understand. We do that by constructing Boolean expressions.
As with the numeric and string expressions, Boolean expressions will make use of literals, variables, operators, properties, and functions. As a matter of fact the expressions we have already learned about will or can be part of a Boolean expression.
Boolean expressions produce a value of true or false. Often this is done by comparing two values, e.g., first greater than second, value #1 equal to value #2, age less than 21, etc. Sometimes we want to examine several conditions, e.g., height greater than 48 inches and weight greater than 60 pounds. There are operators that allow us to do that also.
We construct Boolean using: literals, variables, property values, numeric and string (data) operators (+ - * / ^ Mod &
), relational operators (< <= >= > = <>
), logical operators (And Or Not
), and parentheses. You also need to be thinking about how to evaluate the expression — how the computer will "calculate" the result. Questions/Comments?
When evaluating Boolean expressions do the data expression values first, then the relational, then the logical. The precedence/order of operations is:
- parentheses
- unary minus & exponentiation
(Be careful here! -4^2 produces -16, (-4)^2 produces 16, 4^-2 produces 0.0625)
- multiplication & division
- Mod
- addition & subtraction & concatenation
- comparisons — less than, greater than, equal to, less than or equal to, greater than or equal to, not equal to
Not
And
Or
Some Examples
- Am I failing the course?
Dim scoreTotal As Integer Dim result as Boolean scoreTotal = Val(txtInput1.Text) result = scoreTotal < 3 / 14 MessageBox.Show("Failing? :: " & result)
- Do I fall below the obese threshold?
Dim weight, height As Integer Dim bmi as double height = Val(txtInput1.Text) weight = Val(txtInput2.Text) bmi = weight / height ^ 2 * 703 MessageBox.Show("Below obese? :: " & bmi < 30)
- Can I calculate pay the "regular" way?
Dim hours As Double = Val(txtHours.Text) MessageBox.Show("Use regular formula? :: " & hours <= 40)
- Is this the greatest value (seen so far)?
Dim bigSoFar As Double = 0 Dim thisVal as double ' ... there will usually be other code here thisVal = Val(txtValueIn.Text) MessageBox.Show("Greatest so far? :: " & thisVal > bigSoFar)
Note that you could use either > or >= in the above code. There is subtle difference about whether it is the first or last copy of equal greatest values that sets the greatest.
- Does this word come after that word alphabetically?
Dim this As String = txtFirst.Text Dim that As String = txtNext.Text MessageBox.Show("""This"" after ""That""? :: " & this > that)
Note that when comparing strings, VB does a character by character comparison. When it get to two unequal characters in the same position the first or smallest one will be the one with the "smaller"/"first" of the two characters. Most computers use the ASCII (or UTF) coding schemes in which digits come before upper case characters which come before lower case characters. (Other characters occur before, between, and after these three groups of characters which occur in sequence.)
- Does the quantity of items ordered merit a discounted price?
Dim itemsOrdered As Integer = txtItemsIn.Text MessageBox.Show("Discount merited? :: " & itemsOrdered >= 100)
The cutoff for the discount, e.g., 100. (The prices could be also, but they are irrelevant to this task.)
- Did the user check the "contact me" check box?
MessageBox.Show("Contact requested? :: " & chkContact.Checked)
- Does this person meet the requirements for the scholarship?
My definition for meeting the scholarship requirements is: has a C average or better, has financial need (indicated by a checkbox), and wrote a application essay that was at okay or better (indicated by one of three radio buttons (marginal, okay, good).
Dim gpa As Double = Val(txtGPA.Text) Dim finanAidApproved As Boolean = chkAidApproved.Checked Dim essayOkay As Boolean = Not rdoEssay.Checked _ And (rdoEssayOK.Checked Or rdoEssayGood.Checked) Dim qualified As Boolean = gpa >= 2.0 And finanAidApproved And EssayOkay MessageBox.Show("Requirements met? :: " & qualified)
- Did the user enter an even number? (first, did s/he enter anything?)
This is usually two separate things. We would normally check to see if something was entered then check to see if it was an even number. But VB seems to be able to handle both at once, e.g.,
Dim isEven as Boolean = IsNumeric(txtValueIn.Text) And Val(txtValueIn.Text) Mod 2 = 0 MessageBox.Show("Requirements met? :: " & result)
Note that if you were to try to put the expression in the message box you would have to parenthesize the Boolean expression. Otherwise, VB will try to concatenate first and that would leave you trying to do an "And" with a string value and a Boolean value—ERROR! That does not compute.
- Did the user decide on a large pizza (check its radio button)?
MessageBox.Show("Large selected? :: " & rdoLarge.Checked)
- Did the user pick a size for her pizza?
Dim result As Boolean result = rdoSmall.Checked Or rdoMed.Checked Or rdoLarge.Checked MessageBox.Show("Size selected? :: " & result)
- Is the user a senior citizen?
MessageBox.Show("Senior citizen? :: " & Val(txtAge.Text) >= 65)
Next Time
- Work on conditional expressions; CD II.a