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
• Create nested structures
Visual Basic .NET Programming: From Problem Analysis to Program Design
2
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
3
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
4
Visual Basic .NET Programming: From Problem Analysis to Program Design
5
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
6
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
7
Visual Basic .NET Programming: From Problem Analysis to Program Design
8
Controlling Iteration
• Counter control
– Employs Integer variable 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
9
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
10
Visual Basic .NET Programming: From Problem Analysis to Program Design
11
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
12
Visual Basic .NET Programming: From Problem Analysis to Program Design
13
Implementing Iteration Using
the Do While and Do Until
Statements
• Iteration statements:
– Do
• Do While
• Do Until
– For
Visual Basic .NET Programming: From Problem Analysis to Program Design
14
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
15
Example 5-3: Computing an
exam average using a countercontrolled 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
16
Example 5-3: Computing an
exam average using a countercontrolled 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
17
Example 5-3: Computing an
exam average using a countercontrolled 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
18
Visual Basic .NET Programming: From Problem Analysis to Program Design
19
Writing Loops Using Do Until
• Executes until expression is true
– Contrast to Do While
• Syntax
Do Until expression
Statement(s)
Loop
Visual Basic .NET Programming: From Problem Analysis to Program Design
20
Visual Basic .NET Programming: From Problem Analysis to Program Design
21
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
Visual Basic .NET Programming: From Problem Analysis to Program Design
22
Visual Basic .NET Programming: From Problem Analysis to Program Design
23
Visual Basic .NET Programming: From Problem Analysis to Program Design
24
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
25
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
26
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
27
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
28
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
29
Visual Basic .NET Programming: From Problem Analysis to Program Design
30
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
31
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
32
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
33
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
34
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
35