1 / 18

Topics

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.”

tiara
Download Presentation

Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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?

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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?

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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)

  15. 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

  16. 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

  17. 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

  18. 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

More Related