lecture set 5 control structures part a - decisions structures
TRANSCRIPT
![Page 1: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/1.jpg)
Lecture Set 5
Control Structures Part A - Decisions
Structures
![Page 2: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/2.jpg)
Slide 2
Objectives
You have seen almost all of this before …
Use the Boolean data type in decision-making statements
Use If statements and Select Case statements to make decisions
Use logical operators to create complex conditional expressions
Learn how to write and evaluate conditional expressions (sometimes called Boolean expressions because they have True/False values)
![Page 3: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/3.jpg)
Slide 3
Introduction to Decision-making - 1
Programming “simply involves the us of three basic control structures -- The sequence structure The decision structure The repetition structure
Recursion aside, these are the only three constructs you need to build algorithms This is it -- End of story!
![Page 4: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/4.jpg)
Slide 4
Introduction to Decision-making - 2
This chapter discusses the decision-making structures provided by Visual Basic Statements execute conditionally based
on the outcome of a decision Three kinds of decision-making or
alternative path execution structures Single alternative decision structures (if-then)
Double alternative structures (if-then-else) Multiple alternative structures (including
the select) Much like Java and C++ - not much new
under the sun
![Page 5: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/5.jpg)
Slide 5
Introduction to Boolean Data
Boolean data operates similarly to an on/off switch True signifies on False signifies off
Many properties are of the Boolean data type (can store only Boolean values) True or False Visible and Enabled for example
![Page 6: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/6.jpg)
Slide 6
Declaring a Boolean Variable
Declare a Boolean variable Uninitialized Boolean variables have a
value of FalseDim Valid As Boolean
Declare and initialize a Boolean variableDim BrowseMode As Boolean = True
Declare multiple Boolean variablesDim Test1, Test2 As Boolean
![Page 7: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/7.jpg)
Slide 7
Boolean Assignment Statements
The keywords True and False are used in Boolean assignment statements
Boolean expressions may also be used Example:
Dim Valid As BooleanValid = TrueValid = False
![Page 8: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/8.jpg)
Slide 8
Decision-making Statements
Applications need the capability to execute one group of statements in certain circumstances and other statements in other circumstances
These statements are called decision-making statements or decision structures or alternative structures
The If statement is used to make decisions
![Page 9: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/9.jpg)
Slide 9
Decision-making (Pseudocode)
If the input date is greater than the current date then
Display a message box indicating the input is invalid.
End of If statement
![Page 10: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/10.jpg)
Slide 10
Flowchart of a Decision-making Statement
![Page 11: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/11.jpg)
Slide 11
If Statements and Comparison Operators
A conditional statement executes one group of statements when a condition is True and another group of statements when a condition is False
Comparison operators are used in conditional statements
Conditional operations always produce a Boolean result
![Page 12: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/12.jpg)
Slide 12
Comparison Operators
Equal to (=) Not equal to (<>) Less than (<) Greater than (>) Less than or equal to (<=) Greater than or equal to (>=)
![Page 13: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/13.jpg)
Slide 13
Logical Operators
Name DescriptionAnd Returns True if both expressions are True. This
operator always evaluates both expressions.
Or Returns True if either expression is True. This operator always evaluates both expressions.
AndAlso Returns True if both expressions are True. This operator only evaluates the second
expression if necessary.OrElse Returns True if either expression is True. This operator only evaluates the second expression if necessary.Not Reverses the value of the expression.
![Page 14: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/14.jpg)
Slide 14
Using Comparison Operators (Example)
Example:
Dim Result As BooleanDim Value1 As Integer = 3, Value2 As Integer = 5Result = Value1 < Value2 ' TrueResult = Value1 + 2 < Value2 – 1 ' False
![Page 15: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/15.jpg)
Slide 15
Using Comparison Operators (Example, continued)
Parentheses can clarify the order of evaluation
The following two statements are equivalent:Result = Value1 + 2 < Value2 – 1Result = (Value1 + 2) < (Value2 – 1)
![Page 16: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/16.jpg)
Slide 16
Evaluating a Condition
![Page 17: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/17.jpg)
Slide 17
Comparison Operators and If Statements
Comparison operators are most commonly used with an If statement A group of statements executes only when
a condition is True This form of If statement is called a one-
way If statement The statements that execute as a result of
a condition are called a statement block
![Page 18: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/18.jpg)
Slide 18
One-Way If Statement (Syntax)
AKA “Single Alternative Decision Structures”
If condition Then statements ‘a block of one or more statementsEnd IfNext sequential statement ‘(NSS)
condition must evaluate to a Boolean value If the condition is True, statements execute If the condition is False, statements do not
execute Execution continues at the statement
following the End If (the NSS) statements make up a statement block
![Page 19: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/19.jpg)
Slide 19
One-Way If Statement (Example)
Dim CurrentValue As Boolean = True
If CurrentValue = True Then ' Statements that execute when ' CurrentValue is TrueEnd If
' statements
![Page 20: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/20.jpg)
Slide 20
One-Way If Statement
![Page 21: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/21.jpg)
Slide 21
Comparison Operations with Dates
Yep! We are a bit ahead of ourselves. But you will get a “taste” of using the date type
Comparison operations can be performed on dates
Dates in the past are less than dates in the future
Example:
Dim StartDate As DateTime = #3/22/2007#Dim EndDate As DateTime = #3/24/2007#If StartDate < EndDate = True Then EndDate = System.DateTime.TodayEnd If
![Page 22: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/22.jpg)
Slide 22
Comparison Operations with Numeric Data Types
Comparison operations can be performed on numeric data
Example:Dim Value1 As Integer = 90If Value1 < 100 Then ' Statements execute when ' Value1 is less than 100End If' statements
![Page 23: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/23.jpg)
Slide 23
Comparison Operations with Strings
Comparison operations can be performed with strings
Strings are compared character-by-character from left to right
String comparisons are performed in two ways Case sensitive (binary comparison)
A < B < E < Z < a < b < e < z Option Compare Binary
Case insensitive (text comparison) (A=a) < (B=b) < (E=e) < (Z=z) Option Compare Text
![Page 24: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/24.jpg)
Slide 24
String Equality Using Text and Binary Comparison (aha – something NEW)
![Page 25: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/25.jpg)
Slide 25
Binary and Text Comparisons (optional)
It’s all a matter of what you are used to … If you turn on Option Compare Binary in a module you get case sensitive comparisons
If you use Option Compare Text you get case insensitive comparisons
The default setting for a new project is Option Compare Binary You can change this setting for an entire
project by going to the Project Properties dialog box
But you probably will not want to do this
![Page 26: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/26.jpg)
Slide 26
Introduction to Two-way If Statements
AKA “Double alternative decision structures” (if-then-else)
One statement block executes when a condition is True and another statement block executes when the condition is False
This form of If statement is commonly referred to as an If . . . Then . . . Else statement
![Page 27: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/27.jpg)
Slide 27
Two-way If Statements (Syntax)
If condition Then statements(True)Else statements(False)End Ifstatements
Statements(True) execute if the condition is True
Statements(False) execute if the condition is False
![Page 28: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/28.jpg)
Slide 28
Two-way If Statements (Example)
If Grade is greater than 75, set Pass to True. Otherwise, set Pass to False
Dim Pass As BooleanDim Grade As Integer = 80If Grade > 75 Then Pass = TrueElse Pass = FalseEnd If' statements
![Page 29: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/29.jpg)
Slide 29
Two-way If Statement
![Page 30: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/30.jpg)
Slide 30
Introduction to Multiway If Statements
AKA – “Multiple Alternative Decision Structures”
Multiway If statements have three or more possible outcomes
![Page 31: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/31.jpg)
Slide 31
Multiway If Statements (Syntax)
If condition1 Then [statements][ElseIf condition2 Then [elseifStatements]][Else] [elseStatements]]End Ifstatements
![Page 32: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/32.jpg)
Slide 32
Multiway If Statements (Dissection)
condition1 is first tested If True, then the first statement block
executes Execution continues as the statement
following the decision-making statement If False, condition2 is tested and then the
remaining conditions are tested If no conditions are True, then the
statements in the Else block execute The Else block is optional
![Page 33: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/33.jpg)
Slide 33
Multiway If Statement
![Page 34: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/34.jpg)
Slide 34
Multiway If Statement (Example)
Dim NumericGrade As Integer = 84Dim LetterGrade As StringIf NumericGrade >= 90 Then LetterGrade = "A"ElseIf NumericGrade >= 80 Then LetterGrade = "B"ElseIf NumericGrade >= 70 Then LetterGrade = "C"ElseIf NumericGrade >= 60 Then LetterGrade = "D"Else LetterGrade = "F"End If' statements
![Page 35: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/35.jpg)
Slide 35
Notes About If Statements
If statements can be written in different ways Chose the If statement that is most readable This decision can be subjective
The Code Editor automatically indents blocks in an If statement
The Code Editor automatically inserts the End If
If statements can be nested One If statement can contain another If
statement
![Page 36: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/36.jpg)
Slide 36
Introduction to Select Case Statements
Select Case statements are similar to multiway If statements
The same expression must be used in each condition
Select Case statements are faster than comparable multiway If statements
Select Case statements tend to be more readable than comparable multiway If statements
![Page 37: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/37.jpg)
Slide 37
Select Case Statement (Syntax)
Select Case testExpression Case expressionList-1 statement-block1 [Case expressionList-2 statement-block2] [Case expressionList-n statement-blockn] [Case Else statements]End Select' statements
![Page 38: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/38.jpg)
Slide 38
Select Case Statement (Dissection)
testExpression is evaluated once Each expressionList is then tested. If True, the
corresponding statement-block executes and the Select Case statement ends
Each expressionList is tested in order When an expressionList is found to be True, the
statement block executes and the Select Case statement ends
If no expessionList is True, then the statements in the Case Else block execute
![Page 39: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/39.jpg)
Slide 39
Select Case Statement (Example)
Dim Quarter As Integer = 1Dim QuarterString As StringSelect Case Quarter Case 1 QuarterString = "First" Case 2 QuarterString = "Second" Case 3 QuarterString = "Third" Case 4 QuarterString = "Fourth" Case Else QuarterString = "Error"End Select' statements
![Page 40: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/40.jpg)
Slide 40
Select Case Statement
![Page 41: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/41.jpg)
Slide 41
Select Case Statement (Variations)
The To clause is used to test a range of values Case 90 to 100
The Is clause is used with comparison operators Case is > 90
A list of values can be created with a comma separated list Case 1, 3, 5
![Page 42: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/42.jpg)
Slide 42
Logical Operators (Introduction)
Logical operators are used in conjunction with comparison and arithmetic operators
Logical operators perform the same task as a conjunction (and) or a disjunction (or) in English
The logical operators are And, Or, Not, Xor
See Table 7-2 for examples
![Page 43: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/43.jpg)
Slide 43
Logical Operators (Precedence)
Logical operators (Not, And, Or, Xor) have an order of precedence Arithmetic operators are evaluated first Comparison operators are evaluated
second Logical operators are evaluated last (of all
the BINARY operators), from left to right in the following order:
Not, And, Or, Xor
![Page 44: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/44.jpg)
Slide 44
Logical Operators (Example)
Evaluation of an expression (be sure you know how to do these):
Dim Result As BooleanResult = (3 + 4) > 6 And (4 + 1) < 6Result = 7 > 6 And 5 < 6Result = True And TrueResult = True
If I remove the parentheses in line 1, does it change the result? Why? How?
If I remove the parentheses in line 1, does it change the result? Why? How?
![Page 45: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/45.jpg)
Slide 45
Logical Operators (Example, continued)
Evaluation of an expression using And and Xor
Dim Result As BooleanResult = (7 > 6) And (5 > 3) Xor (3 > 2)Result = True And True Xor TrueResult = True Xor TrueResult = False
![Page 46: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/46.jpg)
Slide 46
The Not Operator
The Not operator is a unary operator Examples:
Result = Not (True) ' False Result = Not (False) ' True Result = Not (4 > 3) ' False
![Page 47: Lecture Set 5 Control Structures Part A - Decisions Structures](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649eb45503460f94bbc8f0/html5/thumbnails/47.jpg)
Slide 47
Using Logical Operators
Logical operators are typically combined with comparison and arithmetic operators in decision-making statements
Example:If Input >= CurrentMin And _ Input <= CurrentMax Then
Valid = TrueElse Valid = FalseEnd If' statements