500 likes | 658 Views
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
E N D
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
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.
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
Selection Structure Flowcharts F T T F
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
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
Comparison Operators Comparison operators are evaluated from left to right, and are evaluated after any mathematical operators
Operator Precedence • Operator Precedence • ^ 1 • - negation 2 • * / 3 • \ 4 • Mod 5 • + - 6 • & 7 • < =, >=,<, >, <> 8 • Not 9 • And, AndAlso 10 • Or, OrElse 11 • Xor 12
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
Using a Comparison Operator Block-level Variable
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
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
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
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
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
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”
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
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
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
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
The Pmt Function • You can use the Visual Basic .NET Pmt function to calculate a periodic payment on either a loan or an investment
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
Using DialogResult • MessageBox.Show returns one of seven values depending on which button has been clicked.
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
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
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:
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
ControlChars Class strText = “Hello” & ControlChars.NewLine & “World”