1 chapter 10 additional control structures and exceptions
TRANSCRIPT
1
Chapter 10Additional Control Structures
and Exceptions
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
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
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
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
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
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
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
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
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
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.
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
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.
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
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
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.
17
For Statement
For control-var = Init To End [ Step Incr ]
Statement
Next control-var
For Statement Syntax
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
19
Example of Repetition
Dim num As Integer
For num = 1 To 3
Console.WriteLine("P" & num)
Next
20
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
?
21
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
1
22
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
1
Potato1
23
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
1
Potato1
24
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
2
Potato1
25
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
2
Potato1Potato2
26
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
2
Potato1Potato2
27
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
3
Potato1Potato2
28
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
3
Potato1Potato2Potato3
29
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
3
Potato1Potato2Potato3
30
Example of Repetition
num
Dim num As Integer
For num = 1 To 3
Console.WriteLine ("Potato" & num)
Next
OUTPUT
4
Potato1Potato2Potato3
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
32
The output was:
Potato1
Potato2
Potato3
33
What output from this loop?
Dim count As Integer
For count = 1 To 0
Console.WriteLine ("*")
Next
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
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
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
37
VB .NET Has Combined Assignment Operators
Dim pivotPoint As Integer
pivotPoint = pivotPoint * (n+3)
OR,
pivotPoint *= n+3
38
Write a statement to subtract 10 from weight
Dim weight As Integer
weight = weight - 10
OR,
weight -= 10
39
Write a statement to divide money by 5.0
Dim money As Double
money = money / 5.0
OR,
money /= 5.0;
40
Write a statement to double profits
Dim profits As Double
profits = profits * 2.0
OR,
profits *= 2.0
41
Write a statement to raise cost 15%
Dim cost As Double
OR,
cost = 1.15 * cost;
OR,
cost *= 1.15 ;
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
43
Case Study: Rainfall
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
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)
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
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
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報表紙上,勿使用電腦列印