Programming Structures - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Programming Structures

Description:

You also can use the Alignment property to align the contents of a text box, but ... Displays one of Visual Basic's predefined dialog boxes, which contains a message, ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 31
Provided by: Dian71
Category:

less

Transcript and Presenter's Notes

Title: Programming Structures


1
Programming Structures
  • Sequence - program instructions are processed,
    one after another, in the order in which they
    appear in the program
  • Selection - allows the program to make a
    decision/comparison and then select the
    appropriate action to take based on that
    decision/comparison
  • Repetition - (looping or iteration) tells the
    computer to repeat one or more instructions
    either a specified number of times or until some
    condition is met

2
Forms of the Repetition Structure
  • For Next
  • Do While
  • Do Until

3
For Next Loop
  • Tells the computer to repeat one or more
    statements a specified number of times. Called a
    pretest loop because the loop is evaluated before
    the instructions are processed
  • Syntax
  • For counter startvalue To endvalue Step
    stepvalue
  • instructions you want repeated
  • Next counter
  • counter is the name of a numeric variable and it
    keeps track of how many times the loop
    instructions are repeated
  • startvalue, endvalue, and stepvalue must be
    numeric and they can be either positive or
    negative, integer or non-integer (default
    stepvalue is 1)

4
For Next Loop Tasks
  • The loop initializes the counter to the
    startvalue (done only once, at the beginning of
    the loop)
  • If the stepvalue is positive (negative), the loop
    checks if the value in counter is greater than
    (less than) the endvalue. If it is, the loop
    stops otherwise the instructions within the loop
    are processed and the next task is performed
  • The loop adds the stepvalue to the counter. It
    then repeats the prior two steps

5
Flowchart and Pseudocode
  • Hexagon
  • Repeat for intCount 1 to 3 by 1
  • Print intCount
  • Next intCount

intCount
gt 3
1
1
Print intCount
6
Do While and Do Until Loops
  • Do While loop repeats a block of instructions
    while a condition is true
  • You also can use a Do While loop to repeat a
    block of instructions a specified number of times
  • Do Until loop repeats a block of instructions
    until a condition becomes true
  • You also can use a Do Until loop to repeat a
    block of instructions a specified number of times

7
Syntax of the Do While and Do Until Loops
  • Do While condition
  • loop instructions
  • Loop
  • Do
  • loop instructions
  • Loop Until condition
  • condition must evaluate to true or false
  • condition can contain variables, constants,
    properties, functions, mathematical operators,
    relational operators, and logical operators

8
Do While and Do Until Loops
  • In the Do While loop, the instructions are
    processed only when the condition evaluates to
    true the loop stops when the condition evaluates
    to false
  • Called a pretest loop
  • In the Do Until loop, the instructions are
    processed only when the condition evaluates to
    false the loop stops when the condition
    evaluates to true
  • Called a posttest loop

9
Do While Loop
intCount 1
  • intCount 1
  • Repeat while intCount lt 3
  • Display intCount
  • Add 1 to intCount
  • End Repeat while intCount lt 3

intCount lt 3
F
T
Display intCount
intCount intCount 1
10
Do Until Loop
intCount 1
  • intCount 1
  • Repeat
  • Display intCount
  • Add 1 to intCount
  • End Repeat until intCount gt 3

Display intCount
intCount intCount 1
intCount gt 3
T
F
11
Counters and Accumulators
  • Used within a repetition structure to calculate
    subtotals, totals, and averages
  • Initialized (usually to 0 or 1) outside the loop
    and updated within the loop
  • A counter is a numeric variable used for counting
    something and is typically updated by 1
  • An accumulator is a numeric variable used for
    accumulating (adding together) and is updated by
    an amount that varies

12
Calculating an Average
  • Average accumulator / counter
  • Be sure to verify that counter is greater than 0
    before calculating the average
  • Use the IfThenElse selection structure

13
Control Arrays
  • Group of controls of the same type that have the
    same name and share the same set of event
    procedures
  • Each control in the array is identified by the
    arrays name and a unique index
  • The first control in the array has an index of 0

14
Creating a Control Array
  • To make existing controls into an array, simply
    assign the same name to each control. When you
    are asked if you want to create a control array,
    click the Yes button
  • If the controls are not already on the form,
    place the first control on the form and set its
    name and other properties
  • Copy the control to the clipboard, then paste the
    appropriate number of controls on the form
  • When you are asked if you want to create a
    control array, click the Yes button

