Introduction to Computer Programming IT-104 - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Computer Programming IT-104

Description:

Objectives Introduction Determinate Indeterminate Loop Types Event Do-While Exit While-Wend Do-Until For-Next Introduction to Computer Programming IT-104 – PowerPoint PPT presentation

Number of Views:116
Avg rating:3.0/5.0
Slides: 33
Provided by: ittesi
Learn more at: http://www.afn.org
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Computer Programming IT-104


1
Objectives
Introduction
Determinate
Indeterminate
Loop Types
Event
Do-While
Exit
While-Wend
Do-Until
For-Next
  • Introduction to Computer Programming IT-104
  • Unit Six Arrays

2
Objectives
  • Understand the use of control, list, and table
    arrays in Visual Basic
  • Use control arrays to work with groups of
    controls using a single name.
  • Describe the difference between arrays and combo
    boxes, list boxes, and similar controls.
  • Declare the maximum number of elements or rows
    and columns for an array.

3
Objectives
  • Understand the errors that can occur from
    exceeding the declared upper limits on index
    values.
  • Input data into an array from the keyboard or
    files using loops.
  • Manipulate array data to find the sum and average
    of array values, the largest or smallest value in
    an array, or to find a particular value.
  • Work with multiple arrays to match values in one
    array to those in the other.

4
Objectives
  • Output the result of array processing to a
    control, the printer, or the Immediate window.
  • Use Step operations to step though an array
    operation to find and correct an error.

5
Introduction
Home
  • Arrays are an important part of computer
    programming. An Array is a special type of
    variable structure implemented in virtually every
    computer language. Without the array and the
    loop collectively, programming a computer would
    be a daunting task indeed.

6
Using Control Arrays
  • A control array is group of the same type control
    which is assigned a single name.
  • The individual controls are identified by the
    control array name and an index value starting
    with 0.
  • The Case decision structure can be useful in
    working with a control array.
  • To create a control array, just enter the same

7
Using Control Arrays
Home
  • name for more than one control and replay yes
    to the query.
  • Alternatively, simply copy an existing control
    and paste it to your existing form, and then
    answer Yes to the Confirm Control Array
    dialog box.
  • The order in which the controls are entered
    determines the index values for the controls in
    the control array.

8
Check Boxes Vs. Option Buttons
Home
  • Check box is used to make multiple selections
    from a list
  • Prefix is chk
  • Option button is used to make one selection from
    a list.
  • Often referred to as radio buttons
  • Prefix is opt

9
Frame Control
Home
  • Option buttons must be within a frame control
    which acts as a container .
  • Frames have a prefix of fra.
  • Recommendation is to add frame first and add the
    option button by drawing it in, not double
    clicking.

10
Frame Control
  • All option buttons will have the same name but a
    different Index, such as optDemo(0), optDemo(1),
    optDemo(2).
  • If the frame is moved, option buttons will move
    with it.

11
Option Button Array
Home
  • Double click any option button to open the code
    window and it will have event similar to this
  • Private Sub optDemo_Click(Index as Integer)
  • The Index value is being passed to the event
    procedure, in other words, the index of the
    option button that was clicked is now known to
    the event procedure.
  • The Index value is used is Select Case
    statements.

12
VB Code for Option Buttons
  • Private Sub optDemo_Click(Index As Integer)
  • Select Case Index
  • Case 0
  • MsgBox "Button One selected"
  • Case 1
  • MsgBox "Button Two selected"
  • Case 2
  • MsgBox "Button Three selected"
  • End Select
  • End Sub

13
Check Box Control Array
  • All check boxes will have the same name but a
    different Value, such as chkSelect(0).Value,
    chkSelect(1).Value, chkSelect(2).value
  • The Value will either be True or False. It is
    True if the check box is selected and False if
    not selected
  • To determine if it is False use the Not operator
  • Code on Next slide is used to check if a value is
    True or False

