180 likes | 295 Views
Topics. Decision Making If…Then…End If structure If…Then…ElseIf…End If structure Select Case structure If…Then…ElseIf…End If vs. Select Case If…Then…End If for Validation. “That’s the thing about people who think they hate computers. What they really hate is lousy programmers.”
E N D
Topics • Decision Making • If…Then…End If structure • If…Then…ElseIf…End If structure • Select Case structure • If…Then…ElseIf…End If vs. Select Case • If…Then…End If for Validation “That’s the thing about people who think they hate computers. What they really hate is lousy programmers.” Larry Niven
Decision Making Overview Review • Decision making in code is one of the three fundamental programming constructs • Sequence • Selection or Choice • Iteration • Allows program to execute designated code when certain conditions are met If sglTotal <= 5000 Then sglTax = sglTotal * .06 Else sglTax = 5000 * .06 + (sglTotal - 5000) * .05 End If What policy is implemented in this code?
Decision Making Overview (cont.) • Three essential patterns in conditional execution • Only execute this code if a condition is true • If a condition is true, execute one section of code, otherwise always execute another If condition is true Then code to execute : End If If condition is true Then code to execute if true : Else code to execute if false : End If
Decision Making Overview (cont.) • Three essential patterns in conditional execution (cont.) 3 Execute a section of code depending on which of many possible conditions is true If condition1 is true Then Code to execute if condition1 is true : ElseIf condition2 is true Then Code to execute if condition 2 is true : ElseIf ... : Else Code to execute if none of the conditions are true : EndIf
Syntax If condition Then statement statement : [Else statement statement :] End If Example stSal = “Dear “ ‘* Determine sex If optMale.Checked = True Then stSal = stSal & “Mr. “ Else stSal = stSal & “Ms. “ End If stSal = stSal & stLname Introduction to the If...Then Structure
Syntax If condition Then statement statement : [Else statement statement :] End If Introduction to the If...Then Structure (cont.) Condition must be a logical test which can be evaluated to be True or False • The statement block can contain as many • statements as are needed • May include subroutine calls • May nest additional structures (including If...Then structures The Else block is optional. If it is not present the Else condition is to donothing The End If statement is required and finishes the structure
Programming Challenge • Assume you have a text box called txtQty. • Write a code segment that will check this block to be sure that the value is a positive, nonzero, number • If it passes the test set the cmdOK button’s enabled property to True. Otherwise set the value to False. • Where might you put this code? • What might you do if you have a text box called txtPrice which must also satisfy the same rules?
Advanced If...Then Techniques • Nesting tests If intQty <= 5 Then code to execute Else If intQty = 8 Then code to execute Else code to execute End If End If Note use of indenting to make structures stand out
Advanced If...Then Techniques (cont.) • Use of the ElseIf statement If x < 2 Then code to execute ElseIf x < 4 Then code to execute ElseIf x < 6 Then code to execute Else code to execute if no other tests passed End If • Evaluation stops when the first true condition is found • ElseIf and Else substructuresare optional • There can be as many ElseIf structures as desired
Advanced If...Then Techniques (cont.) • The conditional test may be replaced by • A literal True or False value (keywords recognized by Visual Basic) (Why should you never see this?) • Expressions which return True/False • optRed.Checked • Format(iChoice, “True/False”) • cboStudents.Enabled • InputForm.Visible • A variable declared as type “Boolean” • If optRed.Checked Then • stColor = “Red” • End If
Select Case Structure • Allows construction of complex conditional execution structures testing a single value Select Case intChoice Case 0 statements... Case 1 statements... Case Else statements... End Select • Actual value of variable is tested for equality against values following Case statements • Only code in first true block executes Almost always literals String, date, or numeric Must match data type ofSelect Case variable
Select Case Structure (cont.) • Using Select Case to translate values • Select Case LCase(txtStyle.Text) • Case “premier” • intStyle = 0 • Case “standard” • intStyle = 1 • Case “economy” • intStyle = 2 • Case Else • MsgBox (“Invalid style name”, 48, “Error”) • txtStyle.SetFocus • End Select
Select Case Structure (cont.) • The Case statement can take several forms • Case “No” String values • Case 3 Numeric values • Case 3 To 7 Range of values • Case Is > 7 Comparison (“Is” keywordCase Is <= 2 is required) • Case 3, 5, 8 List of valuesCase “Red”, “Green” Case 3 To 7, 9
Select Case vs. If…Then…ElseIf…End If • When testing a target variable or property for equality or membership in a value range then both Select Case and an If…Then…ElseIf…End If structure can do the same task • I prefer the Select Case structure • Less cluttered • Meaning of the test logic is clearer (marginally)
Select Case vs. If…Then…ElseIf…End If (cont.) • Use If…Then…ElseIf tests for dissimilar tests If strStatus = “Full Time” Then code to execute ElseIf blnBenefitsEligible = True Then code to execute ElseIf blnIsIntern = True Then code to execute Else code to execute End If
If..Then Tests for Validation • Avoid monstrous compound logical tests to validate control values If txtLName.Text <> “” AND txtFName.Text <> “” _ AND txtAddress.Text <> “” AND txtCity.Text <> “” _ AND txtZip.Text <> “” AND txtEMail.Text <> “” _ AND txtPhone.Text <> “” Then code to execute if all tests are valid End If
If..Then Tests for Validation (cont.) • Perform discrete tests with Boolean results instead Programming Challenge: Use this approach to also construct a custom error message to inform user of problems Dim blnIsValid as Boolean = TRUE If txtLName.Text = “” Then blnIsValid = False End If If txtFName.Text = “” Then blnIsValid = False End If ... If blnIsValid = True Then code to execute if all tests are valid End If
Commenting • Be sure to comment your logic if the meaning is not completely clear from context • Often applies to Else expressions If strEmployee Status = “Full Time” Then code to execute Else ‘* Employee not full time code to execute End If