Upload
ahmad-al-bakry
View
46
Download
4
Tags:
Embed Size (px)
Citation preview
Introducing the Do While…Loop and Do Until…Loop Repetition Statements
Introducing the Do…Loop While and Do…Loop Until Repetition Statements
Do While…Loop Repetition Statement
• Loop-continuation condition– While loop-continuation condition remains
true, loop statement executes its body repeatedly
– When loop-continuation condition becomes false, loop terminates
• Code example:– Dim intProduct As Integer = 3
Do While intProduct <= 50 intProduct *= 3
Loop
Do While…Loop Repetition Statement
[intProduct <= 50]
[intProduct > 50]
triple product va lue
merge
dec ision
Corresponding Visua l Basic .NET statement:
intProduct *= 3
Figure 9.4 Do While Loop repetition statement UML activity diagram.
Do Until…Loop Repetition Statement
• Loop-continuation condition– While loop-continuation condition remains
false, loop statement executes its body repeatedly
– When loop-continuation condition becomes true, loop terminates
• Code example:– Dim intProduct As Integer = 3
Do Until intProduct > 50 intProduct *= 3
Loop
Do Until…Loop Repetition Statement
[intProduct <= 50]
[intProduct > 50]
triple product va lue
merge
decision
Corresponding Visual Basic .NET statement:
intProduct *= 3
Figure 9.5 Do Until Loop repetition statement UML activity diagram
Do…Loop While Repetition Statement
• Do…Loop While example: Dim intCouner As Integer = 1
Do 1stDisplay/Items.Add(intCounter) intCounter += 1 Loop While intCounter <= 3
• Set counter to 1– Excecute body of Do…Loop While statement while the counter is less than or equal to 3
Do…Loop While Repetition Statement
decision
action state
[intCounter <= 3]
[intCounter > 3]
Corresponding VB .NET Statements:lstDisplay.Items.Add(intCounter)intCounter += 1
loop-continuation
condition
Do…Loop While repetition statement UML activity diagram.
• Do…Loop While repetition statement– Body of the loop executes first
– Loop-continuation condition checked
• If False, loop exits
• If True, loop executes again
Do…Loop Until Repetition Statement
• Do…Loop Until example: Dim intCouner As Integer = 1
Do 1stDisplay/Items.Add(intCounter) intCounter += 1 Loop Until intCounter > =3
• Set counter to 1– Execute body of Do…Loop Until statement until the counter is greater than to 3
Do…Loop Until Repetition Statement
Decision
action state
[intCounter <= 3]
[intCounter > 3]
Corresponding VB .NET Statements:lstDisplay.Items.Add(intCounter)
intCounter += 1
Loop-termination c ondition
Do…Loop Until repetition statement UML activity diagram.
• Do…Loop Until repetition statement– Body of the loop executes
– Loop-continuation condition checked
• If False, loop executes again
• If True, loop exits
Introducing the Do…Loop While and Do…Loop Until Repetition Statements
Class Average Application
Test-Driving the Class Average Application
Application Requirements A teacher regularly issues quizzes to a class of ten students. The grades on these quizzes are integers in the range from 0 to 100 (0 and 100 are each valid grades). The teacher would like you to develop an application that computes the class average for a quiz.
Test-Driving the Class Average Application
Entering grades in the Class Average application.
• Entering quiz grades– Enter a grade into the Enter grade: TextBox
– Click Add Grade Button
– Grade added to ListBox– Focus given to TextBox
Test-Driving the Class Average Application
Figure 10.3 Class Average application after 10 grades have been input.
Ten quiz grades entered Disabled Add Grade Button
• Entering ten grades– Add Grade Button disabled
– Click Average Button to calculate average quiz grade
Test-Driving the Class Average Application
Figure 10.4 Displaying the class average.
Label displaying average
Click to calculate class average
• Calculating the average– Click the Average Button
– Average displayed in output Label– Add Grade Button enabled
Test-Driving the Class Average Application
Figure 10.5 Entering a new set of grades.
• Entering another set of grades– Old quiz grades removed from ListBox– New grade added to ListBox
Creating the Class Average Application
Event Control Event
Label all the application’s controls lblPrompt, lblGradeList, lblDescribeOutput
User enters grade txtInput
Retrieve grade entered by user in the Enter grade: TextBox
btnAdd Click
Display the grades lstGrades
Calculate the class average by reading ListBox values
btnAverage Click
Display the class average lblOutput
ACE table for the Class Average application.
Creating the Class Average Application
Class Average application’s Form in design view.
• Template application– Contains:
• ListBox
• Buttons• Output Label
18
Creating the Class Average Application
Clearing the ListBox and output Label after a calculation.
Clearing the grade list and class average
• Clearing ListBox and output Label– If output Label contains text
• Clear output Label of previous result
• Clear ListBox of previous quiz grades
19
Creating the Class Average Application
Adding the grade input to the ListBox and clearing the Enter grade: TextBox.
• Displaying grades– Add grade to ListBox– Clear TextBox txtInput
Adding a numeric grade to the ListBox and
clearing the user input from the TextBox
20
Creating the Class Average Application
Transferring the focus to the TextBox control.
Transferring the focusof the application to
the TextBox
• Transferring focus to a control– Makes application easier to user
– Use method Focus of that control
21
Creating the Class Average Application
• Accepting ten grades– Use method lstGrades.Items.Count to determine if ten grades
entered
– If ten grades entered:• Disable Add Grade Button
• Give focus to Average Button
22
Creating the Class Average Application
Application accepts only 10 grades.
Disabling the Add
grade Button and transferringthe focus to the
Average Button
23
Creating the Class Average Application
Do…Loop Until summing grades.
Using the Do…LoopUntil repetition
statement to sumgrades in the ListBox.
24
Creating the Class Average Application
• Adding quiz grades– Use a Do…Loop Until statement to retrieve ten grades
• Use lstGrades.Items.Item() to retrieve grade from ListBox
• Add grade to intTotal
• Increment counter
25
Creating the Class Average Application
• Displaying average– Calculate average of ten quiz grades
– Format and display average– Enable Add Grade Button– Transfer the focus to txtInput TextBox
26
Creating the Class Average Application
Displaying the result of the average calculation.
Calculating the class average,
enabling the Add
Grade Button, and transferring the
focus to the Enter
Grade: TextBox.
27
ClassAverage.vb(1 of 3)
1 Public Class FrmClassAverage2 Inherits System.Windows.Forms.Form
3 4 ' Windows Form Designer generated code
5 6 ' handles Add Grade Button’s Click event
7 Private Sub btnAdd_Click(ByVal sender As System.Object, _8 ByVal e As System.EventArgs) Handles btnAdd.Click
9 10 ' clear previous grades and calculation result
11 If lblOutput.Text <> ““ Then12 lblOutput.Text = ““
13 lstGrades.Items.Clear()14 End If
15 16 ' display grade in ListBox
17 lstGrades.Items.Add(Val(txtInput.Text))18 txtInput.Clear() ' clear grade from TextBox19 txtInput.Focus() ' transfer focus to TextBox
20 21 ' prohibit users from entering more than 10 grades
22 If lstGrades.Items.Count >= 10 Then23 btnAdd.Enabled = False ' disable Add Grade Button
24 btnAverage.Focus() ' transfer focus to Average
Button25 End If
Disabling the Add Grade Button and transferring the focus to the Average Button
28
ClassAverage.vb(2 of 3)
26 27 End Sub ' btnAdd_Click
28 29 ' handles Average Button’s Click event
30 Private Sub btnAverage_Click(ByVal sender As System.Object, _31 ByVal e As System.EventArgs) Handles btnAverage.Click
32 33 ' initialization phase
34 Dim intTotal As Integer = 035 Dim intGradeCounter As Integer = 0
36 Dim intGrade As Integer = 037 Dim dblAverage As Double = 0
38 39 ' sum grades in ListBox
40 Do41
42 ' read grade from ListBox43 intGrade = lstGrades.Items.Item(intGradeCounter)
44 intTotal += intGrade ' add grade to total45 intGradeCounter += 1 ' increment counter
46 Loop Until intGradeCounter >= 1047
Using a Do…Loop Until statement to calculate the class average
29
ClassAverage.vb(3 of 3)
48 dblAverage = intTotal / 10 ' calculate average49 lblOutput.Text = String.Format(“{0:F}”, dblAverage)
50 btnAdd.Enabled = True ' enable Add Grade Button 51 txtInput.Focus() ' reset focus to Enter grade: TextBox
52 End Sub ' btnAverage_Click53
54 End Class ' FrmClassAverage
Enabling the Add
Grade Button and transferring the focus to the Enter grade: TextBox