1 / 45

Chapter 4 – Decisions

Chapter 4 – Decisions. 4.1 Relational and Logical Operators (see other set of slides) 4.2 If Blocks 4.3 Select Case Blocks 4.4 Input via User Selection (see other set of slides). 4.2 If Blocks. If Block Nested If Blocks ElseIf Clauses Input Validation with If Blocks. If Block.

questa
Download Presentation

Chapter 4 – Decisions

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. Chapter 4 – Decisions 4.1 Relational and Logical Operators (see other set of slides) 4.2 If Blocks 4.3 Select Case Blocks 4.4 Input via User Selection (see other set of slides)

  2. 4.2 If Blocks • If Block • Nested If Blocks • ElseIf Clauses • Input Validation with If Blocks

  3. If Block The following program will take a course of action based on whether a condition is true. Ifcondition Then action 1 Else action 2 EndIf Will be executed if condition is true Will be executed if condition is false

  4. Another example If Block Ifcondition Then action 1 EndIf Statement 2 Statement 3 Regardless of whether the condition in the If statement is true or false, these statements will be executed

  5. Pseudocode and Flowchart for an If Block

  6. Demo 4.2.1 txtFirstNum txtSecondNum txtResult

  7. Code PrivateSub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2, largerNum AsDouble num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If num1 > num2 Then largerNum = num1 Else largerNum = num2 EndIf txtResult.Text = "Larger number: " & largerNum End Sub • What is output if num1 = 15 and num2 = 85? • What if num1 = 85 and num2 = 15?

  8. Output

  9. Example 4.2.2 txtAnswer txtSolution

  10. Code If statement condition must be Boolean expression…Here, it is a logical combination of two separate relational expressions PrivateSub btnEvaluate_Click(...) _ Handles btnEvaluate.Click Dim answer AsDouble answer = CDbl(txtAnswer.Text) If (answer >= 0.5) And (answer <= 1) Then txtSolution.Text = "Good, " Else txtSolution.Text = "No, " EndIf txtSolution.Text &= "it holds about 3/4 gals." EndSub

  11. Output

  12. Example 4.2.3 mtbAnswer txtQuote

  13. Code MessageBox is a popup window that displays information…see page 90 (Ch3) for details

  14. Output First argument to MessageBox.Show is the text of the message, Second argument is the title of the MessageBox window. This is the Message Box

  15. Output (continued)

  16. Nested If Blocks When one If block is contained inside another If block, the structure is referred to as nested If blocks.

  17. Nested If Ifcondition 1 Then Ifcondition 2 Then action 2 EndIf Else action 1 EndIf Independent statement

  18. Example 4.2.5

  19. Code

  20. Output

  21. ElseIf Clause Ifcondition 1 Then action 1 ElseIfcondition 2 Then action 2 Else action 3 EndIf Rewrite 4.2.5 example from previous slide to ElseIf statement

  22. Example 4.2.6 (old 4.2.1) txtFirstNum txtSecondNum txtResult

  23. Code Private Sub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2 As Double num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If (num1 > num2) Then txtResult.Text = "Larger number is " & num1 ElseIf (num2 > num1) Then txtResult.Text = "Larger number is " & num2 Else txtResult.Text = "The two are equal." End If EndSub

  24. Example 4.2.7

  25. Example 7: Partial Code If ytdEarnings are 10,000 and curEarnings 1,000 – what will be in ficaTaxes Relationship between Fica & Federal taxes??

  26. Example 7: Output

  27. Input Validation The statement If (IsNumeric(txtBox.Text) = True) Then is commonly used to validate that input is numeric. It can be condensed to IfIsNumeric(txtBox.Text) Then

  28. Simplified Nested If Statement Care should be taken to make If blocks easy to understand. Ifcond1 Then Ifcond1 Andcond2 Then action Ifcond2 Then End If action End If End If Clear Confusing

  29. Comment Some programs call for selecting among many possibilities. Although such tasks can be accomplished with complicated nested If blocks, the Select Case block (discussed in Section 4.3) is often a better alternative.

  30. 4.3 Select Case Block • A decision-making structure that simplifies choosing among several actions. • Avoids complex nested If constructs. • If blocks make decisions based on the truth value of a condition. Select Case choices are determined by the value of an expression called a selector.

  31. Select Case Terminology Each of the possible actions is preceded by a clause of the form CasevalueList where valueList itemizes the values of the selector for which the action should be taken.

  32. Select Case Syntax The general form of the Select Case block is Select Caseselector CasevalueList1 action1 CasevalueList2 action2 Case Else action of last resort End Select

  33. Rules for Select Case Each value list contains one or more of the following types of items separated by commas. • a literal • a variable • an expression • an inequality sign preceded by Is and followed by a literal, variable, or expression • a range given in the form a To b, where a and b are literals, variables, or expressions.

  34. Example 4.3.1 txtPosition txtOutcome

  35. Code (individual cases) PrivateSub btnEvaluate_Click(...) _ Handles btnEvaluate.Click Dim position AsInteger = CInt(txtPosition.Text) SelectCase position Case 1 txtOutcome.Text = "Win" Case 2 txtOutcome.Text = "Place" Case 3 txtOutcome.Text = "Show" Case 4, 5 txtOutcome.Text = "Almost in the money." CaseElse txtBox.Text = "Out of the money." EndSelect EndSub Selector ValueLists

  36. Output

  37. Example 4.3.2 txtPosition txtOutcome

  38. Code (range of cases) Selector ValueLists

  39. Output

  40. Example 4.3.3 mtbMonth mtbYear

  41. Code (multiple cases) If stmt nested inside Select-Case stmt

  42. Output

  43. Comments • In a Case clause of the form Case b To c, the value of b should be less than or equalto the value of c. • The word Is should precede an inequality sign in a value list. • If the word Is is accidentally omitted where required, the editor will automatically insert it when checking the line.

  44. Data Type Comment • The items in the value list must evaluate to the same data type as the selector. • For instance, if the selector evaluated to a string value, as in Dim firstName AsString= txtBox.Text SelectCase firstName then the clause Case firstName.Length would be meaningless.

  45. Block-Level Scope • A variable declared inside an If or Select Case block has block-level scope. • The variable cannot be referred to outside of the block.

More Related