14
VB Code for Check Box Control Array
  • Private Sub cmdSelect_Click()
  • If chkSelect(0).Value And Not
    chkSelect(1).Value Then
  • MsgBox "Box One selected"
  • ElseIf Not chkSelect(0).Value And
    chkSelect(1).Value Then
  • MsgBox "Box Two selected"
  • ElseIf chkSelect(0).Value And
    chkSelect(1).Value Then
  • MsgBox "Boxes One and Two selected"
  • Else
  • MsgBox "No boxes selected"
  • End If
  • End Sub

15
VB Code Select Prices Using Option Buttons (Pos
demo)
  • Private Sub OptTypes_Click(Index As Integer)
  • Dim curItemPrice As Currency
  • Select Case Index
  • Case 0 Perishables
  • curItemPrice 3.99
  • Case 1 Staples
  • curItemPrice 1.99
  • Case 2 Hardware
  • curItemPrice 5.99
  • Case 3 Cleaning supplies
  • curItemPrice 2.99
  • End Select
  • txtItemPrice.text Format(curItemPrice,
    "currency")
  • End Sub

16
VB Code to Clear the OptTypes Option Buttons
Home
  • Private Sub cmdClear_Click()
  • For Counter 0 to 2 Clear option buttons
  • OptTypes(Counter).Value False
  • Next
  • End Sub

17
Using List Arrays
  • Arrays are lists or tables of data which have
    single name. They provide a way of working with
    long lists in memory versus working with short
    lists in a list or combo box.
  • Arrays can only store one type of data.
  • Individual array elements are identified by the
    array name and one or more subscripts or index
    values. For instance
  • curPrices(0), curPrices(1), curPrices(2)
  • The index must be an integer constant, variable
    or

18
Using List Arrays
  • expression.
  • To be used in Visual Basic, an array must be
    declared just like any other variable.
  • Arrays can be fixed size or dynamic size (we use
    only fixed arrays.)
  • Fixed size-array- specific amount of memory set
    aside for it.
  • Dynamic array - no fixed amount of memory is
    reserved.

19
Using List Arrays
  • Form of array declaration
  • Dim ArrayName (max index value) as variable type
  • Dim curPrices(9) as Currency
  • Above statement allows for 10 values to be stored
    (0-9)
  • The declaration statement defines the upper limit
    but by default the lower limit on an array index
    is zero but this can be changed to one or any
    other value.
  • It is not possible to exceed the upper limit or
    go below the lower limit on the array index
    values . Will receive a Subscript out of range
    error

20
Using List Arrays
Home
  • If a lower bound other than zero is desired, the
    programmer can implement his own numbering scheme
    as in the following
  • Dim sngRate(100 to 199) as Single
  • This statement allocates 100 elements, but the
    lower bound is now 100 instead of 0. This gives
    the programmer the flexibility of matching
    existing numbering schemes that may exist within
    files he has been given to process.

21
Entering Event Driven Array Data
  • Most of the time you want to input multiple
    values into an array.
  • Arrays can be input with any of the three types
    of loops discussed earlier--event driven,
    for-next, or while/until loops.
  • If input is from a keyboard and you dont know
    the values to be input, an event driven loop is
    most appropriate. Similar to loops in Chapter 5
    except values are entered directly in an array
    versus being added to a list box. Values dont
    need to be summed

22
Entering Event Driven Array Data
  • immediately.
  • Index for the array corresponds to the value of
    the counting variable for each value that is
    input.
  • With an event driven loop, each click of a button
    increments a counter and inputs the corresponding
    array element.

