Professional Documents
Culture Documents
Solution Manual For Programming With Microsoft Visual Basic 2015 7th Edition
Solution Manual For Programming With Microsoft Visual Basic 2015 7th Edition
Solution Manual For Programming With Microsoft Visual Basic 2015 7th Edition
Chapter 6
The Repetition Structure
A Guide to this Instructor’s Manual:
We have designed this Instructor’s Manual to supplement and enhance your teaching
experience through classroom activities and a cohesive chapter summary.
This document is organized chronologically, using the same headings that you see in the
textbook. Under the headings you will find: lecture notes that summarize the section, Teaching
Tips, Class Discussion Topics, and Additional Projects and Resources. Pay special attention to
teaching tips and activities geared towards quizzing your students and enhancing their critical
thinking skills.
In addition to this Instructor’s Manual, our Instructor’s Resources also contain PowerPoint
Presentations, Test Banks, and other supplements to aid in your teaching experience.
At a Glance
• Objectives
• Teaching Tips
• Quick Quizzes
• Additional Projects
• Additional Resources
• Key Terms
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-2
Lecture Notes
Overview
Chapter 6 introduces repetition structures. Students learn how to create pretest and posttest
loops, and how to nest loop structures. The use of counter and accumulator variables is also
presented. This chapter contains three lessons:
• Lesson A covers how to code the repetition structure and how to depict it in pseudocode
and flowcharts.
• Lesson B introduces the use of the list box control and how to determine which item in
the list was selected and calculating a periodic payment.
• Lesson C covers the use of priming and update reads, nesting of repetition structures,
refreshing the screen, and delaying program execution.
Lesson A Objectives
After studying Lesson A, students should be able to:
• Differentiate between a looping condition and a loop exit condition
• Explain the difference between a pretest loop and a posttest loop
• Include pretest and posttest loops in pseudocode and in a flowchart
• Write a Do…Loop statement
• Stop an infinite loop
• Utilize counters and accumulators
• Abbreviate assignment statements using the arithmetic assignment operators
• Code a counter-controlled loop using the For…Next statement
Teaching Tips
Previewing the Monthly Payment Application
1. Use Figure 6-1 to review the sample application to be completed in this chapter.
2. Explain the concept of a list box and why it would be preferred over a text box.
Teaching Run the completed program to demonstrate the final result that will be achieved
Tip in this chapter.
3. Use Figure 6-2 to explain the difference between a looping condition and a loop exit
condition.
1. Use the problem specification in Figure 6-5 and the sample run in Figure 6-5 to
introduce the Projected Sales application.
2. Use Figure 6-7 to introduce the concepts of the pretest loop and posttest loop.
Teaching Explain to students that usage of pretest and posttest loops depends on when the
Tip condition should logically be tested.
3. Describe the two variations of syntax for the Do...Loop statement shown in Figure 6-8,
and discuss the example code.
Teaching Point out the availability of the Exit Do statement to provide an early exit from a
Tip Do...Loop statement.
Teaching
Remind students that conditions must evaluate to either True or False.
Tip
4. Use Figure 6-9 to describe the processing steps for the loop examples.
5. Use Figure 6-10 to describe the flowchart for the loop examples.
Teaching Ask students to describe the differences between the pretest and posttest loops in
Tip these examples.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-4
6. Point out the importance of understanding when the condition is tested and how the
condition expression is evaluated.
Teaching Ensure that students understand that a posttest loop will always be processed at
Tip least once because the condition is tested after the body of the loop has executed.
1. Use Figures 6-11 through 6-14 to review the steps needed to code the modified
application.
2. Define the terms overflow error, infinite loop and endless loop.
4. Use Figures 6-15 and 6-16 to illustrate updating counters and accumulators.
Teaching Explain that when a negative increment value is involved, programmers use the
Tip term “decrementing.”
1. Use Figure 6-17 to explain the need for a Start Over button and review the problem
specification, pseudocode, and a sample run for the Addition application.
2. Discuss the functions of the Multiline, ReadOnly, and ScrollBars properties of text
boxes.
3. Remind students of the need to use TryParse to convert the input to a number.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-5
4. Use Figure 6-18 to walk through the code in the loop of the Addition application.
1. Use Figure 6-21 to review the syntax and examples of the arithmetic assignment
operators.
3. Describe the syntax and examples of the For...Next statement shown in Figure 6-22.
4. Point out that you can declare the counter variable within the For...Next statement itself.
Teaching
You may wish to review the concept of block scope at this time.
Tip
5. Describe the effects of using a positive value and a negative value for the stepvalue
clause.
6. Use Figure 6-23 to walk through the processing steps for the example in the textbook.
Teaching When introducing loop structures, it is a good idea to use a simple example and
Tip “play computer,” showing the actions that occur within each iteration of the loop.
1. Use Figure 6-24 to review the problem specification for the Projected Sales application.
2. Use Figure 6-25 to describe the code for the btnCalc control’s Click event procedure in
the Monthly Payment Calculator application.
Teaching Remind students that the computer’s ability to process the same instructions
Tip repetitively is one of its most important features.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-6
1. Using Figure 6-27, point out the similarities and differences between the For…Next and
Do…Loop statements.
Lesson A Summary
1. Summarize the repetition structure (loop).
Quick Quiz 1
1. A For...Next statement creates a(n) ____________________ loop.
Answer: pretest
2. True or False: You can use a Do...Loop statement to create a posttest loop.
Answer: True
Lesson B Objectives
After studying Lesson B, students should be able to:
• Include a list box on a form
• Add items to a list box
• Clear the items from a list box
• Sort the items in a list box
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-7
Teaching Tips
Creating the Monthly Payment Application
1. Review the TOE chart for the application shown in Figure 6-33.
Teaching Point out that this application has been partially coded. Students will complete
Tip only a portion of the code.
Teaching
Compare and contrast the list box control to the combo box control.
Tip
Teaching
You can demonstrate multi-selection in Windows Explorer.
Tip
3. Use Figure 6-34 to describe the correct location and size of the ListBox.
4. Use Figure 6-35 to describe the correct sequence for the TabIndex property.
1. Introduce the concepts of a collection and the Items collection of a list box.
Teaching
Stress the concept of zero-relative counting used with indexes.
Tip
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-8
3. Use Figure 6-36 to introduce the Add method of the Items collection, and describe its
syntax.
1. Use Figure 6-37 to introduce the Clear method and describe its syntax.
1. Introduce the Sorted property of the list box and explain its effects using Figure 6-38
as an example.
2. Point out that a list box will be sorted in dictionary order, not numerical order.
Numeric values can be forced to sort in numerical order by ensuring that all
Teaching
values have the same number of digits. Use left padding with zeros to enforce
Tip
this.
Teaching Remind students that IntelliSense will assist in the creation of correct code for
Tip the Add method of the Items collection.
1. Point out that like the combo box, the list box has the SelectedItem property and
SelectedIndex property that provide the same type of information.
2. Review the examples for using the SelectedItem and SelectedIndex properties in Figure
6-41.
3. Introduce the concept of the default list box item, and point out that the default list box
item can be selected using code as shown in Figure 6-42.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-9
Teaching
Setting a default item is a Windows standard that users expect in an application.
Tip
4. Describe the results of running this program shown in Figure 6-43.
5. Review the guidelines for the default list box item in the GUI Design Tip box.
1. Review the pseudocode for the btnCalc_Click procedure shown in Figure 6-44.
Lesson B Summary
1. Review how to add a list box control to a form.
2. Review the steps involved in allowing a user to multi-select items in a list box.
4. Summarize the use of the Sorted property for determining the order in which items will
appear in a list box.
5. Review the use of the SelectedItem property or SelectedIndex property to obtain the
value of the currently selected item.
Quick Quiz 2
1. Which property of a list box will provide the position of the selected item in the list?
Answer: SelectedIndex
2. When using a list box control, the event named ____________________ will occur
when the user makes a selection.
Answer: SelectedValueChanged
SelectedIndexChanged
4. True or False: The list box only supports allowing a user to make a single choice.
Answer: False
Lesson C Objectives
After studying Lesson C, students should be able to:
• Explain the purpose of the priming and update reads
• Nest repetition structures
• Refresh the screen
• Delay program execution
Teaching Tips
The Electric Bill Application
1. Use Figure 6-55 to introduce the problem specification, pseudocode, and sample run for
the Electric Bill application.
2. Explain the need for a priming read and the difference between a priming read and an
update read.
3. Review the code in the btnCalc_Click procedure in Figures 6-56 and 6-57.
4. Review the completed code for the btnCalc_Click procedure in Figure 6-58 and the
sample run in Figure 6-59.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-11
3. Review the interface shown in Figure 6-61 for the Clock application.
2. Introduce the Sleep method for pausing the program’s execution. Explain the
millisecond time unit.
3. Review the code for the Refresh and Sleep methods shown in Figure 6-62.
Students may have a tough time understanding the logic behind the workings of a
Teaching clock. The application displays minutes from 0 to 2 and seconds from 0 to 5.
Tip This will add to the confusion. Even though they will code for the simple
version, you should explain the concept using 60 seconds and 60 minutes.
1. Describe the nested repetition structures in the code for the procedure shown in Figure
6-65.
2. Describe the role played by the inner loop and the role played by the outer loop as
shown in Figure 6-66.
2. Show the sample output in Figures 6-67 and 6-68 as well as the modifications to the
nested loop shown in Figure 6-69.
Lesson C Summary
1. Review the process of nesting repetition structures.
Quick Quiz 3
1. True or False: Repetition structures cannot be nested within other types of structures
such as If...Then...Else statements.
Answer: False
2. The (inner/outer) ____________________ loop will be processed more times than the
(inner/outer) ____________________ loop.
Answer: inner, outer
2. Consider the SelectionModeProperty of the list box control. Although it can allow the
user to select multiple items at the same time, does this change how the program’s code
must be written?
Additional Projects
1. Have students create an interface that allows a user to select a state code from a list box
and use the select case statement to print out the state name. You can limit the number
of states in the list box to make the assignment coding shorter.
2. Ask students to experiment with the sorted property of the list box control by adding
only numeric values to the list. How does dictionary sort affect the order when the
Sorted property is set to True?
Additional Resources
1. An Introduction to Loops:
www.homeandlearn.co.uk/NET/nets3p1.html
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Programming with Microsoft Visual Basic 2015, Seventh Edition 6-13
Key Terms
➢ Accumulator—a numeric variable used for accumulating (adding together) something
➢ Add method—the Items collection’s method used to add items to a list box
➢ Arithmetic assignment operators—composed of an arithmetic operator followed by
the assignment operator; used to abbreviate an assignment statement that has the
following format, in which variableName is the name of the same variable:
variableName = variableName arithmeticOperator value
➢ Collection—a group of individual objects treated as one unit
➢ Counter—a numeric variable used for counting something
➢ Counter-controlled loop—a loop whose processing is controlled by a counter; the loop
body will be processed a precise number of times
➢ Decrementing—adding increments of a negative number
➢ Default list box item—the item automatically selected in a list box when the interface
appears on the screen
➢ Do...Loop statement—a Visual Basic statement that can be used to code both pretest
loops and posttest loops
➢ Endless loop—a loop whose instructions are processed indefinitely; also called an
infinite loop
➢ Financial.Pmt method—used to calculate the periodic payment on either a loan or an
investment
➢ For...Next statement—a Visual Basic statement that is used to code a specific type of
pretest loop, called a counter-controlled loop
➢ Incrementing—another name for updating
➢ Infinite loop—another name for an endless loop
➢ Initializing—the process of assigning a beginning value to a memory location, such as
a counter or an accumulator variable
➢ Items collection—the collection composed of the items in a list box
➢ List box—a control used to display a list of choices from which the user can select zero
choices, one choice, or multiple choices
➢ Loop—another name for the repetition structure
➢ Loop body—the instructions within a loop
➢ Loop exit condition—the requirement that must be met for the computer to stop
processing the loop body instructions
➢ Looping condition—the requirement that must be met for the computer to continue
processing the loop body instructions
➢ Multiline property—determines whether a text box can accept and display only one
line of text or multiple lines of text
➢ Posttest loop—a loop whose condition is evaluated after the instructions in its loop
body are processed
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Solution Manual for Programming with Microsoft Visual Basic 2015, 7th Edition
➢ Pretest loop—a loop whose condition is evaluated before the instructions in its loop
body are processed
➢ Priming read—the input instruction that appears above the loop that it controls; used to
get the first input item from the user
➢ ReadOnly property—controls whether the user is allowed to change the contents of a
text box during run time
➢ Real numbers—numbers with a decimal place
➢ Refresh method—can be used to refresh (redraw) a form
➢ Repetition structure—the control structure used to repeatedly process one or more
program instructions; also called a loop
➢ ScrollBars property—a property of a text box; specifies whether the text box has
scroll bars
➢ SelectedIndex property—stores the index of the item selected in a list box
➢ SelectedIndexChanged event—occurs when an item is selected in a list box
➢ SelectedItem property—stores the value of the item selected in a list box
➢ SelectedValueChanged event—occurs when an item is selected in a list box
➢ SelectionMode property—determines the number of items that can be selected in a list
box
➢ Sleep method—can be used to delay program execution
➢ Sorted property—specifies whether the list box items should appear in the order they
are entered or in sorted order
➢ Update read—the input instruction that appears within a loop and is associated with
the priming read
➢ Updating—the process of adding a number to the value stored in a counter or
accumulator variable; also called incrementing
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.