Logistics
- Roll
- Today: CD IIIa review; Unit IV introduction
- questions/comments
Review Unit III Competency Demo
- Review each item
- Respond to questions
Selection
At the start of the course, I mentioned the basics of computing—data & actions and the basic ways of organizing actions—sequence, selection, repetition, and modularization. Programming is figuring out what actions are needed and then organizing them in a way that accomplishes our overall task.
- sequence
This entails knowing each action and its dependencies (what it depends on and what depends on it). The things it depends on must come before the action and the action itself must come before those things that depend on it.
- selection
Selection is deciding which action to take in a given situation or perhaps to take no action at all. Again, the action may depend on something happening before it in order to take the action or to decide whether to take the action. Things that depend on the action being taken (or not) must be placed after it.
- repetition
Repetition simply repeats some set of actions. Again, some actions may need to occur before the repeated actions or in order to test for the repetition and some actions may need to be sequenced after the repetition.
- modularization
Modularization is merely putting some actions together and giving them a name. This allows you to "see" the action without getting bogged down in details. It also allows you to take the action from different places in your program without duplicating the code
So, what does today's topic, "selection" look like. You are familiar already with the idea of if something is true, do something. That is selection. In programming we have an If
statement; actually we have several variations of the If
statement. The two basic forms are:
' actions required before the IF If true Then ' set of actions End If ' actions required after the IF
and
' actions required before the IF If true Then ' set of actions Else ' alternate set of actions End If ' actions to occur after the IF
Questions? ... Let's explore the various parts and see what we can deduce about the If
statement.
- If True
- what does this mean?
- what do we put in place of "True"?
- questions/comments?
- ' set of actions
- what does this mean?
- what do we put here?
- questions/comments?
- ' alternate set of actions
- what does this mean?
- what do we put here?
- questions/comments?
That is how the if
statement works. Questions? Comments? Wonderings?
There is one particular way of using if
statements that you should be aware of—as a flag. Sometimes there are multiple occasions to test a condition and any or all of the tests could determine the action to be taken when all tests are completed. The pattern here is something like the following.
Dim okay As Boolean = True ... If <bad test 1> Then okay = False End If If <bad test 2> Then okay = False End If . . . If okay Then ' do okay action else ' do NOT okay action End IF
Examples
Next, we will look at slightly more complex examples and alternatives for doing them. Let's look at an example involving a student asking about their grade (its a silly example, but one we are familiar with). Even a simple problem like get a score from the user and determine the grade involves some thinking in order to code it correctly. Some considerations are:
- Did the user enter a number for her/his score?
- How do I figure out what the grade is?
- What do I need to know or do to be able to respond to the user?
- What, if anything, needs to happen before the IF
- What will my IF statement look like? (its actions)?
- What conditional expression do I need for the IF?
- ...
So a simple IF statement could check to see if the user entered a number for a score. Typically we want to do something with the score once we know the input is okay. That would go in the ELSE part of the IF we did above.
Here is some example code—what was done in class may differ.
- Grade determination: sequence of
If
sDim valueIn As String = txtScore.Text Dim score As Double Dim grade as String If IsNumeric(valueIn) Then score = Val(valueIn) If score >= 90 Then grade = "A" End If If score < 60 Then grade = "F" End IF If score >= 80 And score < 90 Then grade = "B" End If If score >= 70 And score < 80 Then grade = "C" End If If score >= 60 And score < 70 Then grade = "D" End IF MessageBox.Show("The grade is " & grade) Else MessageBox.Show("Please enter a numeric score.") End If
- Grade determination: connected
If
sDim valueIn As String = txtScore.Text Dim score As Double Dim grade as String IfIsNumeric(valueIn) Then score = Val(valueIn) If score >= 90 Then grade = "A" ElseIf score >= 80 Then grade = "B" ElseIf score >= 70 Then grade = "C" ElseIf score >= 60 Then grade = "D" Else grade = "F" End IF MessageBox.Show("The grade is " & grade) Else MessageBox.Show("Please enter a numeric score.") End If
- Sequence of validity tests (setting a flag)
Dim okToOrder As Boolean = True Dim order As String = "" If rdo8in.Checked Then order = order & "8 inch " ElseIf rdo12in.Checked Then order = order & "12 inch " ElseIf rdo16in.Checked Then order = order & "16 inch " ElseIf rdo20in.Checked Then order = order & "20 inch " Else MessageBox.Show("Size NOT selected") okToOrder = False End If ' Similar code for checking to see if a crust was selected ' It would continue using the "okToOrder" variable or FLAG ' Similar code for checking to see if a type of pizza was selected ' It would continue using the "okToOrder" variable or FLAG If okToOrder Then lstOrder.Items.Add( order ) End If
If Statements and Complex Boolean Expressions
Our experience with conditionals suggested (I hope) that it is possible to construct quite complex conditional expressions. Once we do, the If
statement can be quite simple. However, the complexity can be reversed. A single If
statement with a complex conditional can be replaced by a set of (perhaps interconnected) If
statements with simple conditionals. One example is leap year.
A leap is a year whose number is divisible by 4 but if it is divisible by 100, it must also be divisible by 400 or is it not a leap year. A single conditional that tests for a leap year is:
(year Mod 4 = 0) And ((year Mod 100 <> 0) Or ((year Mod 100 = 0) And (year Mod 400 = 0)))
We could use that expression to construct a simple If
statement to test a year value for being a leap year, e.g.,
If (year Mod 4 = 0) And _ ((year Mod 100 <> 0) Or ((year Mod 100 = 0) And (year Mod 400 = 0))) Then MessageBox.Show(year.ToString & " is a leap year!" Else MessageBox.Show(year.ToString & " is NOT a leap year!" End If
Alternatively, we could construct a more complex If
statement that uses simple Boolean expressions in each If
, e.g.,
Dim isLeapYear As Boolean If year Mod 4 = 0 Then If year Mod 100 <> 0 Then '(we know year is divisible by 4) isLeapYear = True Else If year Mod 400 = 0 Then '(we know year is divisible by 100) isLeapYear = True Else isLeapYear = False End If End If Else isLeapYear = False '(we know year is NOT divisibile by 4) End If If isLeapYear Then MessageBox.Show(year.ToString & " is a leap year!" Else MessageBox.Show(year.ToString & " is NOT a leap year!" End If
In both these cases, we will need to take a substantial amount of care to get things correct. It is just a matter of which of them is easier for you to think about and do the due diligence to make sure your code is correct? Any questions/comments?
Work Time
Use the remaining time to examine/work on the Unit IV learning activity and ask any questions you might have.
Next Time
- questions & work on Unit IV