repeating program instructions chapter microsoft visual basic.net: reloaded 1

39
Repeating Program Instructions Chapter Microsoft Visual Basic .NET: Reloaded 1

Upload: samantha-greene

Post on 22-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

Repeating Program Instructions

Chapter Microsoft Visual Basic .NET: Reloaded

1

Page 2: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

2Microsoft Visual Basic .NET: Reloaded

Objectives

• Include the repetition structure in pseudocode and in a flowchart

• Write a For…Next statement• Calculate a periodic payment using the Pmt

function• Write a Do…Loop statement• Initialize and update counters and

accumulators• Display a dialog box using the InputBox

function

Page 3: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

3Microsoft Visual Basic .NET: Reloaded

The Repetition Structure

• Repetition Structure (also known as a loop)• Repeatedly processes a set of instructions

until a condition is met

• Pretest loop:• Evaluation occurs before instructions within loop

• PostTest loop:• Evaluation occurs after the instructions within

loop are processed • Instructions will always be processed at least

once

Page 4: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

4Microsoft Visual Basic .NET: Reloaded

The For…Next Statement

• Use to code a loop to process instructions a precise number of times

• For…Next is a pretest loop• StartValue (starting value of loop counter)

• EndValue (ending value of loop counter)

• Stepvalue (amount of change of loop counter)• Default stepvalue is a positive 1• Can be negative• Designate with Step keyword

• Example Step -2• Subtracts 2 from the loop counter

Page 5: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

5Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 6: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

6Microsoft Visual Basic .NET: Reloaded

The For…Next Statement (continued)

Page 7: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

7Microsoft Visual Basic .NET: Reloaded

The For…Next Statement (continued)

Page 8: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

8Microsoft Visual Basic .NET: Reloaded

The Pmt Function

• Use to calculate a periodic payment on a loan or investment

• Contains 5 arguments

• Rate, Nper, PV are required

• FV, Due are optional

• Negation operator reverses the sign of a number

• -Pmt(.05,3,9000)

Page 9: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

9Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 10: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

10Microsoft Visual Basic .NET: Reloaded

The Payment Calculator Application

Page 11: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

11Microsoft Visual Basic .NET: Reloaded

The Payment Calculator Application (continued)

Page 12: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

12Microsoft Visual Basic .NET: Reloaded

The Do…Loop Statement

• Can be used to code both pretest and posttest loops• Do While or Do Until is a pretest loop

• Pretest loops should have a “priming read”• Data input entry placed above loop

• Loop While or Loop Until is a posttest loop• [While|Until] portion can only contain one of

the keywords (While or Until)• While processes instructions as long as the

condition evaluates true• Until processes instructions until the condition

becomes true

Page 13: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

13Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 14: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

14Microsoft Visual Basic .NET: Reloaded

The Do…Loop Statement (continued)

Page 15: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

15Microsoft Visual Basic .NET: Reloaded

The Do…Loop Statement (continued)

Page 16: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

16Microsoft Visual Basic .NET: Reloaded

The Do…Loop Statement (continued)

Page 17: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

17Microsoft Visual Basic .NET: Reloaded

The Do…Loop Statement (continued)

Page 18: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

18Microsoft Visual Basic .NET: Reloaded

The Do…Loop Statement (continued)

Page 19: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

19Microsoft Visual Basic .NET: Reloaded

Using Counters and Accumulators

• Counter – numeric variable used for counting something number of employees paid in a week

• Accumulator – numeric variable used for calculating running totals total dollar amount of weeks payroll

• Initializing – assigning a beginning value to a variable

• Updating (incrementing) – adding a number to the value stored in the counter or accumulator

Page 20: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

20Microsoft Visual Basic .NET: Reloaded

The Sales Express Application

• Application for sales manager to display average amount the company sold during the prior year.

• Sales manager will enter each salesperson’s sales

• Application will use a counter to keep track of number of sales amounts and an accumulator to total those amounts

Page 21: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

21Microsoft Visual Basic .NET: Reloaded

The Sales Express Application (continued)

Page 22: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

22Microsoft Visual Basic .NET: Reloaded

The InputBox Function

• Displays a dialog box that prompts the user in input information

• Contains a prompt, one or more buttons, and an input area for data entry

• Prompt - message you want displayed• Title displays caption of input box• defaultResponse - initial text displayed in the

input area• Ok button returns value in input area, Cancel

and Close buttons return empty string

Page 23: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

23Microsoft Visual Basic .NET: Reloaded

The InputBox Function (continued)

Page 24: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

24Microsoft Visual Basic .NET: Reloaded

The InputBox Function (continued)

Page 25: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

25Microsoft Visual Basic .NET: Reloaded

Code for the Sales Express Application

Page 26: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

26Microsoft Visual Basic .NET: Reloaded

Programming Example – Grade Calculator

• Application allows the user to enter the points a student earns on 4 projects and 2 tests

• Totals the points earned and calculates grade• Total Points Earned Grade

360-400 A

320-359 B

280-319 C

240-279 D

below 240 F

• Displays total points and grade earned

Page 27: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

27Microsoft Visual Basic .NET: Reloaded

TOE Chart

Page 28: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

28Microsoft Visual Basic .NET: Reloaded

User Interface

Page 29: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

29Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings

Page 30: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

30Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings (continued)

Page 31: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

31Microsoft Visual Basic .NET: Reloaded

Tab Order

Page 32: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

32Microsoft Visual Basic .NET: Reloaded

PseudoCode

btnExit Click event procedure

Close application

btnAssignGrade Click event procedure

1. repeat while the number of projects counter is less than 5

if the points earned is numeric

add the project points to the total points accumulator

add 1 to the number of projects counter

else

display an appropriate message in a message box

end if

end repeat

(continued on next slide)

Page 33: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

33Microsoft Visual Basic .NET: Reloaded

PseudoCode (continued)2. repeat while the number of tests counter is less than 3

get the points earned on the test

if the points earned is numeric

add the test points to the total points accumulator

add 1 to the number of tests counter

else

display an appropriate message in a message box

end if

end repeat

(continued on next page)

Page 34: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

34Microsoft Visual Basic .NET: Reloaded

PseudoCode (continued)3. Assign the grade based on the total points earned value:

>= 360 assign A as the grade

>= 320 assign B as the grade

>= 280 assign C as the grade

>= 240 assign D as the grade

<240 assign F as the grade

4. Display the total points earned in lblTotalPoints

5. Display the grade in lblGrade

Page 35: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

35Microsoft Visual Basic .NET: Reloaded

Code

Page 36: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

36Microsoft Visual Basic .NET: Reloaded

Code (continued)

Page 37: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

37Microsoft Visual Basic .NET: Reloaded

Summary

• Three programming structures are sequence, selection, and repetition

• Repetition (loop) allows program to repeatedly process one or more program instructions

• Pretest loops test before program instructions

• Posttest loops test after program instructions and will always be processed at least once

• Pmt function calculates payment on a loan

Page 38: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

38Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• The condition in a loop evaluates to a Boolean variable

• While processes loop instructions while the condition is true

• Until processes loop instructions until the condition becomes true

• Use a counter or accumulator to calculate subtotals, totals and averages

• Counters and accumulators must be initialized and updated

Page 39: Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1

39Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• InputBox Function displays a dialog box that displays a message, an OK button, a Cancel button, and an input area.

• Test for possible division by zero to prevent an error ending the program prematurely