1 / 87

Microsoft Visual Basic 2005: Reloaded Second Edition

Microsoft Visual Basic 2005: Reloaded Second Edition. Chapter 4 Making Decisions in a Program. Objectives. After studying this chapter, you should be able to: Include the selection structure in pseudocode and in a flowchart Write an If…Then…Else statement

aseymour
Télécharger la présentation

Microsoft Visual Basic 2005: Reloaded Second 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 2005: Reloaded Second Edition Chapter 4 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 • Write an If…Then…Else statement • Write code that uses comparison operators and logical operators • Create a variable having block-scope • Concatenate strings Microsoft Visual Basic 2005: Reloaded, Second Edition

  3. Objectives (continued) • Use the ControlChars.NewLine constant • Change the case of a string • Determine whether a string contains data • Display a message in a message box • Include a nested selection structure in pseudocode, a flowchart, and code Microsoft Visual Basic 2005: Reloaded, Second Edition

  4. Objectives (continued) • Code an If/ElseIf/Else selection structure • Include a Case selection structure in pseudocode, a flowchart, and code • Generate random numbers Microsoft Visual Basic 2005: Reloaded, Second Edition

  5. 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 • Four forms of selection structure: • If • If/Else • If/ElseIf/Else • Case Microsoft Visual Basic 2005: Reloaded, Second Edition

  6. The Selection Structure (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  7. Writing Pseudocode for the If and If/Else Selection Structures • If selection structure: contains one set of instructions to process when the condition is true • If/Else selection structure: • Contains two sets of instructions • One set is processed when the condition is true • The other set is processed when the condition is false • True path: path to follow when condition is true • False path: path to follow when condition is false Microsoft Visual Basic 2005: Reloaded, Second Edition

  8. Writing Pseudocode for the If and If/Else Selection Structures (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  9. Flowcharting the If and If/Else Selection Structures • Selection/repetition symbol: • Diamond shape • Represents both selection and repetition structures • One flowline entering and two flowlines leaving Microsoft Visual Basic 2005: Reloaded, Second Edition

  10. Flowcharting the If and If/Else Selection Structures (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  11. Coding the If and If/Else Selection Structures Microsoft Visual Basic 2005: Reloaded, Second Edition

  12. Coding the If and If/Else Selection Structures (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  13. Coding the If and If/Else Selection Structures (continued) • Else clause: an optional part of the If statement • Statement block: set of statements terminated by an Else or End If Microsoft Visual Basic 2005: Reloaded, Second Edition

  14. Comparison Operators • Comparison operators (or relational operators): • Used as part of the condition in an If statement • 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 2005: Reloaded, Second Edition

  15. Comparison Operators (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  16. Comparison Operators (continued) • Comparison operators: • Have no order of precedence • Are evaluated from left to right in an expression Microsoft Visual Basic 2005: Reloaded, Second Edition

  17. Comparison Operators (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  18. Using Comparison Operators – Swapping Numeric Values • Pseudocode for a procedure that displays highest and lowest of two numbers: Microsoft Visual Basic 2005: Reloaded, Second Edition

  19. Using Comparison Operators – Swapping Numeric Values (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  20. Using Comparison Operators – Swapping Numeric Values (continued) • Block scope: the scope of a variable created within a block • Block-scope variable: can only be used within the statement block in which it was declared • Concatenation operator (&): links two strings • ControlChars.NewLine constant: • Advances the insertion point to the next line Microsoft Visual Basic 2005: Reloaded, Second Edition

  21. Using Comparison Operators – Swapping Numeric Values (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  22. Using Comparison Operators – Swapping Numeric Values (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  23. Using Comparison Operators – Swapping Numeric Values (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  24. Using Comparison Operators – Swapping Numeric Values (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  25. Using Comparison Operators – Swapping Numeric Values (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  26. Using Comparison Operators – Example 2 • Pseudocode for a procedure to allow the user to display the sum or difference of two numbers: Microsoft Visual Basic 2005: Reloaded, Second Edition

  27. Using Comparison Operators – Example 2 (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  28. Using Comparison Operators – Example 2 (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  29. Using Comparison Operators – Example 2 (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  30. Using Comparison Operators – Example 2 (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  31. Using 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 2005: Reloaded, Second Edition

  32. Using the ToUpper and ToLower Methods (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  33. Using the ToUpper and ToLower Methods (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  34. Using the ToUpper and ToLower Methods (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  35. Logical Operators • Logical operators (or Boolean operators): • Used to combine one or more conditions • Compound condition: a combination of conditions using logical operator(s) Microsoft Visual Basic 2005: Reloaded, Second Edition

  36. Logical Operators (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  37. Logical Operators (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  38. Logical Operators (continued) • Truth tables: used to evaluate logical operators in an expression • Short-circuit evaluation: an evaluation in which the second condition may not be evaluated • And and Or operations always evaluate both conditions • AndAlso and OrElse operations do not evaluate second condition if the first condition is false Microsoft Visual Basic 2005: Reloaded, Second Edition

  39. Logical Operators (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  40. Logical Operators (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  41. 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 • Use XOr when exactly one condition must be true to give a true result • Logical operators are evaluated after arithmetic or comparison operators in an expression Microsoft Visual Basic 2005: Reloaded, Second Edition

  42. Using the Truth Tables (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  43. Using the Truth Tables (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  44. Using Logical Operators in an If…Then…Else Statement • Data validation: • Verifying that the input data is within the expected range • Use an If…Then…Else statement to validate input data Microsoft Visual Basic 2005: Reloaded, Second Edition

  45. Using Logical Operators in an If…Then…Else Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  46. Using Logical Operators in an If…Then…Else Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  47. Using Logical Operators in an If…Then…Else Statement (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  48. The String.IsNullOrEmpty Method • String.IsNullOrEmpty method: determine if a control’s Text property or String variable contains data Microsoft Visual Basic 2005: Reloaded, Second Edition

  49. The String.IsNullOrEmpty Method (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition

  50. Modifying the Skate-Away Sales Application Microsoft Visual Basic 2005: Reloaded, Second Edition

More Related