Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 10 Additional Control Structures and Exceptions.

Similar presentations


Presentation on theme: "1 Chapter 10 Additional Control Structures and Exceptions."— Presentation transcript:

1 1 Chapter 10 Additional Control Structures and Exceptions

2 2 Chapter 10 Topics Select Case Multiway Branching Structure Do-Loop While Statement for Looping For statement for Looping l Bitwise Logical Operators l Assignment operators l Exception Handling using Try and Catch l Defining Exception Classes

3 3 10.1 Select Case Statement Is a selection control structure for multiway branching( 多重分支 ). SYNTAX Select Case (Test Expression) Case [expression1 to expression2] [Is comparison operator expression] [expression], [expression] Statements(s). Case Else Statement(s) End Select

4 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 5 Select Case Statement l The value of TestExpresssion (of Boolean, Byte, Char, Date, Double, Decimal, Integer, Long, Object, Short, Single, and String) determines which branch is executed l Case expressions can consist of a single expression, multiple expressions separated by commas, of the form n Expression1 to Expression2 n Is (comparison operator) expression

6 6 Control in Select Case Statement l 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. l 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 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 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 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 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 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 12 'Count controlled repetition sum = 0 'Initialize counter = 1 Do While (counter <= 10) 'Condition sum = sum + counter counter += 1 'Increment loop Using Do While-Loop to implement a count-controlled loop

13 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 Do statement Loop While (Expression) Loop body can be a single statement or a block.

14 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 15 'Count controlled repetition sum = 0 'Initialize counter = 1 Do sum = sum + counter counter += 1 'Increment loop While (counter <= 10) 'Condition 'Count controlled repetition sum = 0 'Initialize counter = 1 While (counter <= 10) sum = sum + counter counter += 1 End While Using Do-Loop While to implement a count-controlled loop

16 16 Do-Loop vs. While l POSTTEST loop (exit-condition) ( 後測離開條件 ) l The loop condition is tested after executing the loop body. l Loop body is always executed at least once. l PRETEST loop (entry-condition) ( 前測進入條件 ) l The loop condition is tested before executing the loop body. l Loop body may not be executed at all.

17 17 For Statement For control-var = Init To End [ Step Incr ] Statement Next control-var For Statement Syntax

18 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 19 Example of Repetition Dim num As Integer For num = 1 To 3 Console.WriteLine("P" & num) Next

20 20 Example of Repetition num Dim num As Integer For num = 1 To 3 Console.WriteLine ("Potato" & num) Next OUTPUT ?

21 21 Example of Repetition num Dim num As Integer For num = 1 To 3 Console.WriteLine ("Potato" & num) Next OUTPUT 1

22 22 Example of Repetition num Dim num As Integer For num = 1 To 3 Console.WriteLine ("Potato" & num) Next OUTPUT 1 Potato1

23 23 Example of Repetition num Dim num As Integer For num = 1 To 3 Console.WriteLine ("Potato" & num) Next OUTPUT 1 Potato1

24 24 Example of Repetition num Dim num As Integer For num = 1 To 3 Console.WriteLine ("Potato" & num) Next OUTPUT 2 Potato1

25 25 Example of Repetition num Dim num As Integer For num = 1 To 3 Console.WriteLine ("Potato" & num) Next OUTPUT 2 Potato1 Potato2

26 26 Example of Repetition num Dim num As Integer For num = 1 To 3 Console.WriteLine ("Potato" & num) Next OUTPUT 2 Potato1 Potato2

27 27 Example of Repetition num Dim num As Integer For num = 1 To 3 Console.WriteLine ("Potato" & num) Next OUTPUT 3 Potato1 Potato2

28 28 Example of Repetition num Dim num As Integer For num = 1 To 3 Console.WriteLine ("Potato" & num) Next OUTPUT 3 Potato1 Potato2 Potato3

29 29 Example of Repetition num Dim num As Integer For num = 1 To 3 Console.WriteLine ("Potato" & num) Next OUTPUT 3 Potato1 Potato2 Potato3

30 30 Example of Repetition num Dim num As Integer For num = 1 To 3 Console.WriteLine ("Potato" & num) Next OUTPUT 4 Potato1 Potato2 Potato3

31 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 32 The output was: Potato1 Potato2 Potato3

33 33 What output from this loop? Dim count As Integer For count = 1 To 0 Console.WriteLine ("*") Next

34 34 l No output from the for loop! Why? l The initial value of the loop control variable is greater than the ending value. l This means the loop is already satisfied. l Control passes to the statement past the end of the for loop OUTPUT

35 35 ' Calculating compound interest Dim year As Integer Dim amount As Double Dim principle As Double = 1000.0 Dim rate As Double = 0.07 Console.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 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 37 VB.NET Has Combined Assignment Operators Dim pivotPoint As Integer p ivotPoint = pivotPoint * (n + 3) OR, pivotPoint *= n+3

38 38 Write a statement to subtract 10 from weight Dim weight As Integer weight = weight - 10 OR, weight - = 10

39 39 Write a statement to divide money by 5.0 Dim money As Double money = money / 5.0 OR, money / = 5.0;

40 40 Write a statement to double profits Dim profits As Double profits = profits * 2.0 OR, profits * = 2.0

41 41 Write a statement to raise cost 15% Dim cost As Double OR, cost = 1.15 * cost; OR, cost * = 1.15 ;

42 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 43 Case Study: Rainfall

44 44 Imports System.IO Module 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 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 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 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 48 Exercises l Chapter 9 n Programming problems: 1 l Chapter 10 n Exam preparation exercises: –5, 10,15 –11 ( For i=5 To 2 Step -1 ) –12 ( For row = 1 To 8, 所有 10 改為 8) n Programming warm-up exercises: –1,5,7,9 l Due Date: 12/15 ( 三 ) l 請寫在 A4 報表紙上,勿使用電腦列印


Download ppt "1 Chapter 10 Additional Control Structures and Exceptions."

Similar presentations


Ads by Google