Objectives After studying this chapter, you should be able to: Include the selection structure in pseudocode and in a flowchart Explain the difference between single-alternative and dual-alternative selection structures Code a selection structure using the If…Then…Else statement Include comparison and logical operators in a selection structure’s condition Prevent the division by zero run time error Swap two values Create a block-level variable
CIS 16 Application Development Programming with Visual Basic Chapter Four The Selection Structure
Selection structures All of the procedures in an application are written using 1 of 3 structures Sequence Instructions are processed sequentially Selection Instructions are dependent on a decision being made Repetition Instructions repeat until stopped
The Selection Structure The Selection Structure - also called the decision structure – indicates that a decision needs to me made before the program continues The decision is based on a condition that must be evaluated to determine the next instruction to process The condition must evaluate to either True or False only
The Selection Structure Three types of selection structures A single-alternative selection structure has a specific set of instructions to follow only when its condition evaluates to True A dual-alternative selection structure has one set of instructions to follow when the condition evaluates to True, but a different set of instructions to follow when the condition evaluates to False A multiple-alternative selection structure has many sets of instructions to follow based on conditions evaluating to True or False
Flowcharting a Selection Structure Decision symbol Used to represent the condition (decision) in both the selection and repetition structures Other symbols: Oval: Start/stop symbol Rectangle: Process symbol Parallelogram: Input/output symbol
Selection Structures
Coding Single-Alternative and Dual-Alternative Selection Structures STATEMENT BLOCK
Selection Structure (cont.) To determine whether a procedure requires a selection structure, and which one, study the problem specification Selection structure is: a) Sequence b) Dual c)not needed ?
Lets try flowcharting it
The Selection Structure (cont’d.)
Lets try flowcharting it
The Selection Structure (cont’d.)
NESTED SELECTION STRUCTURES
Nested Selection Structures When a decision needs to be made within a decision The inner selection structure is referred to as a nested selection structure. It is contained (nested) entirely within the outer selection structure.
Nested Selection Structures
Nested Selection Structures
MULTIPLE ALTERNATIVE SELECTION STRUCTURES
Multiple-Alternative Selection Structures Multiple-alternative selection structures or extended selection structures choose from several different alternatives.
Multiple-Alternative Selection Structures
Multiple-Alternative Selection Structures
Multiple-Alternative Selection Structures
Select Case Statement Select Case statement is simple and clear to code multiple selection structures.
Select Case Statement Specifying a Range of Values in a Case Clause
How to make comparisons
Comparison Operators Each comparison operator (also referred to as relational operators) can be used to compare two values, and the comparison always results in a Boolean value: either True or False. Rules for comparison operators: They do not have an order of precedence They are evaluated from left to right They are evaluated after any arithmetic operators in the expression
Comparison Operators (cont’d.)
Comparing Strings Visual Basic is case-sensitive, which means that the uppercase letters of the alphabet are not equal to their lowercase counterparts These three string comparisons will evaluate to False: "A" = "a", "Yes" = "yes", and "Yes" = "YES". Use the ToUpper method to temporarily convert a string to uppercase Use the ToLower method to temporarily convert a string to lowercase A text box’s CharacterCasing property indicates whether the text inside the control should remain as typed, or be converted to either uppercase or lowercase
Comparing Strings
Comparing Boolean Values Check boxes provide one or more independent and nonexclusive items from which the user can choose An interface can contain any number of check boxes, and any number of them can be selected at the same time Each check box should be labeled to make its purpose obvious
Comparing Boolean Values
Logical Operators Logical operators allow you to combine two or more sub-conditions into one compound condition The compound condition will always evaluate to a Boolean value: either True or False
Using Logical Operators When more than one condition is included in an If...Then...Else statement, the conditions are called a compound condition
Using the And Logical Operator
Using the Or Logical Operator
Using the Not Logical Operator
Logical Operators (cont’d.)
Logical Operators (cont’d.)
Microsoft Visual Basic 2012: Reloaded, Fifth Edition CHAPTER APPLICATION