Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline 10.1 Test-Driving the Class Average Application 10.2 Do…Loop While Repetition Statement 10.3 Do…Loop Until Repetition Statement 10.4 Creating the Class Average Application 10.5 Wrap-Up
In this tutorial, you will learn to: Objectives In this tutorial, you will learn to: Use the Do…Loop While statement. Use the Do…Loop Until statement. Understand counter-controlled repetition. Transfer the focus to a control. Enable and disable Buttons.
10.1 Test-Driving the Class Average Application
10.1 Test-Driving the Class Average Application Figure 10.1 Class Average application’s Form in run mode. Output Label The Class Average application Output Label displays quiz average to user
10.1 Test-Driving the Class Average Application Figure 10.2 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
10.1 Test-Driving the Class Average Application Figure 10.3 Class Average application after 10 grades have been input. Disabled Add Grade Button Ten quiz grades entered Entering ten grades Add Grade Button disabled Click Average Button to calculate average quiz grade
10.1 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
10.1 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
10.2 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
10.2 Do…Loop While Repetition Statement Figure 10.6 Do…Loop While repetition statement UML activity diagram. Corresponding VB .NET Statements: lstDisplay.Items.Add(intCounter) action state intCounter += 1 [intCounter <= 3] decision loop- [intCounter > 3] continuation condition Do…Loop While repetition statement Body of the loop executes first Loop-continuation condition checked If False, loop exits If True, loop executes again
10.3 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
10.3 Do…Loop Until Repetition Statement Figure 10.7 Do…Loop Until repetition statement UML activity diagram. Corresponding VB .NET Statements: lstDisplay.Items.Add(intCounter) action state intCounter += 1 [intCounter <= 3] Decision [intCounter > 3] Do…Loop Until repetition statement Body of the loop executes Loop-continuation condition checked If False, loop executes again If True, loop exits
10.4 Creating the Class Average Application
10.4 Creating the Class Average Application
10.4 Creating the Class Average Application Figure 10.9 Class Average application’s Form in design view. Template application Contains: ListBox Buttons Output Label
10.4 Creating the Class Average Application Figure 10.10 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
10.4 Creating the Class Average Application Figure 10.11 Adding the grade input to the ListBox and clearing the Enter grade: TextBox. Adding a numeric grade to the ListBox and clearing the user input from the TextBox Displaying grades Add grade to ListBox Clear TextBox txtInput
10.4 Creating the Class Average Application Figure 10.12 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 use Use method Focus of that control
10.4 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
10.4 Creating the Class Average Application Figure 10.13 Application accepts only 10 grades. Disabling the Add grade Button and transferring the focus to the Average Button
10.4 Creating the Class Average Application Initializing event handler variables intTotal stores sum of all quiz grades intGradeCounter stores number of grades entered intGrade stores current grade entered dblAverage stores average of quiz grades
10.4 Creating the Class Average Application Figure 10.14 Initialization phase of class-average calculation. Initializing variables
10.4 Creating the Class Average Application Figure 10.15 Do…Loop Until summing grades. Using the Do…Loop Until repetition statement to sum grades in the ListBox.
10.4 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
10.4 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
10.4 Creating the Class Average Application Figure 10.16 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.
ClassAverage.vb (1 of 3) Disabling the Add Grade Button and transferring the focus to the Average Button
ClassAverage.vb (2 of 3) Using a Do…Loop Until statement to calculate the class average
Enabling the Add Grade Button and transferring the focus to the Enter grade: TextBox ClassAverage.vb (3 of 3)