420 likes | 632 Views
Chapter 4: The Selection Structure. Programming with Microsoft Visual Basic .NET, Second Edition. The Selection Structure. Use the selection structure to make a decision or comparison and select a particular set of tasks to perform The selection structure is also called the decision structure
E N D
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic .NET, Second Edition
The Selection Structure • Use the selection structure to make a decision or comparison and select a particular set of tasks to perform • The selection structure is also called the decision structure • The condition must result in either a true (yes) or false (no) answer
The Selection Structure (continued) • 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 • Visual Basic .NET provides four forms of the selection structure: If, If/Else, If/ElseIf/Else, and Case
Writing Pseudocode for If and If/Else Selection Structures • An If selection structure contains only one set of instructions, which are processed when the condition is true • An 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
Flowcharting the If and If/Else Selection Structures (continued) F T T F
Selection Control Structure Yes No IF Test condition Then Else IFWorker’s hours in a week exceed 40THEN Overtime hours equal the number of hours exceeding 40ELSEThe worker has no overtime hoursEND IF
Coding the If and If/Else Selection Structures If conditionThen statement block containing one or more statements to be processed when the condition is true [Else statement block containing one or more statements to be processed when the condition is false] End If
Coding the If and If/Else Selection Structures (continued) • The items in square brackets ([ ]) in the syntax are optional • You do not need to include the Else portion • Words in bold are essential components of the statement
Coding the If and If/Else Selection Structures (continued) • Items in italic 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 statements in the false path, are referred to as a statement block
Comparison Operators (continued) • Comparison operators are also referred to as relational operators • All expressions containing a relational operator will result in either a true or false answer only • Comparison operators are evaluated from left to right, and are evaluated after any mathematical operators
Logical Operators (continued) • Truth table for Not operator
Logical Operators (continued) • Truth table for And operator
Logical Operators (continued) • Truth table for AndAlso operator
Logical Operators (continued) • Truth table for Or operator
Logical Operators (continued) • Truth table for OrElse operator
Logical Operators (continued) • Truth table for Xor operator
Logical Operators (continued) Figure 4-19: Order of precedence for arithmetic, comparison, and logical operators
Using the ToString Method to Format Numbers • Use the ToString method to format a number • Syntax: variablename.ToString(formatString) • variablename is the name of a numeric variable
Using the ToString Method to Format Numbers (continued) • formatString is a string that specifies the format • Must be enclosed in double quotation marks • Takes the form Axx: • A is an alphabetic character called the format specifier • xx is a sequence of digits called the precision specifier
Comparing Strings • Example 1: Using the Or operator Dim strLetter As String strLetter = txtLetter.Text If strLetter = “P” Or strLetter = “p” Then lblResult.Text = “Pass” Else lblResult.Text = “Fail” End if
Comparing Strings (continued) • Example 2: Using the And operator Dim strLetter As String strLetter = txtLetter.Text If strLetter <> “P” And strLetter <> “p” Then lblResult.Text = “Fail” Else lblResult.Text = “Pass” End if
Comparing Strings (continued) • Example 3: Correct, but less efficient, solution Dim strLetter As String strLetter = txtLetter.Text If strLetter = “P” Or strLetter = “p” Then lblResult.Text = “Pass” End If If strLetter <> “P” And strLetter <> “p” Then lblResult.Text = “Fail” End if
Comparing Strings (continued) • Example 4: Using the ToUpper method Dim strLetter As String strLetter = txtLetter.Text If strLetter.ToUpper() = “P” Then lblResult.Text = “Pass” Else lblResult.Text = “Fail” End if
Numeric value? • Function: IsNumeric(VariableName) • Returns a Boolean value (True or False) Dim strAge as String Dim intAge as Integer strAge = txtAge.text If IsNumeric (strAge) Then intAge = convert.ToInt32(strAge) …
Text value? • Function: IsNumeric(VariableName) • Returns a Boolean value (True or False) Dim strName as String strName = txtName.text If Not IsNumeric (strName) Then …
The MessageBox.Show Method • Use the MessageBox.Show method to display a message box that contains text, one or more buttons, and an icon
The MessageBox.Show Method (continued) • Syntax: MessageBox.Show(text, caption, buttons, icon[, defaultButton]) • text: text to display in the message box • caption: text to display in the title bar of the message box • buttons: buttons to display in the message box • icon: icon to display in the message box • defaultButton: button automatically selected when the user presses Enter
Buttons • MessageBoxButtons.AbortRetryIgnore • MessageBoxButtons.OK • MessageBoxButtons.OKCancel • MessageBoxButtons.RetryCancel • MessageBoxButtons.YesNo • MessageBoxButtons.YesNoCancel MessageBox.Show("The value entered is incorrect, do you want to continue?", "Wrong value", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
Icons • MessageBoxIcon.Exclamation • MessageBoxIcon.Information • MessageBoxIcon.Stop MessageBox.Show("The value entered is incorrect, do you want to continue?", "Wrong value", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation)
Values Returned by MessageBox • DialogResult.OK • DialogResult.Cancel • DialogResult.Abort • DialogResult.Retry • DialogResult.Ignore • DialogResult.Yes • DialogResult.No
Values Returned by MessageBox Dim intButton as Integer intButton = MessageBox.Show("The value entered is incorrect, do you want to continue?", "Wrong value", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation) If intButton = DialogResult.Yes then … End if
Homework #1 Calculator • Use Message boxes to inform user of eventual problems. For example: • Division by 0 forbidden!
Summary • To evaluate an expression containing arithmetic, comparison, and logical operators, evaluate arithmetic operators first, then comparison operators, and then logical operators • To code a selection structure, use the If...Then...Else statement • To create a compound condition, use the logical operators
Summary (continued) • Use the GroupBox tool to add a group box control to the form; drag controls from the form or the Toolbox window into the group box control • To calculate a periodic payment on either a loan or an investment, use the Financial.Pmt method • To display a message box that contains text, one or more buttons, and an icon, use the MessageBox.Show method
Summary (continued) • To allow a text box to accept only certain keys, code the text box’s KeyPress event • To align the text in a control, set the control’s TextAlign property • To catch an exception, and then have the computer take the appropriate action, use a Try/Catch block