T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 10 Class Average Application Introducing the Do...Loop While and Do...Loop Until.

Slides:



Advertisements
Similar presentations
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
Advertisements

Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Shipping Hub Application Introducing Generic Collections, LINQ, For Each...Next.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 12: How Long Can This Go On?
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Wage Calculator Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Fund Raiser Application Introducing Scope, Pass-by-Reference and Option Strict.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 8 Dental Payment Application Introducing CheckBox es and Message Dialogs.
 2009 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 1B) UTPA – Fall 2011.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
Visual Basic Programming
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Screen Scraping Application Introducing String Processing.
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Microwave Oven Application Building Your Own Classes and Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Shipping Hub Application.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Introduction to Problem Solving and Control Statements.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Introduction to Computers and Programming Lecture 7:
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 7 Wage Calculator Application Introducing Algorithms, Pseudocode and Program Control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 11.1 Test-Driving the Interest Calculator Application 11.2 Essentials of Counter-Controlled Repetition.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
while Repetition Structure
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
Tutorial 9 - Car Payment Calculator Application Introducing the while Repetition Statement Outline 9.1 Test-Driving the Car Payment Calculator Application.
Problem Solving and Control Statements: Part 2
Week 4 – Repetition Structures / Loops
Tutorial 12 – Security Panel Application Introducing the Select Case Multiple-Selection Statement Outline Test-Driving the Security Panel Application.
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Presentation transcript:

T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until Repetition Statements

 2009 Pearson Education, Inc. All rights reserved. 2 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

 2009 Pearson Education, Inc. All rights reserved. 3 In this tutorial you will learn: ■Use the Do...Loop While statement. ■Use the Do...Loop Until statement. ■Further understand counter-controlled repetition. ■Transfer the focus to a control. ■Enable and disable Button s. Objectives

