1 / 44

Tutorial 4 The Selection Structure

Tutorial 4 The Selection Structure. The If…Then…Else Statement Lesson A Objectives. After completing this lesson, you will be able to: Write pseudocode for the selection structure Create a flowchart to help you plan an application’s code Write an If…Then…Else statement

shima
Download Presentation

Tutorial 4 The Selection Structure

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. Tutorial 4 The Selection Structure

  2. The If…Then…Else Statement Lesson A Objectives After completing this lesson, you will be able to: • Write pseudocode for the selection structure • Create a flowchart to help you plan an application’s code • Write an If…Then…Else statement • Write code that uses comparison operators and logical operators • Use the UCase and LCase functions

  3. The Selection Structure • Use to make a decision or comparison and then, based on the result of that decision or comparison, to select one of two paths • You use the selection structure, also called the decision structure, when you want a program to make a decision or comparison and then, based on the result of the decision or comparison, select a particular set of tasks to perform • The condition must result in either a true (yes) or false (no) answer • If the condition is true, the program performs one set of tasks. If the condition is false, there may or may not be a different set of tasks to perform.

  4. An If structure contains only one set of instructions which are processed when the condition is true If condition is true Then perform these tasks End If Perform these tasks whether condition is true or false An If/Else structure contains two sets of instructions: one set is processed when the condition is true and the other set is processed when the condition is false If condition is true then perform these tasks Else perform these tasks End If Perform these tasks whether condition is true or false Writing Pseudocode for If and If/Else Selection Structures

  5. Flowchart Symbols

  6. Selection Structure Flowcharts F T T F

  7. Coding the If and If/Else Selection Structures • The items in square brackets ([ ]) in the syntax are optional • You do not need to always include the Else portion • Words in bold, however, are essential components of the statement • Items in italics indicate where the programmer must supply information pertaining to the current application • The set of statements contained in the true path, as well as the set of statements contained in the false path, are referred to as a statement block

  8. Coding the If and If/Else Selection Structures IfconditionThen instructions when the condition is true End If IfconditionThen [instructions when the condition is true] [Else [instructions when the condition is false]] End If

  9. Comparison Operators Comparison operators are evaluated from left to right, and are evaluated after any mathematical operators

  10. 10 + 3 < 5 * 2 5 * 2 is evaluated first, giving 10 10 + 3 is evaluated second, giving 13 13 < 10 is evaluated last, giving false 7 > 3 * 4 / 2 3 * 4 is evaluated first, giving 12 12 / 2 is evaluated second, giving 6 7 > 6 is evaluated last, giving true Expressions Containing Relational Operators All expressions containing a relational operator will result in either a true or false answer only

  11. Using a Comparison Operator Block-level Variable

  12. If the answer is False

  13. Logical Operators

  14. Truth Table for Not Operator

  15. Truth Table for And Operator

  16. 3 > 2 And 6 > 5 3 > 2 is evaluated first, giving true 6 > 5 is evaluated second, giving true true And true is evaluated last, giving true 10 < 25 And 6 > 5 + 1 5 + 1 is evaluated first, giving 6 10 < 25 is evaluated second, giving true 6 > 6 is evaluated third, giving false true And false is evaluated last, giving false Expressions Containing the And Logical Operator

  17. Truth Table for AndAlso Operator

  18. 3 < 2 AndAlso 6 > 5 3 < 2 is evaluated first, giving false 6 > 5 is not evaluated false 10 < 25 AndAlso 6 > 5 + 1 10 < 25 is evaluated first, giving true 5 + 1 is evaluated second, giving 6 6 > 6 is evaluated third, giving false true And false is evaluated last, giving false Expressions Containing the AndAlso Logical Operator strSales = 12000 strRate = “A” strRate =“A” AndAlso sngSales > 10000 • strRate =“A” is evaluated first, giving true • sngSales > 10000 is evaluated second giving true • true AndAlso true gives true

  19. Truth Table for Or Operator

  20. 3 < 2 Or 6 > 5 3 < 2 is evaluated first, giving false 6 > 5 is evaluated second giving true false or true gives true 10 < 25 Or 6 > 5 + 1 10 < 25 is evaluated first, giving true 5 + 1 is evaluated second, giving 6 6 > 6 is evaluated third, giving false true or false is evaluated last, giving true Expressions Containing the Or Logical Operator

  21. Truth Table for OrElse Operator

  22. 3 < 2 OrElse 6 > 5 3 < 2 is evaluated first, giving false 6 > 5 is evaluated second giving true false or true gives true 10 < 25 OrElse 6 > 5 + 1 10 < 25 is evaluated first, giving true 6 > 5 + 1 is not evaluated true Expressions Containing the OrElse Logical Operator strRate1 = “A” : strRate2 = “B”: strTest = “B” strRate1 = strTest OrElse strRate2 = strTest • strRate1 = strTest is evaluated first, giving false • strRate2 = strTest is evaluated second giving true • false Or true gives true

  23. Truth Table for Xor Operator

  24. Arithmetic Operators Exponentiation Negation Multiplication and division Integer division Modulus Addition and subtraction Concatenation Operators Comparison Operators Logical Operators Not And and AndAlse Or and OrElse Xor Operator Order of Precedence

  25. Using Logical Operators in an If…Then…Else Statement

  26. Another Example

  27. The UCase and LCase Functions • In most programming languages, string comparisons are case sensitive--in other words, the letter “A” is not the same as the letter “a” • UCase is a function that converts all characters to Upper Case • strData = UCase(“New York”) • strData now contains “NEW YORK” • LCase is a function the converts all characters to Lower Case • strData = LCase(“New York”) • strData now contains “new york”

  28. The UCase and LCase Functions • Determine if the variable strState is California If strState =“CA” OrElse strState = “ca” OrElse strState = “cA” OrElse strState = “Ca” Then Or you can use the UCase function If UCase(strState) = “CA” Then Or you can use the ToUpper function If strState.ToUpper = “CA” Then

  29. The ToUpper and ToLower Functions

  30. The Monthly Payment Calculator ApplicationLesson B Objectives After completing this lesson, you will be able to: • Group objects using a GroupBox control • Calculate a periodic payment using the Pmt function • Create a message box using the MessageBox.Show method • Determine the value returned by a message box

  31. Adding a GroupBoxControl to the Form • You use the GroupBox tool in the Toolbox window to add a group box control to the interface • A group box control serves as a container for other controls • You can use a group box control to visually separate related controls from other controls on the form • Place the GroupBox control on the form and drag other controls into it

  32. Coding the CalcPayButton Click Event Procedure • The CalcPayButton’s Click event procedure is responsible for calculating the monthly payment amount, and then displaying the result in the PaymentLabel control • The pseudocode for the CalcPayButton’s Click event procedure is shown in Figure 4-27 in the textbook

  33. The Pmt Function • You can use the Visual Basic .NET Pmt function to calculate a periodic payment on either a loan or an investment

  34. The MessageBox.Show Method You can use the Message.Box.Show method to display a message box that contains text, one or more buttons, and an icon

  35. MessageBoxButtons

  36. MessageBoxIcons

  37. MessageBoxDefaultButton

  38. MessageBoxOptions

  39. DialogResult

  40. Completing the Monthly Payment Calculator ApplicationLesson C Objectives After completing this lesson, you will be able to: • Specify the keys that a text box will accept • Display the ControlChars constants • Align the text in a label control

  41. Coding the KeyPress Event • Template Private Sub PrincipalTextBox_KeyPress( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyPressEventArgs) _ Handles PrincipalTextBox.KeyPress • Setting e.Handled = True will cancel the key

  42. ControlChars Class strText = “Hello” & ControlChars.NewLine & “World”

  43. Aligning Text in a Label Using the TextAlign Property

More Related