Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.

Similar presentations


Presentation on theme: "Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design."— Presentation transcript:

1 Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design

2 Visual Basic.NET Programming: From Problem Analysis to Program Design2 Objectives Explore the iteration structure Implement iteration using the Do While and Do Until statements Implement iteration using the For Next statement Create nested structures

3 Visual Basic.NET Programming: From Problem Analysis to Program Design3 Exploring the Iteration Structure Iteration structure –Execute one or more statements repeatedly –Often called a loop –Write a set of statements Repeatedly execute them until terminating condition is reached

4 Visual Basic.NET Programming: From Problem Analysis to Program Design4 Iteration Logic Basic iteration logic: –First test to see whether loop should terminate –If loop does not terminate Statement or statements in loop body executed –Control returns to beginning

5 Visual Basic.NET Programming: From Problem Analysis to Program Design5

6 6 Iteration Logic (continued) Infinite loop –Never ends –Terminating condition never occurs Pre-test loop –Terminating condition checked before body of loop executed –Statements in loop may not be executed If terminating condition met

7 Visual Basic.NET Programming: From Problem Analysis to Program Design7 Iteration Logic (continued) Post-test loop –Terminating condition checked after body of loop executed –Statements in loop executed at least once Regardless of terminating condition

8 Visual Basic.NET Programming: From Problem Analysis to Program Design8

9 9 Controlling Iteration Counter control –Employs Integer variable to count number of times loop has executed –Variable called counter –Update counter each time loop executes

10 Visual Basic.NET Programming: From Problem Analysis to Program Design10 Controlling Iteration (continued) Counter control (continued) –When counter reaches predetermined value Loop terminates –Can increment or decrement counter by any value that suits program logic

11 Visual Basic.NET Programming: From Problem Analysis to Program Design11

12 Visual Basic.NET Programming: From Problem Analysis to Program Design12 Controlling Iteration (continued) Sentinel-control logic –Checks user input for specific value –Terminates when value detected –Value called sentinel Should be unique value

13 Visual Basic.NET Programming: From Problem Analysis to Program Design13

14 Visual Basic.NET Programming: From Problem Analysis to Program Design14 Implementing Iteration Using the Do While and Do Until Statements Iteration statements: –Do Do While Do Until –For

15 Visual Basic.NET Programming: From Problem Analysis to Program Design15 Writing Loops Using Do While Pre-test loop –Terminating condition is tested at beginning of loop Syntax: Do While expression Statements Loop Loop continues to execute as long as expression is true

16 Visual Basic.NET Programming: From Problem Analysis to Program Design16 Example 5-3: Computing an exam average using a counter- controlled Do While loop 1. ' define variables 2. Dim sum, average AsDouble 3. Dim numberOfExams As Integer = 5 4. Dim counter As Integer = 1 …

17 Visual Basic.NET Programming: From Problem Analysis to Program Design17 Example 5-3: Computing an exam average using a counter- controlled Do While loop … 5. ' begin loop 6. Do While (counter <= numberOfExams) 7. Console.WriteLine(“Enter an Exam Score: “) 8. sum = sum + Convert.ToDouble(Console.ReadLine()) 9. counter += 1 ' count the number of iterations 10. Loop …

18 Visual Basic.NET Programming: From Problem Analysis to Program Design18 Example 5-3: Computing an exam average using a counter- controlled Do While loop … 11. ' compute & display the average 12. average = sum / numberOfExams 13. Console.WriteLine(“The average is: “ & Math.Round(average, 1)) …

19 Visual Basic.NET Programming: From Problem Analysis to Program Design19

20 Visual Basic.NET Programming: From Problem Analysis to Program Design20 Writing Loops Using Do Until Executes until expression is true –Contrast to Do While Syntax Do Until expression Statement(s) Loop

21 Visual Basic.NET Programming: From Problem Analysis to Program Design21

22 Visual Basic.NET Programming: From Problem Analysis to Program Design22 Implementing Iteration Using the For Next Statement For Next loops provide only: –Counter-controlled loop –Pre-test loop Initializes counter variable Automatically increments counter –Simplifies and shortens code

23 Visual Basic.NET Programming: From Problem Analysis to Program Design23

24 Visual Basic.NET Programming: From Problem Analysis to Program Design24

25 Visual Basic.NET Programming: From Problem Analysis to Program Design25 Example 5-9: Computing an Exam Average Using a For Next Loop 1. ' define variables 2. Dim sum, average As Double 3. Dim numberOfExams As Integer = 5 4. Dim counter As Integer 5. ' begin loop 6. For counter = 1 To 5 Step 1 7. Console.WriteLine("Enter an Exam Score: ") …

26 Visual Basic.NET Programming: From Problem Analysis to Program Design26 Example 5-9: Computing an Exam Average Using a For Next Loop (continued) 8. sum = sum + Convert.ToDouble(Console.ReadLine()) 9. Next 10. ' compute & display the average 11. average = sum / numberOfExams 12. Console.WriteLine("The average is: " & Math.Round(average, 1)) Look at ex. 5-10 and 5-11 to see the difference between For Next and Do while

27 Visual Basic.NET Programming: From Problem Analysis to Program Design27 Creating Nested Structures Nested loop –One iteration structure placed inside another Can place iteration inside selection –And vice versa

28 Visual Basic.NET Programming: From Problem Analysis to Program Design28 Example 5-12: Computing an Exam Average Using a Nested For Next Loop (excerpt) For studentCounter = 1 To numberOfStudents 8. sum = 0 ' reinitialize sum 9. ' begin inner loop for exams 10. For examCounter = 1 To numberOfExams 11. Console.WriteLine (“Enter an Exam Score: “) 12. score = Convert.ToDouble (Console.ReadLine())

29 Visual Basic.NET Programming: From Problem Analysis to Program Design29 Example 5-12: Computing an Exam Average Using a Nested For Next Loop (continued) 13. sum += score 14. Next ' end of inner loop 15. average = sum / numberOfExams 16. Console.WriteLine (“The average is: “ & Math.Round(average, 1)) 17. Next ' end of outer loop

30 Visual Basic.NET Programming: From Problem Analysis to Program Design30

31 Visual Basic.NET Programming: From Problem Analysis to Program Design31 Summary Use iteration structure to execute one or more statements repeatedly –Also called a loop Loop logic types: –Pre-test –Post-test Control number of times a loop executes using: –Counter control –Sentinel control

32 Visual Basic.NET Programming: From Problem Analysis to Program Design32 Summary (continued) Visual Basic.NET Loop structures: –Do While –Do Until –For Iteration and selection structures can be nested For in-class practice: Modify Ex. 5-12 to calculate average grade for the whole class; Review ex. 5-13: PEx. 3 (without re-prompt), 4 (without re-prompt) Homework: p. 172, PEx. 6; Due: 2/29/08


Download ppt "Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design."

Similar presentations


Ads by Google