1 / 49

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

rod
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. Operator Precedence • Operator Precedence • ^ 1 • - negation 2 • * / 3 • \ 4 • Mod 5 • + - 6 • & 7 • < =, >=,<, >, <> 8 • Not 9 • And, AndAlso 10 • Or, OrElse 11 • Xor 12

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

  12. Using a Comparison Operator Block-level Variable

  13. If the answer is False

  14. Logical Operators

  15. Truth Table for Not Operator

  16. Truth Table for And Operator

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

  18. Truth Table for AndAlso Operator

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

  20. Truth Table for Or Operator

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

  22. Truth Table for OrElse Operator

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

  24. Truth Table for Xor Operator

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

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

  27. Another Example

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

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

  30. The ToUpper and ToLower Functions

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

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

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

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

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

  36. MessageBoxButtons

  37. MessageBoxIcons

  38. MessageBoxDefaultButton

  39. MessageBoxOptions

  40. DialogResult

  41. Using DialogResult • MessageBox.Show returns one of seven values depending on which button has been clicked.

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

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

  44. Canceling Unwanted Characters • Keypress is an event associated with the textbox and other controls which we can use as an event to trigger a procedure • When a key is pressed the Sender parameter receives as its value information about which control the key was typed in. • The e parameter receives the code about specifically which key was pressed. • We can check the value of either of these and use them as needed. Here are some examples:

  45. The statement of the If condition in Example may be better understood if we restate it: ‘Here is an alternate statement If (e.KeyChar >= "0" And e.KeyChar <= "9") _ Or e.KeyChar = ChrW(8) Or e.KeyChar = "." Then 'let system handle the key as usual Else 'Cancel the key e.Handled = True End If ‘ Here is the original statement for comparison If (e.KeyChar < "0" Or e.KeyChar > "9") And _ e.KeyChar <> ControlChars.Back And e.KeyChar <> "." Then e.Handled = True End If

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

  47. Aligning Text in a Label Using the TextAlign Property

More Related