Class Scheduling: Exceptions ? What Exceptions ? T2.2 February 5, 2013
1 Chapter 10 Additional Control Structures and Exceptions.
-
Upload
ross-dalton -
Category
Documents
-
view
214 -
download
0
Transcript of 1 Chapter 10 Additional Control Structures and Exceptions.
![Page 1: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/1.jpg)
1
Chapter 10Additional Control Structures
and Exceptions
![Page 2: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/2.jpg)
2
Chapter 10 Topics
Select Case Multiway Branching Structure
Do-Loop While Statement for Looping For statement for Looping Bitwise Logical Operators Assignment operators Exception Handling using Try and Catch Defining Exception Classes
![Page 3: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/3.jpg)
3
10.1 Select Case Statement
Is a selection control structure for multiway branching(多重分支 ).
SYNTAXSelect Case (Test Expression) Case [expression1 to expression2] [Is comparison
operator expression] [expression], [expression]
Statements(s)... Case Else
Statement(s)End Select
![Page 4: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/4.jpg)
4
Select Case (digit) Case 1
Console.WriteLine( “digit is " & “1”) Case 2, 3
Console.WriteLine (“digit is " & “2 or 3”) Case 4
Console.WriteLine (“digit is " & “4”) Case Else
Console.WriteLine (“digit is " & “others”)
End Select
![Page 5: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/5.jpg)
5
Select Case Statement
The value of TestExpresssion (of Boolean, Byte, Char, Date, Double, Decimal, Integer, Long, Object, Short, Single, and String) determines which branch is executed
Case expressions can consist of a single expression, multiple expressions separated by commas, of the form Expression1 to Expression2 Is (comparison operator) expression
![Page 6: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/6.jpg)
6
Control in Select Case Statement
Control branches to the statement following the Case clause whose expression matches the TestExpression . Each statement in the clause is executed, and then control falls out of the Select Case Statement.
If no Case clause expression matches the TestExpression, control falls to the statement inside Case Else, if present. Otherwise, control falls out of the Select Case Statement
![Page 7: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/7.jpg)
7
Select Case (digit) Case 2, 3
Console.WriteLine (“digit is " & “2 or 3”) Case 1
Console.WriteLine( “digit is " & “1”) Case 4
Console.WriteLine (“digit is " & “4”) Case Else
Console.WriteLine (“digit is " & “others”)
End Select
![Page 8: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/8.jpg)
8
Select Case (grade) Case "A", "B"
MsgBox( “Good Work”) Case “C"
MsgBox( “Average Work”) Case “D“, “F”
MsgBox( “Poor Work”) Case Else
MsgBox( grade & “is not a valid grade”)End Select
![Page 9: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/9.jpg)
9
Select Case (grade) Case 80 to 100
MsgBox( “Good Work”) Case 70 to 79
MsgBox( “Average Work”) Case 0 to 69
MsgBox( “Poor Work”) Case Else
MsgBox( grade & “is not a valid grade”)End Select
![Page 10: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/10.jpg)
10
Select Case (grade) Case Is > 79
MsgBox( “Good Work”) Case Is >= 70
MsgBox( “Average Work”) Case Is >= 0
MsgBox( “Poor Work”) Case Else
MsgBox( grade & “is not a valid grade”)End Select
![Page 11: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/11.jpg)
11
Do While-Loop While Statement Syntax
Very similar to the While Statement.
Do While-Loop Statement
Do While (Expression)statement
Loop
Loop body can be a single statement or a block.
![Page 12: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/12.jpg)
12
'Count controlled repetitionsum = 0 'Initializecounter = 1Do While (counter <= 10) 'Condition sum = sum + counter counter += 1 'Incrementloop
Using Do While-Loop to implement a count-controlled loop
![Page 13: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/13.jpg)
13
Do-Loop While Statement Syntax
Is a looping control structure in which the loop condition is tested after executing the body of the loop.
Do-Loop While Statement
Dostatement
Loop While (Expression)
Loop body can be a single statement or a block.
![Page 14: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/14.jpg)
14
Do-Loop While Flowchart
When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the Do-Loop While statement.
Statement
Expression
DO
LOOP WHILE
False
True
![Page 15: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/15.jpg)
15
'Count controlled repetitionsum = 0 'Initializecounter = 1Do sum = sum + counter counter += 1 'Incrementloop While (counter <= 10) 'Condition
'Count controlled repetition
sum = 0 'Initializecounter = 1While (counter <= 10) sum = sum + counter counter += 1End While
Using Do-Loop While to implement a count-controlled loop
![Page 16: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/16.jpg)
16
Do-Loop vs. While
POSTTEST loop (exit-condition)
(後測離開條件 ) The loop condition is
tested after executing the loop body.
Loop body is always executed at least once.
PRETEST loop (entry-condition)
(前測進入條件 ) The loop condition is
tested before executing the loop body.
Loop body may not be executed at all.
![Page 17: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/17.jpg)
17
For Statement
For control-var = Init To End [ Step Incr ]
Statement
Next control-var
For Statement Syntax
![Page 18: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/18.jpg)
18
The For statement contains
an initial value for the control variable
an ending value for the control variable
an optional increment value for the control variable after each iteration of the loop body
![Page 19: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/19.jpg)
19
Example of Repetition
Dim num As Integer
For num = 1 To 3
Console.WriteLine("P" & num)
Next
![Page 20: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/20.jpg)
20
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
?
![Page 21: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/21.jpg)
21
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
1
![Page 22: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/22.jpg)
22
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
1
Potato1
![Page 23: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/23.jpg)
23
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
1
Potato1
![Page 24: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/24.jpg)
24
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
2
Potato1
![Page 25: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/25.jpg)
25
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
2
Potato1Potato2
![Page 26: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/26.jpg)
26
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
2
Potato1Potato2
![Page 27: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/27.jpg)
27
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
3
Potato1Potato2
![Page 28: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/28.jpg)
28
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
3
Potato1Potato2Potato3
![Page 29: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/29.jpg)
29
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
3
Potato1Potato2Potato3
![Page 30: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/30.jpg)
30
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
4
Potato1Potato2Potato3
![Page 31: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/31.jpg)
31
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
4
When the loop control variable (num) obtains a value greater than the ending value in the for, the loop is said to be “satisfied” and control passes to the statement following the for structure
![Page 32: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/32.jpg)
32
The output was:
Potato1
Potato2
Potato3
![Page 33: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/33.jpg)
33
What output from this loop?
Dim count As Integer
For count = 1 To 0
Console.WriteLine ("*")
Next
![Page 34: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/34.jpg)
34
No output from the for loop! Why?
The initial value of the loop control variable is greater than the ending value.
This means the loop is already satisfied. Control passes to the statement past the
end of the for loop
OUTPUT
![Page 35: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/35.jpg)
35
' Calculating compound interestDim year As IntegerDim amount As DoubleDim principle As Double = 1000.0Dim rate As Double = 0.07Console.WriteLine ("Year Amount")For year = 1 to 10 amount = principle * Math.Pow(1.0 + rate, year) Console.WriteLine(year & " " & amount)Next
Another count-controlled loop
![Page 36: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/36.jpg)
36
VB .NET Has Combined Assignment Operators
Dim i As Integer
Write a statement to add 5 to i.
i = i + 5
OR,
i += 5
![Page 37: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/37.jpg)
37
VB .NET Has Combined Assignment Operators
Dim pivotPoint As Integer
pivotPoint = pivotPoint * (n+3)
OR,
pivotPoint *= n+3
![Page 38: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/38.jpg)
38
Write a statement to subtract 10 from weight
Dim weight As Integer
weight = weight - 10
OR,
weight -= 10
![Page 39: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/39.jpg)
39
Write a statement to divide money by 5.0
Dim money As Double
money = money / 5.0
OR,
money /= 5.0;
![Page 40: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/40.jpg)
40
Write a statement to double profits
Dim profits As Double
profits = profits * 2.0
OR,
profits *= 2.0
![Page 41: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/41.jpg)
41
Write a statement to raise cost 15%
Dim cost As Double
OR,
cost = 1.15 * cost;
OR,
cost *= 1.15 ;
![Page 42: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/42.jpg)
42
Precedence (highest to lowest)Operator Associativity ( ) Left to right
^ Left to right- (negation) Left to right
* / Left to right \ (Integer division) Left to right Mod Left to right + - Left to right& (String concatenation) Left to right = Left to right <> Left to right <,> Left to right > = Left to right< = Left to right= += -= *= /= Right to left
![Page 43: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/43.jpg)
43
Case Study: Rainfall
![Page 44: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/44.jpg)
44
Imports System.IOModule Module1 Class DataSetException : Inherits SystemException Public Sub New(ByVal message As String) MyBase.New(message) End Sub End Class
Public Sub processOneSite(ByVal inFile As StreamReader, _ ByVal outFile As StreamWriter, _ ByVal dataSetName As String)
Dim count As Integer ' Loop control variable Dim amount As Double ' Rain for one month Dim sum As Double = 0.0 ' Sums annual rainfall Dim dataLine As String ' Input from inFile Dim currentValue As String ' String for numeric Dim index As Integer
Rainfall program
![Page 45: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/45.jpg)
45
Try dataLine = inFile.ReadLine() For count = 1 To 12 index = dataLine.IndexOf(" ") If (index > 0) Then currentValue = dataLine.Substring(0, index) dataLine = dataLine.Substring(Math.Min(index + 1, _ dataLine.Length()), _ dataLine.Length()) Else currentValue = dataLine End If amount = Double.Parse(currentValue) If (amount < 0.0) Then Throw New DataSetException("Negative in ") Else sum = sum + amount End If Next outFile.WriteLine("Average for " & dataSetName & _ " is " & sum / 12.0)
![Page 46: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/46.jpg)
46
Catch except As IOException outFile.WriteLine("IOException with site " & _
dataSetName) Catch except As FormatException outFile.WriteLine("FormatException in site " & _
dataSetName) Catch except As DataSetException outFile.WriteLine(except.ToString & dataSetName)
End Try
End Sub End Module
![Page 47: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/47.jpg)
47
Sub Main() Dim dataSetName As String ' Reporting station name Dim inFile As StreamReader ' Data file Dim outFile As StreamWriter ' Output file inFile = File.OpenText("rainData.dat") outFile = File.CreateText("outfile.dat")
' Get name of reporting station dataSetName = inFile.ReadLine
' Processing loop Do processOneSite(inFile, outFile, dataSetName) dataSetName = inFile.ReadLine() Loop While (inFile.Peek <> -1) inFile.Close() outFile.Close() Console.Write("press enter to quit") Console.Read() End Sub
![Page 48: 1 Chapter 10 Additional Control Structures and Exceptions.](https://reader035.fdocuments.in/reader035/viewer/2022062720/56649f1b5503460f94c308cf/html5/thumbnails/48.jpg)
48
Exercises
Chapter 9 Programming problems: 1
Chapter 10 Exam preparation exercises:
– 5, 10,15– 11 ( For i=5 To 2 Step -1 )– 12 ( For row = 1 To 8 , 所有 10改為 8)
Programming warm-up exercises: – 1,5,7,9
Due Date: 12/15 ( 三 ) 請寫在 A4報表紙上,勿使用電腦列印