Application Requirements  2009 Pearson Education, Inc. All rights reserved Test-Driving the Class Average Application A teacher regularly gives quizzes to a class of 10 students. The grades on these quizzes are integers in the range from 0 to 100 (0 and 100 are both valid grades). The teacher would like you to develop an application that computes the class average for one quiz.

 2009 Pearson Education, Inc. All rights reserved. 5 Test-Driving the Class Average Application ■Run the completed application (Fig. 10.1). Figure 10.1 | Class Average application’s Form in run mode. Output Label

 2009 Pearson Education, Inc. All rights reserved. 6 Test-Driving the Class Average Application (Cont.) ■After you click the Add Grade Button, the cursor appears in the Enter grade: TextBox (Fig. 10.2). –When a control is selected, it is said to have the focus of the application. –Transferring the focus tells the user what information the application expects next. Figure 10.2 | Entering grades in the Class Average application.

 2009 Pearson Education, Inc. All rights reserved. 7 Test-Driving the Class Average Application (Cont.) ■Enter nine other grades between 0 and 100. ■Note that the Add Grade Button is disabled once you have entered 10 grades (Fig. 10.3). Figure 10.3 | Class Average application after 10 grades have been input. Disabled Add Grade Button Ten quiz grades entered

 2009 Pearson Education, Inc. All rights reserved. 8 Test-Driving the Class Average Application (Cont.) ■Click the Average Button to calculate the average of the 10 quizzes (Fig. 10.4). Figure 10.4 | Displaying the class average. Label displaying average Click to calculate class average

 2009 Pearson Education, Inc. All rights reserved. 9 Test-Driving the Class Average Application (Cont.) ■You can calculate the class average for another set of 10 grades without restarting the application. –Enter a grade in the TextBox, and click the Add Grade Button. –Note that the Grade list: ListBox and the Class average: field are cleared when you start entering another set of grades (Fig. 10.5). Figure 10.5 | Entering a new set of grades.

 2009 Pearson Education, Inc. All rights reserved. 10 ■ Do...Loop While repetition statement is similar to the Do...While Loop statement, except that the loop-termination condition is tested after the loop body is performed. ■Consider the example of packing a suitcase: –You place an item in the suitcase, then determine whether the suitcase is full. –As long as the suitcase is not full, you continue to put items in the suitcase Do...Loop While Repetition Statement

 2009 Pearson Education, Inc. All rights reserved. 11 Common Programming Error An infinite loop occurs when the loop-continuation condition in a Do...Loop While statement never becomes False.

 2009 Pearson Education, Inc. All rights reserved. 12 ■The following application segment displays the numbers 1 through 3 in a ListBox : Dim counter As Integer = 1 Do displayListBox.Items.Add(counter ) counter += 1 Loop While counter <= Do...Loop While Repetition Statement (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 13 ■Including an incorrect relational operator or an incorrect final value for a loop counter can cause off-by-one errors. ■Figure 10.6 illustrates the UML activity diagram for the general Do...Loop While statement. Figure 10.6 | Do...Loop While repetition statement UML activity diagram Do...Loop While Repetition Statement (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 14 Error-Prevention Tip Including a final value in the condition of a repetition statement (and choosing the appropriate relational operator) can reduce the occurrence of off-by-one errors. For example, in a Do While...Loop statement used to print the values 1–10, the loop-continuation condition should be counter <= 10, rather than counter < 10 (which is an off-by-one error) or counter < 11 (which is correct, but less clear).

 2009 Pearson Education, Inc. All rights reserved. 15 ■The Do...Loop Until statement is similar to the Do...Until Loop statement, except that the loop- termination condition is tested after the loop body is performed. ■Imagine that you place an item in the suitcase, then determine whether the suitcase is full. As long as the condition “the suitcase is full” is False, you continue to put items into the suitcase Do...Loop Until Repetition Statement

 2009 Pearson Education, Inc. All rights reserved. 16 Common Programming Error An infinite loop occurs when the loop-termination condition in a Do...Loop Until statement never becomes True.

 2009 Pearson Education, Inc. All rights reserved. 17 ■This application segment displays the numbers 1 through 3 in a ListBox : Dim counter As Integer = 1 Do displayListBox.Items.Add(counter ) counter += 1 Loop Until counter > Do...Loop Until Repetition Statement (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 18 Figure 10.7 | Do...Loop Until repetition statement UML activity diagram Do...Loop Until Repetition Statement (Cont.) ■This UML diagram indicates the exact same guard conditions as detailed in Fig

 2009 Pearson Education, Inc. All rights reserved. 19 When the user clicks the Add Grade Button If an average has already been calculated for a set of grades Clear the output Label and the ListBox Retrieve grade entered by user in the Enter grade:TextBox Display the grade in the ListBox Clear the Enter grade: TextBox Transfer focus to the Enter grade: TextBox If the user has entered 10 grades Disable the Add Grade Button Transfer focus to the Average Button 10.4 Creating the Class Average Application

 2009 Pearson Education, Inc. All rights reserved. 20 When the user clicks the Average Button Set total to zero Set grade counter to zero Do Read the next grade in the ListBox Add the grade to the total Add one to the grade counter Loop While the grade counter is less than 10 Calculate the class average by dividing the total by 10 Display the class average Enable the Add Grade Button Transfer focus to the Enter grade: TextBox 10.4 Creating the Class Average Application (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 21 ■Use an ACE table to convert pseudocode to Visual Basic (Fig. 10.8). Figure 10.8 | ACE table for the Class Average application. (Part 1 of 3.) Action/Control/Event Table for the Class Average Application

 2009 Pearson Education, Inc. All rights reserved. 22 Figure 10.8 | ACE table for the Class Average application. (Part 2 of 3.) Action/Control/Event Table for the Class Average Application (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 23 Figure 10.8 | ACE table for the Class Average application. (Part 3 of 3.) Action/Control/Event Table for the Class Average Application (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 24 Entering Grades in the Class Average Application ■Open the template application (Fig. 10.9). Figure 10.9 | Class Average application’s Form in Design view.

 2009 Pearson Education, Inc. All rights reserved. 25 ■Double click the Button labeled Add Grade to create its event handler (Fig ). ■The program tests whether averageResultLabel displays any text by comparing the Text property’s value to the empty string. Entering Grades in the Class Average Application (Cont.) Figure | Clearing the output Label and ListBox after a calculation. Clearing the grade list and class average

 2009 Pearson Education, Inc. All rights reserved. 26 ■Line 13 (Fig ) Add s the grade entered in gradeTextBox to gradesListBox ’s Items property. The grade is displayed in the ListBox. ■Method Clear deletes the grade from the TextBox so that the next grade can be entered. Figure | Add ing 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 Entering Grades in the Class Average Application (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 27 Transferring the Focus to a Control and Disabling a Button ■Calling gradeTextBox ’s Focus method places the cursor in the TextBox for the next grade input (Fig ). ■This process is called transferring the focus. Figure | Transferring the focus to the TextBox control. Transferring the focus of the application to the TextBox

 2009 Pearson Education, Inc. All rights reserved. 28 GUI Design Tip Transfer the focus to the control that should be used next.

 2009 Pearson Education, Inc. All rights reserved. 29 ■Your application should accept exactly 10 grades. – Items ’s Count property returns the number of items displayed in the Grade list: ListBox. –If 10 grades have been entered, addButton ’s Enabled property is set to False (Fig ). Figure | Application accepts only 10 grades. Disabling the Add grade Button and transferring the focus to the Average Button ■After 10 grades have been entered, transfer the focus to the Average Button. Transferring the Focus to a Control and Disabling a Button (Cont.)

 2009 Pearson Education, Inc. All rights reserved. 30 GUI Design Tip Disable Button s when their function should not be available to users.

 2009 Pearson Education, Inc. All rights reserved. 31 Common Programming Error A control must be enabled in order to receive focus.

 2009 Pearson Education, Inc. All rights reserved. 32 ■Use the Integer total (Figure ) to calculate the sum of the 10 grades. ■The result of the averaging calculation can be a floating-point value; therefore, you declare a Double variable to store the class average. Calculating the Class Average Figure | Initialization phase of class-average calculation. Initializing variables

 2009 Pearson Education, Inc. All rights reserved. 33 ■The Do...Loop Until statement in Figure sums the grades that it reads from the ListBox. ■The statement should iterate until the value of gradeCounter is greater than or equal to 10. ■The items in a ListBox are accessed by their position number, starting from position number 0. Calculating the Class Average (Cont.) Figure | Do...Loop Until summing grades. Using the Do...Loop Until repetition statement to sum grades in the ListBox

 2009 Pearson Education, Inc. All rights reserved. 34 ■The F format specifier ( Fig ) displays average in floating-point format. ■After the average is displayed, the application resets, and another list of grades can be entered. Calculating the Class Average (Cont.) Figure | 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

 2009 Pearson Education, Inc. All rights reserved. 35 GUI Design Tip Enable a disabled Button when its function should be available to the user once again.

 2009 Pearson Education, Inc. All rights reserved. 36 ■Figure displays the source code for the application. Clearing gradeTextBox Outline (1 of 3 ) Transferring focus to gradeTextBox

 2009 Pearson Education, Inc. All rights reserved. 37 Outline (2 of 3 ) Disabling the Add Grade Button and transferring the focus to the Average Button

 2009 Pearson Education, Inc. All rights reserved. 38 Outline (3 of 3 ) Accessing a grade in the ListBox via the Items property Enabling the Add Grade Button and transferring the focus to the Enter grade : TextBox