Download presentation
Presentation is loading. Please wait.
Published bySteven Wade Modified over 6 years ago
1
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
2
CIS 16 Application Development Programming with Visual Basic
Chapter Four The Selection Structure
3
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
4
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
5
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
6
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
7
Selection Structures
8
Coding Single-Alternative and Dual-Alternative Selection Structures
STATEMENT BLOCK
9
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 ?
10
Lets try flowcharting it
11
The Selection Structure (cont’d.)
12
Lets try flowcharting it
13
The Selection Structure (cont’d.)
14
NESTED SELECTION STRUCTURES
15
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.
16
Nested Selection Structures
17
Nested Selection Structures
18
MULTIPLE ALTERNATIVE SELECTION STRUCTURES
19
Multiple-Alternative Selection Structures
Multiple-alternative selection structures or extended selection structures choose from several different alternatives.
20
Multiple-Alternative Selection Structures
21
Multiple-Alternative Selection Structures
22
Multiple-Alternative Selection Structures
23
Select Case Statement Select Case statement is simple and clear to code multiple selection structures.
24
Select Case Statement Specifying a Range of Values in a Case Clause
25
How to make comparisons
26
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
27
Comparison Operators (cont’d.)
28
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
29
Comparing Strings
30
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
31
Comparing Boolean Values
32
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
33
Using Logical Operators
When more than one condition is included in an If...Then...Else statement, the conditions are called a compound condition
34
Using the And Logical Operator
35
Using the Or Logical Operator
36
Using the Not Logical Operator
37
Logical Operators (cont’d.)
38
Logical Operators (cont’d.)
39
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
CHAPTER APPLICATION
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.