15
Control Array Code Window
  • Index As Integer appears in the Code windows for
    a control array
  • All controls in the array share the same set of
    Code windows
  • Index contains an integer that represents the
    value stored in the Index property of the control
    receiving the event

16
Index Property vs Index Argument
  • The Index property stores a number that
    identifies each control in the array
  • Each control in the array has a value in its
    Index property
  • The Index property is like an address
  • The Index argument found in the Code window is a
    local variable created by Visual Basic
  • The Index argument contains the address of the
    control receiving the event

17
Parallel Arrays
  • Arrays whose contents are related by their
    position in the arrays--in other words, by their
    index
  • chkDone(0) txtScore(0)
  • chkDone(1) txtScore(1)

18
Enabled Property
  • An objects Enabled property determines whether
    the object can respond to user-generated events,
    such as clicking or entering information
  • Can be set to True (default) or False
  • It is customary in Windows applications to
    disable objects that dont apply to the current
    state of the application

19
GotFocus Event
  • Occurs when a control receives the focus
  • It is customary in Windows applications to
    highlight a text boxs existing text when the
    text box receives the focus. You do so by setting
    the text boxs SelStart and SelLength properties

20
Change Event
  • Occurs when the contents of an object are changed
  • You can use this event to clear the results of
    calculations from label controls when the
    contents of a text box has changed

21
Alignment Property
  • You can use the Alignment property to align the
    contents of a label control
  • You also can use the Alignment property to align
    the contents of a text box, but you must be sure
    to set the text boxs MultiLine property to True
    and also code its KeyPress event to prevent the
    text box from recognizing the Enter key

22
MsgBox Function
  • Displays one of Visual Basics predefined dialog
    boxes, which contains a message, one or more
    command buttons, and an icon
  • After displaying the dialog box, the MsgBox
    function waits for the user to choose a button,
    then returns a value that indicates which button
    the user selected

23
MsgBox Function
  • Syntax
  • MsgBox(prompt, buttons, title,helpfile,
    context)
  • prompt is the message you want displayed
  • buttons is a numeric expression that specifies
    the number and type of buttons, the icon, the
    default button, and the modality
  • title is a string expression displayed in the
    title bar

24
Modality
  • Application modal
  • Default modality user must respond to the dialog
    boxs message before he or she can continue
    working in the current application the user
    still can access other applications
  • System modal
  • All applications are suspended until the user
    responds to the dialog boxs message

25
buttons Argument
  • Constant Value Description
  • vbOKOnly 0 Display OK button only.
  • vbOKCancel 1 Display OK and Cancel buttons.
  • vbAbortRetryIgnore 2 Display Abort, Retry, and
    Ignore buttons.
  • vbYesNoCancel 3 Display Yes, No, and Cancel
    buttons.
  • vbYesNo 4 Display Yes and No buttons.
  • vbRetryCancel 5 Display Retry and Cancel
    buttons.
  • vbCritical 16 Display Critical Message icon.
  • vbQuestion 32 Display Warning Query icon.
  • vbExclamation 48 Display Warning Message icon.
  • vbInformation 64 Display Information Message
    icon.
  • vbDefaultButton1 0 First button is default.
  • vbDefaultButton2 256 Second button is default.
  • vbDefaultButton3 512 Third button is default.
  • vbDefaultButton4 768 Fourth button is default.
  • vbApplicationModal 0 Application modal the user
    must respond to the message box
  • before continuing work in the current
    application.
  • vbSystemModal 4096 System modal all applications
    are suspended until the user
  • responds to the message box.

26
Return Values
  • Constant Value Description
  • vbOK 1 OK
  • vbCancel 2 Cancel
  • vbAbort 3 Abort
  • vbRetry 4 Retry
  • vbIgnore 5 Ignore
  • vbYes 6 Yes
  • vbNo 7 No

27
Unload Event
  • Occurs when a form is about to be removed from
    the screen and memory
  • Triggered when you use the Unload statement in
    code or when you click the Close button to close
    the form
  • Use the Unload event to verify that the user
    wants to exit an application and also for code
    that saves and closes files

28
Unload Statement vs Unload Event
  • The Unload statement tells Visual Basic to unload
    the form
  • The Unload event is the procedure that occurs
    before the form is unloaded

29
Unload Event Code Window
  • The Cancel argument is a local variable created
    by Visual Basic and it contains the value 0
  • To prevent the form from being unloaded, set
    Cancel to a value other than 0

30
Debugging Technique
  • You can use the KeyPress event to control what
    the user can enter into a text box
Write a Comment
User Comments (0)
About PowerShow.com