T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " (...
-
Upload
gilbert-jefferson -
Category
Documents
-
view
217 -
download
1
Transcript of T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " (...
![Page 1: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/1.jpg)
TODAY’S QUOTE
"The computers do what you tell them to do, not what you want them to do. " (Alexander Atanasov)
![Page 2: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/2.jpg)
CHAPTER 4 – DECISIONS2
![Page 3: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/3.jpg)
4.1 – PART 1 – ASCII
3
![Page 4: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/4.jpg)
ASCII VALUES
Standard characters available http://www.asciitable.com/
4
![Page 5: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/5.jpg)
ASCII VALUES
5
![Page 6: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/6.jpg)
EXTENDED ASCII VALUES
![Page 7: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/7.jpg)
ASCII VALUES
What are some critical characters when reading/writing files?
Code VB 009 TAB vbTab 013 CHR vbCr 010 LF vbLf
vbCr & vbLf vbCrLf
7
![Page 8: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/8.jpg)
ASCII CHARACTER SET
A numeric representation for every key on the keyboard and for other assorted characters.
8
32 (space) 48 0 66 B 122 z 33 ! 49 1 90 Z 123 { 34 “ 57 9 97 a 125 } 35 # 65 A 98 b 126 ~
![Page 9: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/9.jpg)
ASCII CHARACTER SET: CONTINUED
A numeric representation for every key on the keyboard and for other assorted characters.
9
162 ¢ 177 ± 181 µ 190 ¼ 169 © 178 ² 188 ¼ 247 ÷ 176 ° 179 ³ 189 ½ 248 ø
![Page 10: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/10.jpg)
CHR FUNCTION
For n between 0 and 255,
Chr(n)
is the string consisting of the character with
ASCII value n.
EXAMPLES
Chr(65) is "A"Chr(162) is "¢"
10
![Page 11: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/11.jpg)
ASC FUNCTION
For a string str,
Asc(str)
is ASCII value of the first character of str.
EXAMPLES
Asc("A") is 65Asc("¢25") is 162
11
![Page 12: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/12.jpg)
4.1 – PART 2 – LOGICAL OPERATORS
12
![Page 13: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/13.jpg)
LOGICAL OPERATORS
< less than<= less than or equal to> greater than>= greater than or equal to= equal to<> not equal to
ASCII values are used to decide order for strings.
13
![Page 14: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/14.jpg)
CONDITION
Decisions are a result of evaluating a condition
A condition is an expression involving relational and/or logical operators
Result of the condition is Boolean
14
???
![Page 15: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/15.jpg)
EXAMPLE
When a = 3, b = 4 (a + b) < 2 * a TRUE?
15
3 + 4 = 7 2 * 3 = 6
![Page 16: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/16.jpg)
ANOTHER EXAMPLE
a = 4 b = 3 c = "hello" d = "bye"( c.Length – b ) = ( a / 2 )
TRUE?
16
5 – 3 = 2 4 / 2 = 2
![Page 17: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/17.jpg)
RELATIONAL OPERATOR NOTES
Relational operators are binary – they require an operand on both sides of the operator
Value of a relational expression will always be True or False
Expressions are evaluated from left to right with no order of operations
17
![Page 18: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/18.jpg)
LOGICAL OPERATORS
Used with Boolean expressions
Not – makes a False expression True and vice versa
And – will yield a True if and only if both expressions are True
Or – will yield a True if at least one of both expressions are True
18
![Page 19: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/19.jpg)
EXAMPLE 4.3
n = 4, answer = “Y”
Are the following expressions true or false?
1) Not (n < 6)2) (answer = "Y") Or (answer = "y")3) (answer = "Y") And (answer = "y")4) answer = "y")
19
![Page 20: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/20.jpg)
BOOLEAN EXPRESSION
An expression that evaluates to either True or False is said to have Boolean data type.
Example: The statement
txtBox.Text = CStr((2+3)<6)
displays True in the text box.
20
![Page 21: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/21.jpg)
BOOLEAN VARIABLE
A variable declared with a statement of the form
Dim var As Booleanis said to have Boolean data type. It can
assumejust the two values True and False.Example: Dim boolVar As Boolean boolVar = 2 < 6 txtBox.Text = CStr(boolVar)
21
What’s the result?
![Page 22: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/22.jpg)
SYNTAX ERROR
The following is NOT a valid way to test if n falls between 2 and 5:
(2 < n < 5 )
22
![Page 23: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/23.jpg)
CORRECTION TO SYNTAX ERROR
To test if n falls between 2 and 5 use:
(2 < n ) And ( n < 5 )
A complete relational expression must beon either side of the logical operators Andand Or.
23
![Page 24: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/24.jpg)
COMMON ERROR IN BOOLEAN EXPRESSIONS
A common error is to replace the condition
Not ( 2 < 3 )
with the condition
( 2 > 3 )What’s wrong??
24
![Page 25: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/25.jpg)
4.2 IF BLOCKS
If Block ElseIf Clauses
25
![Page 26: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/26.jpg)
IF BLOCK
The program will take a course of actionbased on whether a condition is true.
If condition Then action1Else action2End If
26
Will be executed if condition is true
Will be executed if condition is false
![Page 27: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/27.jpg)
ANOTHER EXAMPLE IF BLOCK
If condition Then
action1
End If
Statement2Statement3
27
Regardless of whether
the condition in the
If statement is true or
alse, these statements
will be executed
![Page 28: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/28.jpg)
PSEUDOCODE/FLOWCHART FOR AN IF BLOCK
28
![Page 29: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/29.jpg)
EXAMPLE 1: FORM
29
txtFirstNum
txtSecondNum
txtResult
![Page 30: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/30.jpg)
EXAMPLE 1: CODE
Private Sub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2, largerNum As Double num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If num1 > num2 Then largerNum = num1 Else largerNum = num2 End If txtResult.Text = "The larger number is " & largerNumEnd Sub 30
What happens?
![Page 31: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/31.jpg)
EXAMPLE 1: OUTPUT
31
![Page 32: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/32.jpg)
EXAMPLE 2: FORM
32
![Page 33: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/33.jpg)
EXAMPLE 2: PARTIAL CODE
If costs = revenue Then txtResult.Text = "Break even"Else If costs < revenue Then profit = revenue - costs txtResult.Text = "Profit is " & _ FormatCurrency(profit) & "." Else loss = costs - revenue txtResult.Text = "Loss is " & _ FormatCurrency(loss) & "." End IfEnd If 33
What happens?
![Page 34: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/34.jpg)
EXAMPLE 2: OUTPUT
34
![Page 35: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/35.jpg)
EXAMPLE 3: FORM
35
txtAnswer
txtSolution
![Page 36: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/36.jpg)
EXAMPLE 3: CODE
Private Sub btnEvaluate_Click(...) _ Handles btnEvaluate.Click Dim answer As Double answer = CDbl(txtAnswer.Text) If (answer >= 0.5) And (answer <= 1) Then txtSolution.Text = "Good, " Else txtSolution.Text = "No, " End If txtSolution.Text &= "it holds about 3/4 of" _ & " a gallon."End Sub
36
What happens?
![Page 37: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/37.jpg)
EXAMPLE 3: OUTPUT
37
![Page 38: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/38.jpg)
EXAMPLE 4: FORM
38
mtxtAnswer
txtQuote
![Page 39: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/39.jpg)
EXAMPLE 4
Private Sub btnDisplay_Click(...) _ Handles btnDisplay.Click Dim message As String message = "Skittles is an old form of bowling in " _ & "which a wooden disk is used to knock down nine" _ & " pins arranged in a square." If txtAnswer.Text.ToUpper = "N" Then MessageBox.Show(message, "") End If txtQuote.Text = "Life ain't all beer and skittles.“ txtQuote.Text &= " – Du Maurier (1894)."End Sub
39
What happens?
![Page 40: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/40.jpg)
EXAMPLE 4: OUTPUT
40
![Page 41: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/41.jpg)
EXAMPLE 4: OUTPUT CONTINUED
41
![Page 42: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/42.jpg)
ELSEIF CLAUSE
If condition1 Then action1ElseIf condition2 Then action2ElseIf condition3 Then action3Else action4End If
42
![Page 43: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/43.jpg)
EXAMPLE 5: FORM
43
txtFirstNum
txtSecondNum
txtResult
![Page 44: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/44.jpg)
EXAMPLE 5: CODEPrivate Sub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2 As Double num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If (num1 > num2) Then txtResult.Text = "Larger number is " & num1 ElseIf (num2 > num1) Then txtResult.Text = "Larger number is " & num2 Else txtResult.Text = "The two are equal." End IfEnd Sub
44
What happens?
![Page 45: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/45.jpg)
EXAMPLE 6: FORM
45
![Page 46: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/46.jpg)
EXAMPLE 6: PARTIAL CODE
Dim ytdEarnings, curEarnings As DoubleDim socSecBenTax, medicareTax, ficaTaxes As DoubleytdEarnings = CDbl(txtToDate.Text)curEarnings = CDbl(txtCurrent.Text)If (ytdEarnings + curEarnings) <= 102000 Then socSecBenTax = 0.062 * curEarningsElseIf ytdEarnings < 102000 Then socSecBenTax = 0.062 * (102000 - ytdEarnings)End IfmedicareTax = 0.0145 * curEarningsficaTaxes = socSecBenTax + medicareTaxtxtText.Text = FormatCurrency(ficaTaxes)End Function
46
What happens?
![Page 47: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/47.jpg)
EXAMPLE 6: OUTPUT
47
![Page 48: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/48.jpg)
COMMENTS
When one If block is contained inside another If block, the structure is referred to as nested If blocks.
Care should be taken to make If blocks easy to understand.
48
![Page 49: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/49.jpg)
SIMPLIFIED NESTED IF STATEMENT
If cond1 Then If cond1 And cond2 Then
If cond2 Then action
action End If
End If
End If
49
NestedIf
Is this equivalent?
![Page 50: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/50.jpg)
MORE COMMENTS
Some programs call for selecting among many possibilities
Can be accomplished with complicated nested If blocks
Select Case block is often a better alternative
50
![Page 51: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/51.jpg)
4.3 SELECT CASE BLOCKS
51
![Page 52: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/52.jpg)
SELECT CASE BLOCK
A decision-making structure that simplifies choosing among several actions.
Avoids complex nested If constructs.
If blocks make decisions based on the truth value of a condition. Select Case choices are determined by the value of an expression called a selector.
52
![Page 53: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/53.jpg)
SELECT CASE TERMINOLOGY
Each of the possible actions is precededby a clause of the form
Case valueList
where valueList itemizes the values of theselector for which the action should betaken.
53
![Page 54: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/54.jpg)
EXAMPLE 1: FORM
54
txtPosition
txtOutcome
![Page 55: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/55.jpg)
EXAMPLE 1: CODE
Private Sub btnEvaluate_Click(...) _ Handles btnEvaluate.Click Dim position As Integer = CInt(txtPosition.Text) Select Case position Case 1 txtOutcome.Text = "Win" Case 2 txtOutcome.Text = "Place" Case 3 txtOutcome.Text = "Show" Case 4, 5 txtOutcome.Text = "You almost placed in the money." Case Else txtBox.Text = "Out of the money." End SelectEnd Sub
55
Selector
Value Lists
What happens?
![Page 56: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/56.jpg)
EXAMPLE 1: OUTPUT
56
![Page 57: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/57.jpg)
EXAMPLE 2: CODE
Private Sub btnEvaluate_Click(...) _
Handles btnEvaluate.Click
Dim position As Integer = CInt(txtPosition.Text)
Select Case position
Case 1 To 3
txtOutcome.Text = "In the money. Congratulations"
Case Is >= 4
txtOutcome.Text = "Not in the money."
End Select
End Sub
57
What happens?
![Page 58: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/58.jpg)
EXAMPLE 2: OUTPUT
58
![Page 59: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/59.jpg)
SELECT CASE SYNTAX
The general form of the Select Case block is
Select Case selector
Case valueList1
action1
Case valueList2
action2
Case Else
action of last resort
End Select
59
![Page 60: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/60.jpg)
RULES FOR SELECT CASE
Case Else (and its action) is optional Each value list contains one or more of the
following types of items separated by commas:
1. a literal (value);2. a variable;3. an expression;4. an inequality sign preceded by Is and followed
by a literal, variable, or expression;5. a range expressed in the form a To b, where a
and b are literals, variables, or expressions.
60
![Page 61: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/61.jpg)
FLOWCHART FOR SELECT CASE
61
![Page 62: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/62.jpg)
FLOWCHART FOR SELECT CASE
(not in book, but equivalent)
62
![Page 63: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/63.jpg)
EXAMPLE 3: PARTIAL CODEDim x As Integer = 2, y As Integer = 3Select Case num Case y - x, x txtPhrase.Text = "Buckle my shoe." Case Is <= 4 txtPhrase.Text = "Shut the door." Case x + y To x * y txtPhrase.Text = "Pick up sticks." Case 7, 8 txtPhrase.Text = "Lay them straight." Case Else txtPhrase.Text = "Start all over again."End Select
63
What happens?
![Page 64: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/64.jpg)
EXAMPLE 4: FORM
64
txtReply
![Page 65: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/65.jpg)
EXAMPLE 4: PARTIAL CODE
Select Case firstName Case "THOMAS" txtReply.Text = "Correct." Case "WOODROW" txtReply.Text = "Sorry, his name" _ & " was Thomas Woodrow Wilson." Case "PRESIDENT" txtReply.Text = "Are you for real?" Case Else txtReply.Text = "Nice try."End Select
65
What happens?
![Page 66: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/66.jpg)
EXAMPLE 4: OUTPUT
66
![Page 67: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/67.jpg)
EXAMPLE 7: FORM
67
![Page 68: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/68.jpg)
EXAMPLE 7: PARTIAL CODE
Dim season As String, numDays As IntegerSelect Case season.ToUpper Case "WINTER" numDays = 87 Case "SPRING" numDays = 92 Case "SUMMER", "AUTUMN", "FALL" numDays = 93End Select
68
What happens?
![Page 69: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/69.jpg)
EXAMPLE 7: FORM & OUTPUT
69
![Page 70: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/70.jpg)
COMMENTS
In a Case clause of the form Case b To c, the value of b should be less than or equalto the value of c
The word Is should precede an inequality sign in a value list
If the word Is is accidentally omitted where required, the editor will automatically insert it when checking the line 70
![Page 71: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/71.jpg)
DATA TYPE COMMENT
The items in the value list must evaluate to a literal of the same data type as the selector
For instance, if the selector evaluated to a string value, as in
Dim firstName As String = txtBox.Text
Select Case firstName
then the clause
Case firstName.Length
would be meaningless. 71
![Page 72: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/72.jpg)
BLOCK-LEVEL SCOPE
A variable declared inside an If … Then or Select Case block has block-level scope
The variable cannot be referred to outside the block
72
![Page 73: T ODAY ’ S Q UOTE "The computers do what you tell them to do, not what you want them to do. " ( Alexander Atanasov)](https://reader035.fdocuments.us/reader035/viewer/2022081515/56649e355503460f94b24c17/html5/thumbnails/73.jpg)