370 likes | 672 Views
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
E N D
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 • Create nested structures Visual Basic .NET Programming: From Problem Analysis to Program Design
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
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
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
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
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
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
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
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
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
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
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
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
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
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
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