Logistics
- Roll
- Questions? Comments? Wonderings?
Unit III Questions
If you have any questions about the Unit III Practice items or the sample Comp.Demo. I distributed, now is the time to ask.
- Questions about the Unit IV practice/learning activity items?
- Address questions
Unit IV Introduction (see day 12 notes)
We will begin consideration of the programming fundamental of selection. But we will halt at 4:10 pm to allow time for the competency demo for Unit III.
Next Time
Logistics
- Roll
- Today: CD IIa review; CD IIIa; 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 basics 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
s
Dim 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
s
Dim 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
Next Time
- questions & work on Unit IV
- Project considerations
Logistics
- Roll
- Questions? Comments? Wonderings?
Project Meeting
Remember, you need to be deciding on a project. Review the project assignment description noting the project checklist information. Think about "apps" that might be useful in your field. If you can't think of anything, talk with your friends or come see me. We have the next three weeks or so to do meetings about your project, and, I will be gone March 8 - 12.
Questions? Comments? Wonderings?
Unit IV Questions
- Questions about the Unit IV practice/learning activity items?
- Address questions, allow time for work
Next Time
- Unit IV (if) Comp.Demo — prep. & administration
Logistics
- Roll
- Questions? Comments? Wonderings?
Remember that the project proposal meeting needs to occur soon. Listed due date is this Friday but I'll change that to next Tuesday. Questions about the project?
Unit IV Competency Demo
- Questions about the Unit IV practice/learning activity items?
- Questions about the Unit IV sample Comp.Demo?
- Unit IV sample Comp.Demo?
If students are ready, complete the Unit IV competency demo.
Next Time