1 / 35

Chapter 5: Control Structures: Iteration

Chapter 5: Control Structures: Iteration. Visual Basic .NET Programming: From Problem Analysis to Program Design. Objectives. Explore the iteration structure Implement iteration using the Do While and Do Until statements Implement iteration using the For Next statement

azizi
Download Presentation

Chapter 5: Control Structures: Iteration

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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 Visual Basic .NET Programming: From Problem Analysis to Program Design

  3. 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 Visual Basic .NET Programming: From Problem Analysis to Program Design

  4. 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 Visual Basic .NET Programming: From Problem Analysis to Program Design

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

  6. Iteration Logic (continued) • Infinite loop • Never ends • Terminating condition never occurs • Pre-test loop • Terminating condition checked beforebody of loop executed • Statements in loop may not be executed • If terminating condition met Visual Basic .NET Programming: From Problem Analysis to Program Design

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

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

  9. Controlling Iteration • Counter control • Employs Integervariable to count number of times loop has executed • Variable called counter • Update counter each time loop executes Visual Basic .NET Programming: From Problem Analysis to Program Design

  10. 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 Visual Basic .NET Programming: From Problem Analysis to Program Design

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

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

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

  14. Implementing Iteration Using the Do While and Do UntilStatements • Iteration statements: • Do • Do While • Do Until • For Visual Basic .NET Programming: From Problem Analysis to Program Design

  15. 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 Visual Basic .NET Programming: From Problem Analysis to Program Design

  16. 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 … Visual Basic .NET Programming: From Problem Analysis to Program Design

  17. 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 … Visual Basic .NET Programming: From Problem Analysis to Program Design

  18. 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)) … Visual Basic .NET Programming: From Problem Analysis to Program Design

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

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

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

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

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

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

  25. 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: ") … Visual Basic .NET Programming: From Problem Analysis to Program Design

  26. 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)) Visual Basic .NET Programming: From Problem Analysis to Program Design

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

  28. 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()) Visual Basic .NET Programming: From Problem Analysis to Program Design

  29. 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 Visual Basic .NET Programming: From Problem Analysis to Program Design

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

  31. Programming Example: Loan Amortization Computation • Input • Loan amount • APR • Loan duration expressed in number of months Visual Basic .NET Programming: From Problem Analysis to Program Design

  32. Programming Example: Loan Amortization Computation (continued) • Output • Payment amount • Monthly interest and principal paid • Remaining loan balance • Total interest paid Visual Basic .NET Programming: From Problem Analysis to Program Design

  33. Programming Example: Loan Amortization Computation (continued) • Program purpose: • Compute and display amortization of a loan with monthly payments Visual Basic .NET Programming: From Problem Analysis to Program Design

  34. Summary • Use iteration structure to execute one or more statements repeatedly • Also called a loop • Loop logic types: • Pre-test • Post-test Visual Basic .NET Programming: From Problem Analysis to Program Design

  35. Summary (continued) • Control number of times a loop executes using: • Counter control • Sentinel control • Visual Basic .NET Loop structures: • Do While • Do Until • For • Iteration and selection structures can be nested Visual Basic .NET Programming: From Problem Analysis to Program Design

More Related