1 / 65

Microsoft Visual Basic 2008: Reloaded Fourth Edition

Learn how to include selection structures, code selection structures using If...Then...Else statement, concatenate strings, generate random numbers, and more in Visual Basic 2010.

jareds
Download Presentation

Microsoft Visual Basic 2008: Reloaded Fourth Edition

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. Microsoft Visual Basic 2008: ReloadedFourth Edition Chapter Four Making Decisions in a Program

  2. Objectives After studying this chapter, you should be able to: • Include the selection structure in pseudocode and in a flowchart • Explain the difference between single-alternative and dual-alternative selection structures • Code a selection structure using the If…Then…Else statement • Include comparison operators and logical operators in a selection structure’s condition • Create a block-level variable Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  3. Objectives (cont'd.) • Concatenate strings • Use the ControlChars.NewLine constant • Change the case of a string • Include a check box in an interface • Generate random numbers Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  4. The Selection Structure • Selection structure (or decision structure): • Used to select a path to take based on the outcome of a decision or comparison • Condition: • The decision to be made • Results in a Boolean (True or False) answer • Single-alternative selection structure: performs a set of tasks only when the condition is true • True path: the tasks to perform when the condition is true Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  5. The Selection Structure (cont’d.) • Dual-alternative selection structure:contains one set of tasks to perform when the condition is true and a different set of tasks to perform when the condition is false • False path: the tasks to perform when the condition is false • Pseudocode uses if…end if to denote a selection structure and else to denote the false path • Indent instructions within the selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  6. The Selection Structure (cont'd.) Figure 4-1: Selection structures you might use today Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  7. The Selection Structure (cont'd.) Figure 4-1: Selection structures you might use today (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  8. The Selection Structure (cont'd.) Figure 4-2: Problem specification for Mountain Biking Figure 4-3: Interface for the Mountain Biking application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  9. The Selection Structure (cont'd.) Figure 4-4: Pseudocode containing only the sequence structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  10. The Selection Structure (cont'd.) Figure 4-5: Modified problem specification and pseudocode containing a single-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  11. Figure 4-6: Single-alternative selection structure shown in a flowchart Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  12. Figure 4-7: Modified problem specification and pseudocode containing a dual-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  13. Figure 4-8: Dual-alternative selection structure shown in a flowchart Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  14. Coding Single-Alternative and Dual-Alternative Selection Structures • If…Then…Else statement: used to code single-alternative and dual-alternative selection structures • Else clause: an optional part of the If statement • Only used for the dual-alternative selection structure • Condition must be a Boolean expression that evaluates to either True or False • Can contain variables, literal constants, named constants, properties, methods, arithmetic operators, comparison operators, and logical operators • Statement block: set of statements in the true path or the false path Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  15. Figure 4-9: How to use the If…Then…Else statement Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  16. Figure 4-9: How to use the If…Then…Else statement (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  17. Comparison Operators • Comparison operators (or relational operators): • Used as part of the condition in an If…Then…Else statement to compare two values • Most commonly used comparison operators: • Equal to: = • Greater than: > • Greater than or equal to: >= • Less than: < • Less than or equal to: <= • Not equal to: <> Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  18. Figure 4-10: How to use comparison operators in a condition Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  19. Comparison Operators (cont’d.) Figure 4-10: How to use comparison operators in a condition (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  20. Comparison Operators (cont'd.) • Comparison operators: • Have no order of precedence • Are evaluated from left to right in an expression • Are evaluated after any arithmetic operators in the expression • All expressions containing comparison operators evaluate to True or False only Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  21. Comparison Operators (cont'd.) Figure 4-11: Evaluation steps for an expression containing arithmetic and comparison operators Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  22. Comparing Numeric Values • Auction House application displays highest and lowest of two bids entered by the user Figure 4-12: Sample run of the Auction House application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  23. Comparing Numeric Values (cont'd.) Figure 4-13: Pseudocode containing a single-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  24. Figure 4-14: Flowchart containing a single-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  25. Comparing Numeric Values (cont'd.) Figure 4-15: Code entered in the Display button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  26. Comparing Numeric Values (cont'd.) Figure 4-15: Code entered in the Display button’s Click event procedure (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  27. Comparing Numeric Values (cont'd.) • Block-level variables: declared within a statement block and remain in memory until the procedure ends • Block scope: A block-scope variable can only be used within the statement block in which it was declared • Concatenation operator (&): connects or links two strings together • ControlChars.NewLine constant: • Advances the insertion point to the next line Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  28. Comparing Numeric Values (cont'd.) Figure 4-16: Illustration of the swapping concept Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  29. Comparing Numeric Values (cont'd.) Figure 4-17: How to concatenate strings Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  30. Comparing Strings • Addition and Subtraction Calculator application: displays the sum or difference of two numbers Figure 4-18: Sample run of the Addition and Subtraction Calculator application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  31. Comparing Strings (cont'd.) Figure 4-19: Pseudocode containing a dual-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  32. Figure 4-20: Flowchart containing a dual-alternative selection structure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  33. Comparing Strings (cont'd.) • MaxLength property: text box property that specifies the maximum number of characters that can be entered • CharacterCasing property: text box property that indicates if text should remain as typed or be converted to uppercase or lowercase Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  34. Figure 4-21: Calculate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  35. The ToUpper and ToLower Methods • String comparisons in Visual Basic are case-sensitive • ToUpper method: converts a string to uppercase • ToLower method: converts a string to lowercase • ToUpper and ToLower can be used to permanently or temporarily convert a variable’s contents Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  36. The ToUpper and ToLower Methods (cont’d.) Figure 4-22: How to use the ToUpper and ToLower methods Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  37. The ToUpper and ToLower Methods (cont’d.) Figure 4-22: How to use the ToUpper and ToLower methods (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  38. The ToUpper and ToLower Methods (cont’d.) Figure 4-22: How to use the ToUpper and ToLower methods (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  39. Figure 4-23: Examples of using the ToUpper method in the calcButton Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  40. Figure 4-23: Examples of using the ToUpper method in the calcButton Click event procedure (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  41. Comparing Boolean Values • Check boxes: used to offer the user one or more independent and nonexclusive items from which to choose Figure 4-24: A different interface for the Addition and Subtraction Calculator application Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  42. Comparing Boolean Values (cont’d.) Figure 4-25: Click event procedures for the subtractionCheckBox and calcButton Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  43. Comparing Boolean Values (cont’d.) Figure 4-25: Click event procedures for the subtractionCheckBox and calcButton (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  44. Logical Operators • Logical operators (or Boolean operators): • Used to combine two or more conditions into one compound condition • Compound condition: a combination of conditions using logical operator(s) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  45. Logical Operators (cont'd.) Figure 4-26: How to use logical operators in a condition Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  46. Logical Operators (cont'd.) Figure 4-26: How to use logical operators in a condition (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  47. Figure 4-26: How to use logical operators in a condition (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  48. Logical Operators (cont'd.) • Truth tables: used to evaluate logical operators in an expression • Short-circuit evaluation: an evaluation in which the second condition may not be evaluated • AndAlso evaluates to True only when both sub-conditions are True • OrElse evaluates to False only when both sub-conditions are False • AndAlso and OrElse operations do not evaluate the second condition if the first condition is false Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  49. Logical Operators (cont'd.) Figure 4-27: Truth tables for the AndAlso and OrElse logical operators Microsoft Visual Basic 2010: Reloaded, Fourth Edition

  50. Using the Truth Tables • Use And or AndAlso when both conditions must be true to give a true result • Use Or or OrElse when one or both conditions must be true to give a true result • Remember: logical operators are evaluated after arithmetic or comparison operators in an expression Microsoft Visual Basic 2010: Reloaded, Fourth Edition

More Related