Repetition - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Repetition

Description:

Chapter 6 Repetition Outline & Objectives Loop Structure Elements of a Loop Structure Processing Lists of Data with Do Loops Option buttons vs. Check boxes Multiple ... – PowerPoint PPT presentation

Number of Views:128
Avg rating:3.0/5.0
Slides: 39
Provided by: FayeN1
Category:

less

Transcript and Presenter's Notes

Title: Repetition


1
Chapter 6
  • Repetition

2
Outline Objectives
  • Loop Structure
  • Elements of a Loop Structure
  • Processing Lists of Data with Do Loops
  • Option buttons vs. Check boxes
  • Multiple forms

3
Types of LOOP Structures
  • Do While . Loop
  • Do Until Loop
  • For Next loop

4
Basic Definition
  • Looping the process of repeating a series of
    statements as many times as needed.
  • Looping also called iteration.

5
Basic Components of Loops
  • Loop control variable A variable used to
    determine whether a loop will be executed
  • Loop body The statement (s) that are executed
    each time a loop repeats

6
The Do While . Loop
  • Do While condition is true
  • statement(s)
  • Loop
  • Compare this to IF . Then

7
Flowchart for a Do While Loop
Is the condition true
No
Yes
Execute statements within the loop
Execute statements that follow the loop
8
The Do While . Loop
  • Is executed as long as the condition is True.
  • If condition is False then the next statement
    after the Loop is executed.
  • Step Demo DoCount

9
Controlling Loops
  • Methods of controlling loops
  • Counter-controlled loops
  • repeat a specific number of times
  • Event-controlled loops
  • repeat until something happens in the loop body
    to change the value of loop control variable.

10
Example of event-controlled loops
  • passWord ""
  • Do While passWord ltgt "SHAZAM"
  • passWord UCase(InputBox("What is the
    password?"))
  • Loop

11
Counter-controlled Loops
  • Is useful when the programmer knows how many
    times the loop should be executed.
  • Initialize the counter by setting it to a
    beginning value before entering the loop.
  • The counter is increased (or decreased) by the
    same value during each repetition.

12
Example
  • num 1
  • Do While num lt 10
  • picOutput.Print num
  • num num 1
  • Loop
  • Watch out for infinite loops!

13
Do Until . Loop
  • Is executed until the condition becomes True
  • Any Do While. Loop can be rewritten as a Do
    Until .. Loop

14
Example (requires the user to give a password
before opening a file)
  • Do
  • passWord UCase(InputBox("What is the
    password?"))
  • Loop Until passWord "SHAZAM
  • Demo - CTRLBreak

15
Example (years to deplete a saving account)
  • Jane has saved 150,000 for retirement. She
    is now 65 and wants to draw 15,000 per year to
    supplement her pension. She is earning interest
    on her account at the rate of 7.5 per year. How
    old will Jane be before all her savings are used
    up?
  • Demo Savings

16
Comparing While and Until Loops
  • The Do While Loop executes while the condition
    is true
  • The Do Until.. Loop executes until the condition
    is true
  • Both can be used to create any type of loop

17
Schneider p. 255 - 261
18
Processing List of Data with Loops
  • EOF Function
  • The EOF function tells us if we have read to the
    end of a file.
  • Checks for the end-of-file marker.

19
EOF Function
  • EOF(n) where n is the reference number for the
    file.
  • If there are more records to be read, EOF is
    False.
  • When the end-of-file marker is reached EOF
    becomes True.

20
Example of EOF
  • Open Phone.txt for Input As 1
  • Do While Not EOF(1)
  • Input 1, nom, phoneNum
  • picOutput.Print nom, phoneNum
  • Loop
  • Close 1

21
Counters and Accumulators
  • A Counter is a numeric variable that keeps track
    of the number of items that have been processed
    in the loop.
  • An Accumulator is a numeric variable that totals
    numbers.
  • Demo Average

22
Example Counter Accumulator
  • Private Sub cmdAnalyze_Click()
  • Dim numCoins As Integer, sum As Single, value
    As Single
  • Open "COINS.TXT" For Input As 1
  • numCoins 0
  • sum 0
  • Do While Not EOF(1)
  • Input 1, value
  • numCoins numCoins 1
  • sum sum value
  • Loop
  • picValue.Print "The value of the" numCoins
    "coins is" sum "cents."
  • Close 1
  • End Sub

23
Example of a Data File
  • 1, 1, 5, 10, 10, 25

24
Output for the Previous Example
  • The value of the 6 coins is 52 cents.

25
For ... Next Loop
  • Is used to create a counting loop.
  • Loop control variable has an initial value.
  • Loop control variable has a terminating value.
  • Loop control variable has a Step value.

26
Example ( display a table of the first 5 numbers
and their square)
Terminating value
  • For i 1 To 5
  • picTable.Print i i 2
  • Next i
  • Demo counter

Initial Value
Loop Control variable
27
Rules for Using For ... Next loop
  • The initial, terminal, and step values cannot be
    modified in the loop body.
  • You should never modify the value of the loop
    control variable in the loop body.
  • Each For statement must end with a Next statement.

28
Example (the step value is negative)
  • For Counter 8 To 1 Step -2
  • picOutput.Print Counter
  • Next Counter

29
Nested Loops
  • For Outer 1 To 4
  • For Inner 1 To 2
  • ..
  • ..
  • Next Inner
  • Next Outer

30
  • For i 1 To 10
  • For j 1 To 10
  • picOutput.Print ""
  • Next j
  • picOutput.Print
  • Next I
  • What wil this code do?

31
Schneider p. 268 275Schneider p. 282 - 289
32
Option Buttons
  • Used in situations where you want to limit the
    user to only one of two or more related and
    mutually exclusive choices
  • Only one in a group can be selected (on) at any
    one time
  • When selected, an option buttons Value property
    contains the Boolean value True otherwise it
    contains the Boolean value False

33
Option Buttons
  • Minimum number in an interface is two
  • Recommended maximum number is seven
  • A default button should be selected when the
    interface first appears (use form_load event)
  • Demo State_opt

34
Frame Control
  • Acts as a container for other controls
  • Used to visually separate controls from one
    another
  • The frame and the controls contained within the
    frame are treated as one unit
  • You must use a frame control if you want to have
    more than one group of option buttons (Demo)

35
Check Box
  • Used in situations where you want to allow the
    user to select any number of choices from one or
    more independent and non-exclusive choices
  • Any number of check boxes can be selected at any
    one time
  • When selected, a check boxs Value property
    contains the number 1 (vbChecked). When
    unselected, it contains the number 0 (vbUnchecked)

36
Check Box
  • Assign a unique access to each check box
  • You also can use the spacebar to select/deselect
    a check box that has the focus
  • Unlike option buttons, you must test a checkbox
    when it is clicked to see if it is checked or
    unchecked

37
  • Demo Rentals_opt
  • Demo Math and Math2

38
Multiple Forms
  • Adding a second form
  • Form load and unload commands
  • Form hide and show commands
  • Syntax - referring to controls on other forms
  • Demo - Order
Write a Comment
User Comments (0)
About PowerShow.com