The document introduces the Do While and Do Until loop repetition statements in Visual Basic .NET. It provides code examples and UML diagrams to illustrate how each loop works. It also describes using Do While and Do Until loops to implement the logic for a Class Average application, which allows a teacher to enter grades, calculate the average, and display the result. Key aspects covered include clearing old data, restricting to 10 grades, summing the grades using a Do Until loop, and re-enabling the interface after calculating the average.
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
Do While and Do Until Loops in VB.NET Class Average App
1. Introducing the Do While…Loop
and Do Until…Loop Repetition
Statements
Introducing the Do…Loop While
and Do…Loop Until Repetition
Statements
2. 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
3. Do While…Loop Repetition
Statement
[intProduct <= 50]
[intProduct > 50]
triple product value
merge
decision
Corresponding Visual Basic .NET
statement:
intProduct *= 3
Figure 9.4 Do While Loop repetition statement UML activity diagram.
4. 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
5. Do Until…Loop Repetition
Statement
[intProduct <= 50]
[intProduct > 50]
triple product value
merge
decision
Corresponding Visual Basic .NET
statement:
intProduct *= 3
Figure 9.5 Do Until Loop repetition statement UML activity diagram
6. 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
7. 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
8. 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
9. Do…Loop Until Repetition
Statement
Decision
action state
[intCounter <= 3]
[intCounter > 3]
Corresponding VB .NET Statements:
lstDisplay.Items.Add(intCounter)
intCounter += 1
Loop-
termination
condition
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
10. Introducing the Do…Loop While
and Do…Loop Until Repetition
Statements
Class Average Application
11. 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.
12. 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
13. 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
14. 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
15. 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
16. 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.
17. Creating the Class Average
Application
Class Average application’s Form in design view.
• Template application
– Contains:
• ListBox
• Buttons
• Output Label
18. 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. 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. 20
Creating the Class Average
Application
Transferring the focus to the TextBox control.
Transferring the focus
of the application to
the TextBox
• Transferring focus to a control
– Makes application easier to user
– Use method Focus of that control
21. 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. 22
Creating the Class Average
Application
Application accepts only 10 grades.
Disabling the Add
grade Button
and transferring
the focus to the
Average Button
23. 23
Creating the Class Average
Application
Do…Loop Until summing grades.
Using the Do…Loop
Until repetition
statement to sum
grades in the ListBox.
24. 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. 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. 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. 27
ClassAverage.vb
(1 of 3)
1 Public Class FrmClassAverage
2 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 <> ““ Then
12 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 TextBox
19 txtInput.Focus() ' transfer focus to TextBox
20
21 ' prohibit users from entering more than 10 grades
22 If lstGrades.Items.Count >= 10 Then
23 btnAdd.Enabled = False ' disable Add Grade Button
24 btnAverage.Focus() ' transfer focus to Average
Button
25 End If
Disabling the Add Grade Button and
transferring the focus to the Average
Button
28. 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 = 0
35 Dim intGradeCounter As Integer = 0
36 Dim intGrade As Integer = 0
37 Dim dblAverage As Double = 0
38
39 ' sum grades in ListBox
40 Do
41
42 ' read grade from ListBox
43 intGrade = lstGrades.Items.Item(intGradeCounter)
44 intTotal += intGrade ' add grade to total
45 intGradeCounter += 1 ' increment counter
46 Loop Until intGradeCounter >= 10
47
Using a Do…Loop Until
statement to calculate the class
average
29. 29
ClassAverage.vb
(3 of 3)
48 dblAverage = intTotal / 10 ' calculate average
49 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_Click
53
54 End Class ' FrmClassAverage
Enabling the Add
Grade Button and
transferring the focus
to the Enter grade:
TextBox