Logistics
- Roll
- Today: CD IIa review; CD IIIa; Unit IV introduction
- questions/comments
See Unit III notes
Selection
At the start of the course, I mentioned the basics of computing—actions, sequence, selection, iteration, and modularization. All but the first are ways of organizing the actions. Programming is figuring out what actions are needed and then organizing them in a way that accomplishes our overall task.
- sequence actions
This entails which 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 place after it.
- iteration
Iteration is simple repetition of 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 simple 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. Do you have any examples you'd like to explore? (when done or otherwise) 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. Ask: What would we want to do? What (if any) data is needed to accomplish that? Do we need to initialize the data or do anything else before the If
statement? Where/When do we report (inside the If
, after the If
, ...?
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 Double.TryParse(valueIn, score) Then If score >= 12 / 14 Then grade = "A" End If If score < 3 / 14 Then grade = "F" End IF If score >= 9 / 14 And score < 12 / 14 Then grade = "B" End If If score >= 6 / 14 And score < 9 / 14 Then grade = "C" End If If score >= 3 / 14 And score < 6 / 14 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 If Double.TryParse(valueIn, score) Then If score >= 12 / 14 Then grade = "A" ElseIf score >= 9 / 14 Then grade = "B" ElseIf score >= 6 / 14 Then grade = "C" ElseIf score >= 3 / 14 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
Next Time
- questions & work on Unit IV
- comments/questions