visual basic - chapter 4 - ucrmshok002/imefall2012/ch04.pdf · 1 visual basic - chapter 4 ......
TRANSCRIPT
![Page 1: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/1.jpg)
1
Visual Basic - Chapter 4
Mohammad Shokoohi
* Adopted from An Introduction to Programming Using Visual Basic 2010, Schneider
![Page 2: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/2.jpg)
2
Chapter 4 – Decisions4.1 Relational and Logical Operators4.2 If Blocks4.3 Select Case Blocks4.4 Input via User Selection
![Page 3: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/3.jpg)
3
4.1 Relational and Logical Operators
• ANSI Values• Relational Operators• Logical Operators• Boolean Data Type• Two Boolean-Valued Methods• A Boolean-Valued Function
![Page 4: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/4.jpg)
4
Condition
• A condition is an expression involving relational and/or logical operators
• The value of the condition is Boolean –that is, True or False
![Page 5: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/5.jpg)
5
ANSI Character Set A numeric representation for every key on the keyboard and for other assorted characters.
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 6: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/6.jpg)
6
ANSI Character Set (continued)
A numeric representation for every key on the keyboard and for other assorted characters.
162 ¢ 177 ± 181 µ 190 ¼ 169 © 178 ² 188 ¼ 247 ÷ 176 ° 179 ³ 189 ½ 248 ø
![Page 7: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/7.jpg)
7
Chr FunctionFor n between 0 and 255,
Chr(n)
is the string consisting of the character withANSI value n.
Examples: Chr(65) is A
Chr(162) is ¢
![Page 8: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/8.jpg)
8
Asc FunctionFor a string str,Asc(str)
is ANSI value of the first character of str.
Examples: Asc("A") is 65
Asc("¢25") is 162
![Page 9: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/9.jpg)
9
Relational Operators< less than<= less than or equal to> greater than>= greater than or equal to= equal to<> not equal toANSI values are used to decide order for strings.
![Page 10: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/10.jpg)
10
Condition
• A condition is an expression involving relational and/or logical operators.
• Result of the condition is True or False.
![Page 11: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/11.jpg)
11
ExampleWhen a = 3, b = 4
(a + b) < 2 * a
3 + 4 = 7 2 * 3 = 6
7 is NOT less than 6 and so the value of the expression is False
![Page 12: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/12.jpg)
12
Another Examplea = 4 b = 3 c = "hello" d = "bye"( c.Length – b ) = ( a / 2 )
5 – 3 = 2 4 / 2 = 2
True because 2 equals 2
![Page 13: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/13.jpg)
13
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
![Page 14: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/14.jpg)
14
Logical OperatorsUsed 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
![Page 15: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/15.jpg)
15
Example 4.3n = 4, answ = “Y” Are the following
expressions true or false?
Not (n < 6)(answ = "Y") Or (answ = "y")(answ = "Y") And (answ = "y")Not(answ = "y")
![Page 16: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/16.jpg)
16
Boolean Expression• An expression that evaluates to either
True or False is said to have Boolean data type.
• Example:The statementtxtBox.Text = CStr((2 + 3) < 6)
displays True in the text box.
![Page 17: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/17.jpg)
17
Boolean Variable A variable declared with a statement of the form
Dim var As Boolean
Has Boolean data type. It can assumejust the two values True and False.
Example: Dim boolVar As Boolean
boolVar = 2 < 6txtBox.Text = CStr(boolVar)
displays True in the text box.
![Page 18: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/18.jpg)
18
Syntax Error
The following is NOT a valid way to test whether n falls between 2 and 5:
2 < n < 5
![Page 19: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/19.jpg)
19
Correction to Syntax ErrorTo 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.
![Page 20: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/20.jpg)
20
Common Error in Boolean Expressions
• A common error is to replace the condition Not ( 2 < 3 ) with the condition ( 2 > 3 ).
• The correct replacement is ( 2 >= 3 )because >= is the opposite of <, just as <= is the opposite of >.
![Page 21: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/21.jpg)
Two Boolean-Valued Methods• The expression strVar1.EndsWith(strVar2)
is true if the value of the first variable ends with the value of the second variable
• The expression strVar1.StartsWith(strVar2)is true if the value of the first variable begins with the value of the second variable
• Note: String literals can be used instead of string variables
21
![Page 22: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/22.jpg)
ExamplesAfter the following code is executed each text box will contain the word True.
Dim firstName As String = "William"txtBox1.Text = firstName.EndsWith("am") txtBox2.Text = firstName.StartsWith("Will")
22
![Page 23: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/23.jpg)
A Boolean-Valued Function• The expression IsNumeric(strVar)
is true if the value of strVar can be converted to a number with CInt or CDbl. Note: The string variable can be replaced with a string literal.
• Examples: IsNumeric("123") is trueIsNumeric("$123") is true IsNumeric("3 - 2") is false
23
![Page 24: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/24.jpg)
24
4.2 If Blocks• If Block• Nested If Blocks• ElseIf Clauses• Input Validation with If Blocks
![Page 25: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/25.jpg)
25
If BlockThe following program will take a course of action based on whether a condition is true.If condition Then
action 1Else
action 2End If
Will be executed if condition is true
Will be executed if condition is false
![Page 26: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/26.jpg)
26
Another example If BlockIf condition Then
action 1End If
Statement 2Statement 3
Regardless of whether
the condition in the
If statement is true or
false, these statements
will be executed
![Page 27: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/27.jpg)
27
Pseudocode and Flowchart for an If Block
![Page 28: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/28.jpg)
28
Example 1: Form
txtFirstNum
txtSecondNum
txtResult
![Page 29: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/29.jpg)
29
Example 1: CodePrivate Sub btnFindLarger_Click(...) _
Handles btnFindLarger.ClickDim num1, num2, largerNum As Doublenum1 = CDbl(txtFirstNum.Text)num2 = CDbl(txtSecondNum.Text)If num1 > num2 Then
largerNum = num1Else
largerNum = num2End IftxtResult.Text = "Larger number: " & largerNum
End Sub
![Page 30: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/30.jpg)
30
Example 1: Output
![Page 31: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/31.jpg)
31
Example 2: Form
txtAnswer
txtSolution
![Page 32: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/32.jpg)
32
Example 2: CodePrivate Sub btnEvaluate_Click(...) _
Handles btnEvaluate.ClickDim answer As Doubleanswer = CDbl(txtAnswer.Text)If (answer >= 0.5) And (answer <= 1) Then
txtSolution.Text = "Good, "Else
txtSolution.Text = "No, "End IftxtSolution.Text &= "it holds about 3/4 gals."
End Sub
![Page 33: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/33.jpg)
33
Example 2: Output
![Page 34: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/34.jpg)
34
Example 3: Form
mtbAnswer
txtQuote
![Page 35: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/35.jpg)
35
Example 3Private Sub btnDisplay_Click(...) _
Handles btnDisplay.ClickDim msg As String msg = "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" ThenMessageBox.Show(msg, "")
End IftxtQuote.Text = "Life ain't all beer " &
and skittles. – Du Maurier (1894)."End Sub
![Page 36: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/36.jpg)
36
Example 3: Output
![Page 37: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/37.jpg)
37
Example 3: Output (continued)
![Page 38: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/38.jpg)
38
Nested If BlocksWhen one If block is contained inside another If block, the structure is referred to as nested If blocks.
![Page 39: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/39.jpg)
39
Example 5: Form
![Page 40: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/40.jpg)
40
Example 5: Partial CodeIf costs = revenue Then
txtResult.Text = "Break even"Else
If costs < revenue Thenprofit = revenue - coststxtResult.Text = "Profit is " &
FormatCurrency(profit) & "."Else
loss = costs - revenuetxtResult.Text = "Loss is " &
FormatCurrency(loss) & "."End If
End If
![Page 41: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/41.jpg)
41
Example 5: Output
![Page 42: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/42.jpg)
42
ElseIf ClauseIf condition 1 Then
action 1ElseIf condition 2 Then
action 2ElseIf condition 3 Then
action 3Else
action 4End If
![Page 43: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/43.jpg)
43
Example 6: Form
txtFirstNum
txtSecondNum
txtResult
![Page 44: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/44.jpg)
44
Example 6: CodePrivate Sub btnFindLarger_Click(...) _
Handles btnFindLarger.ClickDim num1, num2 As Doublenum1 = CDbl(txtFirstNum.Text)num2 = CDbl(txtSecondNum.Text)If (num1 > num2) Then
txtResult.Text = "Larger number is " & num1ElseIf (num2 > num1) Then
txtResult.Text = "Larger number is " & num2Else
txtResult.Text = "The two are equal."End If
End Sub
![Page 45: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/45.jpg)
45
Example 7: Form
![Page 46: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/46.jpg)
46
Example 7: Partial CodeConst WAGE_BASE As Double = 106800 Dim ytdEarnings, curEarnings As DoubleDim socSecBenTax, medicareTax,
ficaTaxes As DoubleytdEarnings = CDbl(txtToDate.Text)curEarnings = CDbl(txtCurrent.Text)If (ytdEarnings + curEarnings) <= WAGE_BASE ThensocSecBenTax = 0.062 * curEarningsElseIf ytdEarnings < WAGE_BASE ThensocSecBenTax = 0.062 * (WAGE_BASE -ytdEarnings)
End IfmedicareTax = 0.0145 * curEarningsficaTaxes = socSecBenTax + medicareTax
![Page 47: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/47.jpg)
47
Example 7: Output
![Page 48: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/48.jpg)
Input Validation
The statementIf (IsNumeric(txtBox.Text) = True) Then
is commonly used to validate that input is numeric. It can be condensed toIf IsNumeric(txtBox.Text) Then
48
![Page 49: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/49.jpg)
49
Simplified Nested If StatementCare should be taken to make If blocks easy to understand. If cond1 Then If cond1 And cond2 Then actionIf cond2 Then End If
actionEnd If
End If
Confusing
Clear
![Page 50: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/50.jpg)
50
Comment
Some programs call for selecting among many possibilities. Although such tasks can be accomplished with complicated nested If blocks, the Select Case block (discussed in Section 4.3) is often a better alternative.
![Page 51: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/51.jpg)
51
4.3 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.
![Page 52: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/52.jpg)
52
Select Case TerminologyEach 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.
![Page 53: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/53.jpg)
53
Example 1: Form
txtPosition
txtOutcome
![Page 54: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/54.jpg)
54
Example 1: CodePrivate Sub btnEvaluate_Click(...) _
Handles btnEvaluate.ClickDim position As Integer = CInt(txtPosition.Text)Select Case positionCase 1txtOutcome.Text = "Win"
Case 2txtOutcome.Text = "Place"
Case 3txtOutcome.Text = "Show"
Case 4, 5txtOutcome.Text = "Almost in the money."
Case ElsetxtBox.Text = "Out of the money."
End SelectEnd Sub
Selector
Value Lists
![Page 55: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/55.jpg)
55
Example 1: Output
![Page 56: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/56.jpg)
56
Example 2: Form
txtPosition
txtOutcome
![Page 57: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/57.jpg)
57
Example 2: CodePrivate Sub btnEvaluate_Click(...) _
Handles btnEvaluate.ClickDim position As Integer =
CInt(txtPosition.Text)Select Case position
Case 1 To 3txtOutcome.Text =
"In the money. Congratulations"Case Is >= 4
txtOutcome.Text = "Not in the money."End Select
End Sub
![Page 58: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/58.jpg)
58
Example 2: Output
![Page 59: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/59.jpg)
59
Select Case SyntaxThe general form of the Select Case block isSelect Case selector
Case valueList1action1
Case valueList2action2
Case Elseaction of last resort
End Select
![Page 60: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/60.jpg)
60
Rules for Select CaseEach value list contains one or more of the following types of items separated by commas.1. a literal2. a variable3. an expression4. an inequality sign preceded by Is and
followed by a literal, variable, or expression5. a range given in the form a To b, where a
and b are literals, variables, or expressions.
![Page 61: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/61.jpg)
61
Flowchart for Select Case
![Page 62: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/62.jpg)
62
Example 4: Form
txtReply
![Page 63: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/63.jpg)
63
Example 4: Partial CodeSelect 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
![Page 64: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/64.jpg)
64
Example 4: Output
![Page 65: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/65.jpg)
65
Example: Form
![Page 66: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/66.jpg)
66
Example: Partial CodeDim numDays As IntegerDim season = txtSeason.TextSelect Case season.ToUpper
Case "WINTER"numDays = 87
Case "SPRING"numDays = 92
Case "SUMMER", "AUTUMN", "FALL" numDays = 93
End Select
![Page 67: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/67.jpg)
67
Example: Output
![Page 68: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/68.jpg)
68
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.
![Page 69: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/69.jpg)
69
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 inDim firstName As String = txtBox.TextSelect Case firstName
then the clauseCase firstName.Length
would be meaningless.
![Page 70: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/70.jpg)
70
Block-Level Scope• A variable declared inside an If or Select
Case block has block-level scope.• The variable cannot be referred to outside
of the block.
![Page 71: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/71.jpg)
4.4 Input via User Selection• Using a List Box for Input• Using Radio Buttons for Input• Using Check Boxes for Input• Events Raised by Selections
71
![Page 72: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/72.jpg)
The Three Types of Controls Used for Selection
72
list box
radio buttons
check boxes
![Page 73: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/73.jpg)
Fill a List Box at Design Time via its String Collection Editor
73
Tasks button
click here to invoke string collection editor
![Page 74: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/74.jpg)
String Collection Editor
74
Fill by direct typing or by copying and pasting from a text editor or a spreadsheet.
![Page 75: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/75.jpg)
List Box at Run Time
75
lstMonths
selected item
The value of lstMonths.Text is the string consisting of the selected item.
![Page 76: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/76.jpg)
Example 1: Code for btnDetermine.Click
Dim daysInMonth As StringSelect Case lstMonths.Text
Case "September", "April", "June", "November"daysInMonth = "30"
Case "February"daysInMonth = "28 or 29"
Case ElsedaysInMonth = "31"
End SelecttxtDays.Text = daysInMonth
76
![Page 77: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/77.jpg)
77
The Group Box Control• Group boxes are passive objects used to
group other objects together.• When you drag a group box, the attached
controls follow as a unit.• To attach controls to a group box, create
the group box and then place the controls into the group box.
![Page 78: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/78.jpg)
78
Group Box Example
Three attached controls:
Button1Button2Button3
![Page 79: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/79.jpg)
79
Radio Button Properties• To determine if the button is on or off
radButton.Checked
has value True if button is on.
• To turn a radio button onradButton.Checked = True
![Page 80: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/80.jpg)
80
Example 3: Form
radChild
radAdult
radSenior
radMinor
![Page 81: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/81.jpg)
81
Example 3: Code for ButtonIf radChild.Checked Then
txtFee.Text = FormatCurrency(0)ElseIf radMinor.Checked Then
txtFee.Text = FormatCurrency(5)ElseIf radAdult.Checked Then
txtFee.Text = FormatCurrency(10)ElseIf radSenior.Checked Then
txtFee.Text = FormatCurrency(7.5)Else
MessageBox.Show("Must make a selection.")End If
![Page 82: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/82.jpg)
82
Example 3: Output
![Page 83: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/83.jpg)
83
The Check Box Control• Consists of a small square and a caption• Presents the user with a Yes/No choice• During run time, clicking on the check box
toggles the appearance of a check mark.• Checked property has value True when
check box is checked and False when not• CheckedChanged event is raised when the
user clicks on the check box
![Page 84: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/84.jpg)
84
Example 4: Form
chkDrug
chkDentalchkVision
chkMedical
![Page 85: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/85.jpg)
Example 4: Code for ButtonDim sum As Double = 0If chkDrugs.Checked Then
sum += 39.15End IfIf chkDental.Checked Then
sum += 10.81End IfIf chkVision.Checked Then
sum += 2.25End IfIf chkMedical.Checked Then
sum += 55.52End IftxtTotal.Text = FormatCurrency(sum)
85
![Page 86: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/86.jpg)
Example 4: Output
86
![Page 87: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/87.jpg)
Events Raised by a Selection
• SelectedIndexChanged – raised when a new item of a list box is selected
• CheckedChanged - raised when the user clicks on an unchecked radio button or a check box; that is, when the value of the Checked property is changed.
87
![Page 88: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/88.jpg)
88
Example 5: CodePrivate Sub checkBox_Changed(...) Handles _
chkDrugs.CheckedChanged,chkDental.CheckedChanged,chkVision.CheckedChanged,chkMedical.CheckChanged
Dim sum As Double = 0If chkDrugs.Checked Then
sum += 39.15End If
(continued on next slide)
![Page 89: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/89.jpg)
89
Example 5: Code (continued)If chkDental.Checked Then
sum += 10.81End IfIf chkVision.Checked Then
sum += 2.25End IfIf chkMedical.Checked Then
sum += 55.52End IftxtTotal.Text = FormatCurrency(sum)
End Sub
![Page 90: Visual Basic - Chapter 4 - UCRmshok002/IMEfall2012/Ch04.pdf · 1 Visual Basic - Chapter 4 ... (answer >= 0.5) And (answer](https://reader031.vdocuments.us/reader031/viewer/2022030403/5a78fdc87f8b9a9d218cafab/html5/thumbnails/90.jpg)
90
Example 5: Output