23
Input Using For-Next Loops
  • With a For-Next loop, you must input the number
    of arrays elements. The array values must be
    input with an Inputbox with the For-Next counter
    variable matching the array index
  • Private Sub Input_Click()
  • Dim intCounter As Integer
  • intNumPrices CInt(InputBox("How many
    prices?"))
  • For intCounter 0 To intNumPrices - 1
  • curPrices(Counter) CCur(InputBox("Next
    price"))
  • Next
  • End Sub

24
Processing Arrays
  • Typical array operations include summing and
    averaging the array values and finding the
    largest or smallest value in the array.
  • Working with arrays usually involves a For-Next
    loop.
  • Summing Prices array elements involves using a
    statement of the form Sum Sum
    Prices(Counter)
  • Averaging Prices array elements involves
    dividing the Sum by the number of elements

25
Processing Arrays
  • Finding the largest or smallest value involves
    multiple comparisons of array elements.
  • To find the maximum Price, you must compare each
    array value to the current highest price if it
    is higher, it becomes the highest price. Do this
    using For-Next loop and If-then

26
VB Code to Compute the Sum and Average in Array
Home
  • Private Sub CmdSumAverage_Click()
  • Dim curSum as Currency Sum variable
  • Dim intCounter as Integer counter variable
  • Dim curAverage as Currency Average variable
  • curSum 0 Set sum to 0
  • For intCounter 0 to intNumPrices - 1
  • curSum curSum curPrices(intCounter) add
    prices
  • Next
  • If intNumPrices gt0 Then check to see if gt0
  • curAverage curSum/intNumPrices true average
    prices
  • Else if false
  • MsgBox "No values to average! display Msgbox
  • Exit Sub
  • End If
  • txtSum.TextFormat(curSum, currency)
  • txtAverage.Text Format (curAverage, currency
  • End Sub

27
Pseudocode to Find Maximum Value
  • Begin procedure to find largest value
  • Set largest value to first item in list
  • Repeat beginning with second item to last item
  • If item in list gt largest value then
  • Largest value item in list
  • End decision
  • End repeat
  • Display largest value
  • End procedure

28
VB Code to Find Maximum Value in List
  • Private Sub cmdFindMax_Click()
  • Declare variables and set largest value to first
    item in the array
  • Dim intCounter As Integer, curLargest As
    Currency
  • curLargest curPrices(0)
  • For intCounter 1 To intNumPrices - 1
  • If curPrices(intCounter) gtcurLargest Then if
    item gtlargest
  • curLargest curPrices(intCounter)
    replace it if it is larger
  • End If
  • Next
  • Display largest number as currency
  • txtMaxPrice.Text Format(curLargest, "currency")
  • End Sub

29
Code to Find Minimum Value in List
  • Private Sub cmdFindMin_Click()
  • Declare variables and set smallest value to
    first item in the array
  • Dim intCounter As Integer, curSmallest As
    Currency
  • curSmallest Prices(0)
  • For intCounter 1 To intNumPrices - 1
  • If curPrices(intCounter) lt curSmallest Then
    if item gt smallest
  • curSmallest Prices(Counter) replace if
    it is smaller
  • End If
  • Next
  • Display smallest number as currency
  • txtMaxPrice.Text Format(curSmallest,
    "currency")
  • End Sub

30
Find the Largest/Smallest String Value in a List
  • Collating Sequence - order in which characters
    are displayed
  • Digits come before alphabetic characters.
  • Upper case A is smaller than uppercase B
    because it comes first in alphabetical ordering
  • Lower case letters come after upper case letters
  • Chr() function converts Integer values of the
    For-Next variable into the corresponding
    characters
  • To reverse operation use the ASC() function with
    the character as the argument

31
VB Code for Displayingthe Collating Sequence
  • Private Sub Form_Load()
  • Dim intCounter As Integer
  • For intCounter 0 To 255
  • Debug.Print Chr(intCounter)
  • Next
  • End Sub

32
Finding Items and Working with Multiple Lists
  • The goal of the project is to find a specified
    part ID and display the part identifier and the
    price of the part
  • To input both the part identifiers and a price a
    second array, PartID needs to be declared as a
    string in the Form General declaration section
  • Dim strPartId(25) as String
  • The Input Statement in the Form-Load event must
    be modified
  • Input 5, strPartID(intNumPrices),
    curPrices(intNumPrices)
  • For a given part, the index values or the
    strPartId, and the curPrices arrays are the same.
    The index acts as a link between them.
Write a Comment
User Comments (0)
About PowerShow.com