1 / 41

Chapter 4: The Selection Structure

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

elata
Download Presentation

Chapter 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. Chapter 4: The Selection Structure Programming with Microsoft Visual Basic .NET, Second Edition

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

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

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

  5. Flowcharting the If and If/Else Selection Structures

  6. Flowcharting the If and If/Else Selection Structures (continued) F T T F

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

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

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

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

  11. Comparison Operators

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

  13. Comparison Operators (continued)

  14. Logical Operators

  15. Logical Operators (continued) • Truth table for Not operator

  16. Logical Operators (continued) • Truth table for And operator

  17. Logical Operators (continued) • Truth table for AndAlso operator

  18. Logical Operators (continued) • Truth table for Or operator

  19. Logical Operators (continued) • Truth table for OrElse operator

  20. Logical Operators (continued) • Truth table for Xor operator

  21. Logical Operators (continued) Figure 4-19: Order of precedence for arithmetic, comparison, and logical operators

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

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

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

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

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

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

  28. 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) …

  29. Text value? • Function: IsNumeric(VariableName) • Returns a Boolean value (True or False) Dim strName as String strName = txtName.text If Not IsNumeric (strName) Then …

  30. The MessageBox.Show Method • Use the MessageBox.Show method to display a message box that contains text, one or more buttons, and an icon

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

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

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

  34. Values Returned by MessageBox • DialogResult.OK • DialogResult.Cancel • DialogResult.Abort • DialogResult.Retry • DialogResult.Ignore • DialogResult.Yes • DialogResult.No

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

  36. Exercise #5

  37. Exercise #5

  38. Homework #1 Calculator • Use Message boxes to inform user of eventual problems. For example: • Division by 0 forbidden!

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

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

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

More Related